3. SpringBoot整合mybatis
3.1 SpringBoot整合mybatis思路分析
回顾spring框架如何整合mybatis
springboot如何整合mybatis
3.2 SpringBoot整合mybatis编码
下面会用到的包结构:
- 引入依赖
在pom.xml中引入下面依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
另外,有可能在引入依赖时特别慢,我们需要在pom.xml中加入下面几段话使用阿里云镜像:
<repositories>
<repository>
<id>aliyun-repos</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
- 书写配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8
username: root
password: root
mybatis:
mapper-locations: classpath:com/baizhi/mapper/*.xml
type-aliases-package: com.baizhi.eneity
接下来要扫描dao接口创建对象
在扫描dao接口所在的包创建对象时有两种方法:
第一种是在入口类上加上@MapperScan(“com.baizhi.dao”)注解,这种方式可以一次向创建dao包中所有的DAO
第二种是直接在DAO接口上加上@Mapper注解,这种方式每次只能创建一个DAO
推荐使用第一种
第一种:入口类上加一个注解@MapperScan扫描dao接口所在的包
@SpringBootApplication
@MapperScan("com.baizhi.dao")
public class SpringbootDay3Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDay3Application.class, args);
}
}
第二种:在DAO接口上加上@Mapper注解
@Mapper
public interface UserDAO {
List<User> findAll();
void save(User user);
}
- 测试
需要用到的包结构
public class User {
private Integer id;
private String name;
private Date birthday;
private Double salary;
}
public interface UserDAO {
List<User> findAll();
void save(User user);
}
<?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.baizhi.dao.UserDAO">
<select id="findAll" resultType="com.baizhi.eneity.User">
select id, name, birthday, salary from user
</select>
<insert id="save" parameterType="com.baizhi.eneity.User" useGeneratedKeys="true" keyProperty="id">
insert into user values (#{id}, #{name}, #{birthday}, #{salary})
</insert>
</mapper>
public interface UserService {
public List<User> findAll();
public void save(User user);
}
@Service
@Transactional
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
@Autowired
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public List<User> findAll() {
return userDAO.findAll();
}
@Override
public void save(User user) {
userDAO.save(user);
}
}
@RestController
public class UserController {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping("save")
public void save(User user){
userService.save(user);
}
@RequestMapping("findAll")
public List<User> findAll(){
return userService.findAll();
}
}
开启SpringBoot应用测试一下:
|