一、SpringBoot整合MyBatis注解版
1.加入依赖
父工程依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
子工程依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--mybatis自动配置包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/briup?useSSL=false&useUnicode=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
创建DataSourceConfig配置类
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource")
public DruidDataSource dataSource(){
return new DruidDataSource();
}
}
实体类
@Data
public class Student {
private Integer sno;
private String sname;
private Integer sage;
private String gender;
}
创建mybatisConfig配置类
@Configuration
@MapperScan("com.gsau.mapper")//扫描指定包下所有接口
public class MyBatisConfig {
//数据库字段下划线与对象属性驼峰模式匹配
@Bean
public ConfigurationCustomizer customizer(){
return new ConfigurationCustomizer() {
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
mapper层
public interface StudentMapper {
@Select("select * from student where sno=#{sno}")
Student selectBySno(Integer sno);
@Select("select * from student")
List<Student> selectAll();
@Options(useGeneratedKeys = true,keyProperty = "sno")
@Insert("insert into student values(#{sno},#{sname},#{sage},#{gender})")
void insert(Student student);
@Delete("delete from student where sno=#{sno}")
void deleteBySno(Integer sno);
}
controller层
@RestController
public class StudentController {
@Resource
private StudentMapper studentMapper;
@RequestMapping("/selectBySno")
public Student selectBySno(){
Student student = studentMapper.selectBySno(1073);
return student;
}
@RequestMapping("/selectAll")
public List<Student> selectAll(){
List<Student> students = studentMapper.selectAll();
return students;
}
}
启动类
@SpringBootApplication
public class MyBatisSpringApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisSpringApplication.class,args);
}
}
二、SpringBoot整合MyBatis配置文件版
配置文件
#数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/briup?useSSL=false&useUnicode=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true #字段匹配
type-aliases-package: com.gsau.model #别名
创建mybatisConfig配置类
@Configuration
@MapperScan("com.gsau.mapper")//扫描指定包下所有接口
public class MyBatisConfig {
}
mapper层
public interface StudentMapper {
Student selectBySno(Integer sno);
List<Student> selectAll();
void insert(Student student);
void deleteBySno(Integer sno);
}
mapper映射文件
<?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.gsau.mapper.StudentMapper">
<select id="selectBySno" resultType="student">
select * from student where sno=#{sno}
</select>
<select id="selectAll" resultType="student">
select * from student
</select>
<insert id="insert" useGeneratedKeys="true" keyProperty="sno">
insert into student values(#{sno},#{sname},#{sage},#{gender})
</insert>
<delete id="deleteBySno">
delete from student where sno=#{sno}
</delete>
</mapper>