Springboot 整合 MyBatis 有两种方式,分别是:“全注解版” 和 “注解、xml混合版”。
创建Springboot项目,选择依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
#配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-test?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
public class User {
private int id;
private String name;
//getter、setter、toString
}
@Mapper
public interface UserDao {
@Select("select * from user where id = #{id}")
public User getUserById(@Param("id") int id);
@Select("select * from user")
public List<User> getAllUser();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUserById(int id) {
return this.userDao.getUserById(id);
}
@Override
public List<User> getAllUser() {
return this.userDao.getAllUser();
}
}
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServiceImpl userService;
@RequestMapping("/getUserById/{id}")
public User getUserById(@PathVariable("id")int id) {
return this.userService.getUserById(id);
}
}
http://localhost:8080/user/getUserById/1
(比之前的多了一个映射文件)
添加了mybatis的配置,包括Mapper文件的位置、alias的配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.jotal.springboot05mybatis.entities
public class User {
private int id;
private String name;
//getter、setter、toString
}
/*
* 在主启动类使用了@MapperScan就不用每一个添加@Mapper了
* */
public interface UserDao {
// @Select("select * from user where id = #{id}")
// 若使用全注解需要添加@Param("id")到形式参数之前
User getUserById(int id);
// @Select("select * from user")
List<User> getAllUser();
}
<?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.jotal.springboot05mybatis.dao.UserDao">
<select id="getUserById" parameterType="Integer" resultType="user">
select * from user where id = #{id}
</select>
<select id="getAllUser" resultType="user">
select * from user
</select>
</mapper>
可以在主启动类使用@MapperScan注解扫描Mapper类,就不需要在每个Mapper类添加@Mapper
@MapperScan("com.jotal.springboot05mybatis.dao")
@SpringBootApplication
public class Springboot05MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot05MybatisApplication.class, args);
}
}
到此,Springboot整合Mybatis的两种方式就介绍到这里了。
原文:https://www.cnblogs.com/Jotal/p/11311563.html