springboot系列(八)springboot整合mybatis
本篇介绍一下在springboot中整合mybatis ,使用mysql数据库,集成durid 连接池技术,全部代码是手动生成,没有使用代码生成器来构建代码。
扫码入群,可获取一手资料和直播课程。
一、创建数据库和表
二、在pom中添加依赖
<!-- 由于maven官方仓库在国外,本地更新很慢,建议使用中国的阿里仓库,在pom中添加如下配置 -->
<repositories><!-- 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<!-- Spring Boot Mybatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- MySQL 连接驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <!-- Druid 数据连接池依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency>
三、添加数据源配置文件 application-dev.yml(dev对应的是开发环境的配置文件)
1、application-dev.yml
spring: datasource: url: jdbc:mysql://localhost:3306/springbootdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 username: root password: 187664 type: com.alibaba.druid.pool.DruidDataSource #连接池的配置信息 initialSize: 10 minIdle: 10 maxActive: 100 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20
四、添加mybatis的配置文件
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
五、在springboot核心配置文件中配置数据源和mybatis
application.yml
#数据源配置
spring:
profiles:
active: dev
# Mybatis配置
mybatis:
configLocation: classpath:mybatis.xml #mybatis配置文件路径
mapperLocations: classpath:mapper/**/*.xml #所有mapper映射文件地址
六、分别创建entity、dao、service、controller 、mapper等文件,先来看看文档结构
1、user实体类
package com.chuhouqi.demo.entity;
import java.io.Serializable;
public class User implements Serializable {
private String id;
private String name;
private String sex;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
'}';
}
}
2、dao接口
3、UserService接口
package com.chuhouqi.demo.service;
import com.chuhouqi.demo.entity.User;
public interface IUserService {
public User getUser();
}
4、UserService接口的实现类
package com.chuhouqi.demo.service.impl;
import com.chuhouqi.demo.dao.IUserDao;
import com.chuhouqi.demo.entity.User;
import com.chuhouqi.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public User getUser() {
return userDao.getUser();
}
}
5、UserController类
6、UserMapper映射文件
七、启动项目测试