springboot集成Mybatis

springboot集成Mybatis官方文档

添加依赖并配置相关参数

添加依赖

在maven中添加如下依赖,

1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

添加该依赖前确保mysql-connector-java的依赖已经添加

mysql-connector-java依赖

1
2
3
4
5
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>

设置相关参数

我的xml映射文件设置在classpath:mapper文件夹下

1
2
#指定映射文件
mybatis.mapperLocations=classpath:mapper/*.xml

将mapper注册为bean

MyBatis-Spring-Boot-Starter将:
自动检测现有的DataSource。
将创建并注册一个SqlSessionFactory实例,使用SqlSessionFactoryBean将该DataSource作为输入参数。
从SqlSessionFactory中创建并注册SqlSessionTemplate的实例。
自动扫描映射器,将它们链接到SqlSessionTemplate并将它们注册到Spring上下文,以便将它们注入到bean中。

使用@Mapper注解

在对应的接口Mapper中使用@Mapper注解可将Mapper注册为Bean,缺点是需要在每一个接口Mapper中添加@Mapper注解,有点麻烦

1
2
3
4
5
6
7
@Mapper
public interface CityMapper {

@Select("SELECT * FROM CITY WHERE state = #{state}")
City findByState(@Param("state") String state);

}

使用@MapperScan注解

如果不想在每个接口Mapper中都添加@Mapper注解,可使用@MapperScan注解,扫描某个包下的Mapper
可在springboot启动类或者配置类中使用该注解

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@MapperScan("com.dxysun.contentsales.dao")
public class ContentsalesApplication {

public static void main(String[] args) {
SpringApplication.run(ContentsalesApplication.class, args);
}

}

使用SqlSession

如果想使用SqlSession,可按照如下配置使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
public class CityDao {

private final SqlSession sqlSession;

public CityDao(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}

public City selectCityById(long id) {
return this.sqlSession.selectOne("selectCityById", id);
}

}

编写相应的Mapper的xml

classpath:mapper文件夹下创建对应的Mapper文件
例如我在包com.dxysun.contentsales.dao创建UserDao的接口类,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.dxysun.contentsales.dao;

import com.dxysun.contentsales.meta.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

public interface UserDao {

public List<User> getAllUsers();
public User getUserByUserame(String username);
}

在mapper文件夹下创建相应的userMapper.xml文件,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://
mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dxysun.contentsales.dao.UserDao">
<select id="getAllUsers" resultType="com.dxysun.contentsales.meta.User">
select * from user
</select>

<select id="getUserByUserame" resultType="com.dxysun.contentsales.meta.User">
select * from user where username=#{username}
</select>

</mapper>

注意namespace要写UserDao的完整路径,select中的id要与UserDao要和UserDao中的方法名一致

在springboot中使用

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
33
34
package com.dxysun.contentsales.web.controller;

import com.dxysun.contentsales.dao.CartDao;
import com.dxysun.contentsales.dao.UserDao;
import com.dxysun.contentsales.meta.Cart;
import com.dxysun.contentsales.meta.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
public class MainController {

@Autowired
private UserDao userDao;

@Autowired
private CartDao cartDao;

@RequestMapping("/index")
public String index(){
List<User> userList = userDao.getAllUsers();
for (User user : userList){
System.out.println(user.getUsername() + " " + user.getId());
}
User user = userDao.getUserByUserame("buyer");
System.out.println(user.getPassword());
return "index";
}

}

参考

Mapper文件的详细配置参考官方文档

打赏

请我喝杯咖啡吧~

支付宝
微信