springboot笔记07——整合MyBatis
前言
Springboot 整合 MyBatis 有两种方式,分别是:“全注解版” 和 “注解、xml混合版”。
创建项目
创建Springboot项目,选择依赖
Maven依赖
<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
}
Dao
@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
@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
注解、xml混合版
项目结构
(比之前的多了一个映射文件)
配置文件
添加了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
}
Dao
/*
* 在主启动类使用了@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文件
<?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>
Service、控制类和之前的一样
可以在主启动类使用@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的两种方式就介绍到这里了。