springboot+maven+idea+freemarker
这几天刚接触springboot,感觉还好,只不过发布的时候总是404,命名配置好的tomcat,每一步没有问题,先打了war.exploded,然后点击插件里面的package,最后配置的tomcat,最终才知道,根本不需要配置tomcat,直接运行springboot那个就好了。
遇到了很多的问题,打jar或者war包的时候需要用到
打包的结果:
整体目录:
package com.example.demo.bean; public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Dao:
package com.example.demo.dao; import com.example.demo.bean.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper { @Select("select * from user where id = #{userId}") User findUserById(@Param("userId") Integer userId); }
Service:
package com.example.demo.service; import com.example.demo.bean.User; public interface UserService { User getUserById(Integer id); } ========================================= package com.example.demo.service.impl; import com.example.demo.bean.User; import com.example.demo.dao.UserMapper; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.Serializable; @Service public class UserServiceimpl implements UserService, Serializable { @Autowired private UserMapper userMapper; public User getUserById(Integer id){ return userMapper.findUserById(id); } }
Controller:
package com.example.demo.controller; import com.example.demo.bean.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @RestController public class TestCotroller { @Autowired UserService userService; @RequestMapping("/test") public ModelAndView test(@RequestParam(defaultValue = "1")Integer id) { //访问路径:http://localhost:8080/test ModelAndView modelAndView = new ModelAndView(); User user = userService.getUserById(id); System.out.println(user.getId()); modelAndView.addObject("user",user); modelAndView.setViewName("test"); System.out.println("aaaaaa"); return modelAndView; } }
test.ftl:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 我是qin,我是${user.id} ,${user.name} </body> </html>
application.properties:
spring.datasource.url = jdbc:mysql://localhost:3306/lsq spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver #设定ftl文件路径 spring.freemarker.template-loader-path=classpath:/templates #设定静态文件路径,js,css等 spring.mvc.static-path-pattern=/static/**
main:
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 运行结果:
我是qin,我是1 ,王