一、项目框架准备
1.1 新建maven空项目,并在pom中引入依赖
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.1.12.RELEASE</version> 5 </parent> 6 7 <properties> 8 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 9 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 10 <java.version>1.8</java.version> 11 </properties> 12 13 <dependencies> 14 <!-- web启动器 --> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-web</artifactId> 18 </dependency> 19 <!--jdbc连接及其启动器 --> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-jdbc</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 <version>5.1.48</version> 28 </dependency> 29 <!--tk.mybatis及mybatis启动器 --> 30 <dependency> 31 <groupId>org.mybatis.spring.boot</groupId> 32 <artifactId>mybatis-spring-boot-starter</artifactId> 33 <version>1.3.2</version> 34 </dependency> 35 <dependency> 36 <groupId>tk.mybatis</groupId> 37 <artifactId>mapper-spring-boot-starter</artifactId> 38 <version>2.1.5</version> 39 </dependency> 40 <!--thymeleaf启动器 --> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-thymeleaf</artifactId> 44 </dependency> 45 </dependencies> 46 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build>
1.2 准备mysql数据表
1 CREATE TABLE `user` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `username` varchar(255) NOT NULL, 4 `password` varchar(255) NOT NULL, 5 `birthday` date DEFAULT NULL, 6 `address` varchar(255) DEFAULT NULL, 7 PRIMARY KEY (`id`) 8 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; 9 INSERT INTO `user` VALUES ('1', '张三', '12345', '2020-01-16', '许昌市'); 10 INSERT INTO `user` VALUES ('2', '李四', '54321', '2020-01-01', '河南省许昌市');
二、实现数据的展示
2.1 新建启动入口并测试
在java文件夹中新建 MyApplication.java 文件,作为程序的启动入口。
1 @SpringBootApplication 2 public class MyApplication { 3 public static void main(String[] args) { 4 SpringApplication.run(MyApplication.class, args); 5 } 6 }
在resources文件夹中新建application.yml文件,配置端口和数据库连接
1 server: 2 port: 7001 3 spring: 4 datasource: 5 driver-class-name: com.mysql.jdbc.Driver 6 url: jdbc:mysql://localhost:3306/mydb 7 username: root 8 password: fanbao0713
新建controller --> UserController.java 文件,并启动测试:http://localhost:7001/user/hello
1 @Controller 2 @RequestMapping("user") 3 public class UserController { 4 @GetMapping("hello") 5 public String hello(){ 6 return "Hello World!"; 7 } 8 }
2.2 数据库连接配置
新建 pojo --> User.java 文件:
1 @Table(name = "user") 2 public class User { 3 @Id 4 @GeneratedValue(strategy = GenerationType.IDENTITY) 5 private Integer id; 6 private String username; 7 private String password; 8 private Date birthday; 9 private String address; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 19 public String getUsername() { 20 return username; 21 } 22 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 35 public Date getBirthday() { 36 return birthday; 37 } 38 39 public void setBirthday(Date birthday) { 40 this.birthday = birthday; 41 } 42 43 public String getAddress() { 44 return address; 45 } 46 47 public void setAddress(String address) { 48 this.address = address; 49 } 50 }
在 application.yml 文件中,新增mybatis关联
1 mybatis: 2 type-aliases-package: com.springbootdemo.pojo
在 mapper --> UserMapper.java 文件:
1 @Mapper 2 public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> { 3 }
在 service --> UserService.java 文件,由于tk.mybati不是spring官方插件,“userMapper”会报错,可以忽略:
1 @Service 2 public class UserService { 3 @Autowired 4 private UserMapper userMapper; 5 6 public User selectById(Long id){ 7 return this.userMapper.selectByPrimaryKey(id); 8 } 9 10 public List<User> selectAll(){ 11 return userMapper.selectAll(); 12 } 13 }
在 controller --> UserController.java 文件中,展示接口,并进行测试:http://localhost:7001/user/findAll
1 @RestController 2 @RequestMapping("user") 3 public class UserController { 4 @Autowired 5 private UserService userService; 6 7 @GetMapping("{id}") 8 public User selectById(@PathVariable("id") Long id){ 9 return this.userService.selectById(id); 10 } 11 12 @GetMapping("findAll") 13 public Map<String, Object> findAll(){ 14 Map<String, Object> map = new HashMap<>(); 15 map.put("用户列表", userService.selectAll()); 16 return map; 17 } 18 }
三、静态资源访问及拦截器
3.1 访问静态资源
在 resources 文件夹中新增static中,并加入目录中,static中的各类资源可以直接访问:http://localhost:7001/js/common.js
3.2 拦截器设置
在 interceptors --> UserInterceptor中,首先我们定义一个拦截器:
1 @Component 2 public class UserInterceptor implements HandlerInterceptor { 3 @Override 4 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 5 System.out.println("preHandle/前置拦截器 method is running!"); 6 return true; 7 } 8 9 @Override 10 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 11 System.out.println("postHandle/运行拦截器 method is running!"); 12 } 13 14 @Override 15 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 16 System.out.println("postHandle/后置拦截器 method is running!"); 17 } 18 }
新建 config --> MvcConfig.java 中,定义配置类,注册拦截器:
1 @Configuration 2 public class MvcConfig implements WebMvcConfigurer { 3 @Autowired 4 private HandlerInterceptor handlerInterceptor; 5 6 @Override 7 public void addInterceptors(InterceptorRegistry registry) { 8 registry.addInterceptor(handlerInterceptor).addPathPatterns("/**"); 9 } 10 }