本章介绍SpringBoot与Mybatis整合
整合流程
1、准备一个数据库,建一个表,内容如下:
1 CREATE DATABASE test_mybatis; 2 USE test_mybatis; 3 4 -- ---------------------------- 5 -- Table structure for employee 6 -- ---------------------------- 7 DROP TABLE IF EXISTS `employee`; 8 CREATE TABLE `employee` ( 9 `id` int(11) NOT NULL AUTO_INCREMENT, 10 `last_name` varchar(255) DEFAULT NULL, 11 `gender` char(1) DEFAULT NULL, 12 `email` varchar(255) DEFAULT NULL, 13 `dept_id` int(11) DEFAULT NULL COMMENT '部门ID', 14 PRIMARY KEY (`id`) 15 ) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8; 16 17 -- ---------------------------- 18 -- Records of employee 19 -- ---------------------------- 20 BEGIN; 21 INSERT INTO `employee` VALUES (1, '大白', '1', 'dabai@163.com', 1); 22 INSERT INTO `employee` VALUES (2, '小明', '1', 'xiaoming@163.com', 1); 23 INSERT INTO `employee` VALUES (3, '小红', '1', 'xiaohong@163.com', 1); 24 COMMIT;
2、新建一个SpringBoot Web项目
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-mybatis</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 <properties> 18 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 <dependency> 32 <groupId>org.mybatis.spring.boot</groupId> 33 <artifactId>mybatis-spring-boot-starter</artifactId> 34 <version>2.0.1</version> 35 </dependency> 36 37 <!-- mysql --> 38 <dependency> 39 <groupId>mysql</groupId> 40 <artifactId>mysql-connector-java</artifactId> 41 <version>8.0.12</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-test</artifactId> 47 <scope>test</scope> 48 </dependency> 49 50 </dependencies> 51 52 53 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 54 <build> 55 <plugins> 56 <plugin> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-maven-plugin</artifactId> 59 </plugin> 60 </plugins> 61 </build> 62 </project>