MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。Spring Boot是能支持快速创建Spring应用的Java框架。本文通过一个例子来学习Spring Boot如何集成MyBatis,而且过程中不需要XML配置。
创建数据库
本文的例子使用Mysql数据库,首先创建一个用户表,执行sql语句如下:
CREATE TABLE IF NOT EXISTS user (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT NULL ,
`age` INT(2) NOT NULL ,
PRIMARY KEY (id)
)
- 1
- 2
- 3
- 4
- 5
- 6
工程目录结构与依赖配置
首先新建一个Maven工程,并配置Pom依赖,本例中所用到的依赖如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
然后创建一下工程目录结构,如下图所示:
代码文件内容
0. 创建配置文件——application.properties
写入一下内容:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
- 1
- 2
- 3
- 4
1. 创建POJO——entity/User.java
这是一个POJO,包含了id, name, age三个属性,代码如下:
package com.xyz.dbtest.entity; public class User { private int id; private String name; private int age;
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2. 创建一个数据层接口——service/UserService.java
这是一个Mapper类,代码如下:
package com.xyz.dbtest.dao;
import com.xyz.dbtest.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper //1
public interface UserDao {
@Results({
//2
@Result(property = “id”, column = “id”), //2
@Result(property = “name”, column = “name”),
@Result(property = “age”, column = “age”)
})
@Select(“SELECT * FROM user WHERE age = #{age}”) //3
List<User> get(int age);
<span class="token annotation punctuation">@Insert</span><span class="token punctuation">(</span><span class="token string">"INSERT INTO user(name, age) VALUES (#{name}, #{age})"</span><span class="token punctuation">)</span> <span class="token comment">//3</span>
<span class="token keyword">void</span> <span class="token function">insert</span><span class="token punctuation">(</span>User user<span class="token punctuation">)</span><span class="token punctuation">;</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
//1 @Mapper将UserDao声明为一个Mapper接口
//2 @Results是结果映射列表,@Result中property是User类的属性名,colomn是数据库表的字段名
//3 @Select, @Insert 分别代表了执行的真实SQL
3. 创建一个用户服务——service/UserService.java
这是一个服务类Bean,提供三个函数功能,代码如下:
package com.xyz.dbtest.service;
import com.xyz.dbtest.dao.UserDao;
import com.xyz.dbtest.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service //声明成一个spring bean
public class UserService {
<span class="token annotation punctuation">@Autowired</span> <span class="token comment">//连接到UserDao Bean</span>
<span class="token keyword">private</span> UserDao userDao<span class="token punctuation">;</span>
<span class="token keyword">public</span> String <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span> <span class="token string">"Hello World!"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> List<span class="token generics function"><span class="token punctuation"><</span>User<span class="token punctuation">></span></span> <span class="token function">showDao</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span> userDao<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> String <span class="token function">insert</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//插入一条记录</span>
User user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
user<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
user<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
userDao<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token string">"Insert ( \""</span><span class="token operator">+</span>name<span class="token operator">+</span><span class="token string">"\", age"</span><span class="token operator">+</span>age<span class="token operator">+</span><span class="token string">") OK!"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
4. 常见一个Web Controller——controller/UserController.java
这是一个Spring Web的Controller类,引入了spring-boot-starter-web依赖,代码如下:
package com.xyz.dbtest.controller;
import com.xyz.dbtest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController //声明为一个Restful的Controller
public class UserController {
@Autowired //自动连接到UserService Bean
private UserService userService;
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/show"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span> userService<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/showDao"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> Object <span class="token function">showDao</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span> userService<span class="token punctuation">.</span><span class="token function">showDao</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span>value<span class="token operator">=</span><span class="token string">"/insert"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">insert</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span> userService<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
5. 创建启动类——main/StartApp.java
这是一个spring boot启动类。代码如下:
package com.xyz.dbtest.main;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = “com.xyz.dbtest”) //1
@MapperScan(basePackages = “com.xyz.dbtest.dao”) //2
public class StartApp {
public static void main(String[] args) {
SpringApplication.run(StartApp.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
//1 由于StartApp类位于基础包的自包中,因此需要设置scanBasePackage
//2 设置Mapper接口所在的包
##运行结果
- 运行Sql语句创建数据库表后,运行StartApp类。启动成功如下图所示
- 测试show服务,结果如下:
- 测试showDao服务,在输入URL时需要将参数打包进url,结果如下:
不带参数时,访问错误:
带参数时,访问成功,由于数据库中没有记录,所以结果是一个空列表:
- 测试insert服务
- 再次测试showDao服务
结语
通过本文的例子可以看出,使用Spring boot集成MyBatis几乎不用任何配置工作,能有效加快开发效率!
代码库地址:github地址
作者:许玉壮
2017年1月6日