一、項目框架準備 1.1 新建maven空項目,併在pom中引入依賴 1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <v ...
一、項目框架準備
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 @RestController 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 }