一.配置引入
添加以下依赖到pom.xml
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<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mybatis spring整合jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>在
resources
目录下薪增mybatis-config.xml
文件,内容如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://10.21.100.77:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/kaychen/springboot01/dao/UserMapper.xml"/>
</mappers>
</configuration>为了能正常引入mappers文件, 需要在
pom.xml
文件的<build>
模块中增加1
2
3
4
5
6
7
8
9
10<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>在
com/kaychen/springboot01/
目录下创建dao
,pojo
,controller
,service
在
utils
工具类包中创建MybatisUtils.java
工具类文件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
27package com.kaychen.springboot01.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}在
pojo
目录中新增User.java
数据库实体类文件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
28package com.kaychen.springboot01.pojo;
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(int id,String username, String password){
this.id = id;
this.username = username;
this.password = password;
}
public int getId(){
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}在
dao
目录中新增userDao.java
接口类文件1
2
3
4
5
6
7
8
9
10package com.kaychen.springboot01.dao;
import com.kaychen.springboot01.pojo.User;
import java.util.List;
public interface UserDao {
List<User> listUser();
}在
dao
目录中新增UserMapper.xml
文件1
2
3
4
5
6
7
8
9
10
<mapper namespace="com.kaychen.springboot01.dao.UserDao">
<select id="listUser" resultType="com.kaychen.springboot01.pojo.User">
select * from mybatis.user
</select>
</mapper>
二.junit测试
1.在com/kaychen/springboot01
目录下创建dao
目录以及UserDaoTest.java
文件
1 | package com.kaychen.springboot01.dao; |
三. 整合springboot
在
service
中新增UserService.java
类文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.kaychen.springboot01.service;
import com.kaychen.springboot01.dao.UserDao;
import com.kaychen.springboot01.pojo.User;
import com.kaychen.springboot01.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class UserService {
public List<User> listUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userdao = sqlSession.getMapper(UserDao.class);
List<User> data = userdao.listUser();
return data;
}
}在
controller
目录中新增UserController.java
类文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package com.kaychen.springboot01.controller;
import com.kaychen.springboot01.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
public class UserController {
public Object listUser(){
UserService userService = new UserService();
return userService.listUser();
}
}请求
localhost/api/hello
得到返回数据1
2
3
4
5
6
7[
{
"id": 1,
"username": "king",
"password": "Zhengkai2021"
}
]