SpringBoot整合MyBatisPlus
SpringBoot整合MyBatisPlus
1、引入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.vantee</groupId>
<artifactId>SpringBoot_MyBatis_MySQL</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、配置yml文件
# 配置数据库链接信息
spring:
datasource:
url: jdbc:mysql://101.200.84.8:3306/giao?useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: giao
password: SzGDjjsi8NpJrw5t
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 5
connection-test-query: SELECT 1 FROM DUAL
maximum-pool-size: 20
auto-commit: true
idle-timeout: 30000
pool-name: SpringBootDemoHikariCP
max-lifetime: 60000
connection-timeout: 30000
mybatis-plus:
configuration:
# 驼峰转下划线
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# mapper目录 这里需要注意,用classpath可能会出现
mapper-locations: classpath*:mapper/**/*Mapper.xml
# 别名
typeAliasesPackage: cn.vantee.**.entity
logging:
level:
root: info
cn.vantee: debug
3、实体类
package cn.vantee.core.entity;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* ;
* @author : http://www.chiner.pro
* @date : 2022-5-8
*/
@Data
public class Student implements Serializable,Cloneable{
/** */
private String sId ;
/** */
private String sName ;
/** */
private LocalDateTime sBirth ;
/** */
private String sSex ;
}
4、Mapper接口
package cn.vantee.core.mapper;
import cn.vantee.core.entity.Student;
import org.apache.ibatis.annotations.Mapper;
/**
* ;(STUDENT)表数据库访问层
* @author : http://www.chiner.pro
* @date : 2022-5-8
*/
@Mapper
public interface StudentMapper {
Student findStudentById(String id);
}
5、Mapper.xml
这里需要注意:MySQL的表名称区分大小写
<?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="cn.vantee.core.mapper.StudentMapper">
<select id="findStudentById" resultType="cn.vantee.core.entity.Student">
select * from Student where s_id = #{id}
</select>
</mapper>
6、测试类
package cn.vantee.test;
import cn.vantee.core.entity.Student;
import cn.vantee.core.mapper.StudentMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @author :rayfoo@qq.com
* @date :Created in 2022/5/8 15:41
* @description:测试类
* @modified By:
* @version: 1.0.0
*/
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class ApplicationTest {
@Resource
public StudentMapper studentMapper;
@Test
public void test() {
Student studentById = studentMapper.findStudentById("01");
log.info(studentById.toString());
}
}