MyBatisPlus整合spring boot
MyBatisPlus整合spring boot
准备数据库表
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
新建一个springboot工程,导包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!--springboot自带MySQL驱动为8.0版本之上的-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
注意:引入 MyBatis-Plus之后请不要再次引入MyBatis以及MyBatis-Spring,以避免因版本差异导致的问题
在application.yml配置文件中添加数据库的相关配置:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis-plus:
configuration:
# 开启日志打印sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
在 Spring Boot 启动类中添加@MapperScan注解,扫描 Mapper 文件夹
package com.yl.mybatis.plus.guigu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.yl.mybatis.plus.guigu")
public class MybatisPlusGuiguApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MybatisPlusGuiguApplication.class);
springApplication.run(args);
}
}
实体类
package com.yl.mybatis.plus.guigu.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 用户
*
* @author Y-wee
*/
@Getter
@Setter
@ToString
public class User {
/**
* id
*/
private Long id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
}
Mapper
package com.yl.mybatis.plus.guigu.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yl.mybatis.plus.guigu.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* 用户
*
* @author Y-wee
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
测试类
package com.yl.mybatis.plus.guigu.mapper;
import com.yl.mybatis.plus.guigu.entity.User;
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;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
@Resource
private UserMapper mapper;
@Test
public void testMybatisPlus(){
// 通过条件构造器查询List,如果条件为空则参数传入null即可
List<User> userList = mapper.selectList(null);
userList.forEach(System.out::println);
}
}
至此,mybatis plus整合springboot完成
记得快乐