注明:我将通过一个Demo来阐述MyBatis-Plus的强大
我的环境
上面是我的环境版本。
源码下载地址
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
数据库准备完成后我们来通过springboot创建初始化一个工程。初始化工程的步骤我就不阐述了,有不知道的小伙伴自行baidu
4.0.0
com.lqf
springboot-mybatis-plus
0.0.1-SNAPSHOT
jar
springboot-mybatis-plus
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.0.3
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-maven-plugin
引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。
如果springboot版本是SNAPSHOT快照版还需要添加仓库
snapshots
https://oss.sonatype.org/content/repositories/snapshots/
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://******:3306/crm
username: ****
password: ****
数据库连接换成自己的就可以
package com.lqf.springbootmybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.lqf.springbootmybatisplus.dao.mapper")
public class SpringbootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
}
}
@MapperScan()内容填写你自己的mapper位置
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
@Data注解会给此类默认提供Getter,Setter,equals,hashCode,toString方法
public interface UserMapper extends BaseMapper {
}
package com.lqf.springbootmybatisplus;
import com.lqf.springbootmybatisplus.dao.mapper.UserMapper;
import com.lqf.springbootmybatisplus.domain.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
----- selectAll method test ------
User(id=1, name=Jone, age=18, [email protected])
User(id=2, name=Jack, age=20, [email protected])
User(id=3, name=Tom, age=28, [email protected])
User(id=4, name=Sandy, age=21, [email protected])
User(id=5, name=Billie, age=24, [email protected])
到这里一个简单的springboot+mybatis-plus就整合完成了,你会发现没有写mapper.xml,mapper.java中什么也没有。对的你没看错,这只是mybatis-plus的冰山一角,让我们看他后续的强大功能吧
上一章 : (一)mybatis-plus详细介绍
下一章 : (三)springboot + mybatis plus集成AutoGenerator快速搭建项目