@@ -24,6 +24,7 @@ | |||||
<module>spring-boot-demo-orm-jpa</module> | <module>spring-boot-demo-orm-jpa</module> | ||||
<module>spring-boot-demo-orm-mybatis</module> | <module>spring-boot-demo-orm-mybatis</module> | ||||
<module>spring-boot-demo-orm-mybatis-mapper-page</module> | <module>spring-boot-demo-orm-mybatis-mapper-page</module> | ||||
<module>spring-boot-demo-orm-mybatis-plus</module> | |||||
<module>spring-boot-demo-email</module> | <module>spring-boot-demo-email</module> | ||||
<module>spring-boot-demo-upload</module> | <module>spring-boot-demo-upload</module> | ||||
<module>spring-boot-demo-war</module> | <module>spring-boot-demo-war</module> | ||||
@@ -0,0 +1,25 @@ | |||||
/target/ | |||||
!.mvn/wrapper/maven-wrapper.jar | |||||
### STS ### | |||||
.apt_generated | |||||
.classpath | |||||
.factorypath | |||||
.project | |||||
.settings | |||||
.springBeans | |||||
.sts4-cache | |||||
### IntelliJ IDEA ### | |||||
.idea | |||||
*.iws | |||||
*.iml | |||||
*.ipr | |||||
### NetBeans ### | |||||
/nbproject/private/ | |||||
/build/ | |||||
/nbbuild/ | |||||
/dist/ | |||||
/nbdist/ | |||||
/.nb-gradle/ |
@@ -0,0 +1,426 @@ | |||||
# spring-boot-demo-orm-mybatis-plus | |||||
> 此 demo 演示了 Spring Boot 如何集成 mybatis-plus,简化Mybatis开发,带给你难以置信的开发体验。 | |||||
## pom.xml | |||||
```xml | |||||
<?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> | |||||
<artifactId>spring-boot-demo-orm-mybatis-plus</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-orm-mybatis-plus</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
<mybatis.plus.version>3.0.5</mybatis.plus.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.baomidou</groupId> | |||||
<artifactId>mybatis-plus-boot-starter</artifactId> | |||||
<version>${mybatis.plus.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>mysql</groupId> | |||||
<artifactId>mysql-connector-java</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.guava</groupId> | |||||
<artifactId>guava</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-orm-mybatis-plus</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> | |||||
``` | |||||
## MybatisPlusConfig.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* mybatis-plus 配置 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.config | |||||
* @description: mybatis-plus 配置 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 17:29 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.plus.mapper"}) | |||||
@EnableTransactionManagement | |||||
public class MybatisPlusConfig { | |||||
/** | |||||
* 性能分析拦截器,不建议生产使用 | |||||
*/ | |||||
@Bean | |||||
public PerformanceInterceptor performanceInterceptor(){ | |||||
return new PerformanceInterceptor(); | |||||
} | |||||
/** | |||||
* 分页插件 | |||||
*/ | |||||
@Bean | |||||
public PaginationInterceptor paginationInterceptor() { | |||||
return new PaginationInterceptor(); | |||||
} | |||||
} | |||||
``` | |||||
## CommonFieldHandler.java | |||||
```java | |||||
package com.xkcoding.orm.mybatis.plus.config; | |||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.apache.ibatis.reflection.MetaObject; | |||||
import org.springframework.stereotype.Component; | |||||
import java.util.Date; | |||||
/** | |||||
* <p> | |||||
* 通用字段填充 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.config | |||||
* @description: 通用字段填充 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 17:40 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Slf4j | |||||
@Component | |||||
public class CommonFieldHandler implements MetaObjectHandler { | |||||
@Override | |||||
public void insertFill(MetaObject metaObject) { | |||||
log.info("start insert fill ...."); | |||||
this.setFieldValByName("createTime", new Date(), metaObject); | |||||
this.setFieldValByName("lastUpdateTime", new Date(), metaObject); | |||||
} | |||||
@Override | |||||
public void updateFill(MetaObject metaObject) { | |||||
log.info("start update fill ...."); | |||||
this.setFieldValByName("lastUpdateTime", new Date(), metaObject); | |||||
} | |||||
} | |||||
``` | |||||
## application.yml | |||||
```yaml | |||||
spring: | |||||
datasource: | |||||
url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 | |||||
username: root | |||||
password: root | |||||
driver-class-name: com.mysql.cj.jdbc.Driver | |||||
type: com.zaxxer.hikari.HikariDataSource | |||||
initialization-mode: always | |||||
continue-on-error: true | |||||
schema: | |||||
- "classpath:db/schema.sql" | |||||
data: | |||||
- "classpath:db/data.sql" | |||||
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 | |||||
logging: | |||||
level: | |||||
com.xkcoding: debug | |||||
com.xkcoding.orm.mybatis.plus.mapper: trace | |||||
mybatis-plus: | |||||
mapper-locations: classpath:mappers/*.xml | |||||
#实体扫描,多个package用逗号或者分号分隔 | |||||
typeAliasesPackage: com.xkcoding.orm.mybatis.plus.entity | |||||
global-config: | |||||
# 数据库相关配置 | |||||
db-config: | |||||
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; | |||||
id-type: auto | |||||
#字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断" | |||||
field-strategy: not_empty | |||||
#驼峰下划线转换 | |||||
table-underline: true | |||||
#是否开启大写命名,默认不开启 | |||||
#capital-mode: true | |||||
#逻辑删除配置 | |||||
#logic-delete-value: 1 | |||||
#logic-not-delete-value: 0 | |||||
db-type: mysql | |||||
#刷新mapper 调试神器 | |||||
refresh: true | |||||
# 原生配置 | |||||
configuration: | |||||
map-underscore-to-camel-case: true | |||||
cache-enabled: true | |||||
``` | |||||
## UserMapper.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* UserMapper | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.mapper | |||||
* @description: UserMapper | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 16:57 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Component | |||||
public interface UserMapper extends BaseMapper<User> { | |||||
} | |||||
``` | |||||
## UserService.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* User Service | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.service | |||||
* @description: User Service | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 18:10 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public interface UserService extends IService<User> { | |||||
} | |||||
``` | |||||
## UserServiceImpl.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* User Service | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.service.impl | |||||
* @description: User Service | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 18:10 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Service | |||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { | |||||
} | |||||
``` | |||||
## UserServiceImplTest.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* User Service 测试 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.service.impl | |||||
* @description: User Service 测试 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 18:13 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Slf4j | |||||
public class UserServiceImplTest extends SpringBootDemoOrmMybatisPlusApplicationTests { | |||||
@Autowired | |||||
private UserService userService; | |||||
/** | |||||
* 测试Mybatis-Plus 新增 | |||||
*/ | |||||
@Test | |||||
public void testSave() { | |||||
String salt = IdUtil.fastSimpleUUID(); | |||||
User testSave3 = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave3@xkcoding.com").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).build(); | |||||
boolean save = userService.save(testSave3); | |||||
Assert.assertTrue(save); | |||||
log.debug("【测试id回显#testSave3.getId()】= {}", testSave3.getId()); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 批量新增 | |||||
*/ | |||||
@Test | |||||
public void testSaveList() { | |||||
List<User> userList = Lists.newArrayList(); | |||||
for (int i = 4; i < 14; i++) { | |||||
String salt = IdUtil.fastSimpleUUID(); | |||||
User user = User.builder().name("testSave" + i).password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave" + i + "@xkcoding.com").phoneNumber("1730000000" + i).status(1).lastLoginTime(new DateTime()).build(); | |||||
userList.add(user); | |||||
} | |||||
boolean batch = userService.saveBatch(userList); | |||||
Assert.assertTrue(batch); | |||||
List<Long> ids = userList.stream().map(User::getId).collect(Collectors.toList()); | |||||
log.debug("【userList#ids】= {}", ids); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 删除 | |||||
*/ | |||||
@Test | |||||
public void testDelete() { | |||||
boolean remove = userService.removeById(1L); | |||||
Assert.assertTrue(remove); | |||||
User byId = userService.getById(1L); | |||||
Assert.assertNull(byId); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 修改 | |||||
*/ | |||||
@Test | |||||
public void testUpdate() { | |||||
User user = userService.getById(1L); | |||||
Assert.assertNotNull(user); | |||||
user.setName("MybatisPlus修改名字"); | |||||
boolean b = userService.updateById(user); | |||||
Assert.assertTrue(b); | |||||
User update = userService.getById(1L); | |||||
Assert.assertEquals("MybatisPlus修改名字", update.getName()); | |||||
log.debug("【update】= {}", update); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 查询单个 | |||||
*/ | |||||
@Test | |||||
public void testQueryOne() { | |||||
User user = userService.getById(1L); | |||||
Assert.assertNotNull(user); | |||||
log.debug("【user】= {}", user); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 查询全部 | |||||
*/ | |||||
@Test | |||||
public void testQueryAll() { | |||||
List<User> list = userService.list(new QueryWrapper<>()); | |||||
Assert.assertTrue(CollUtil.isNotEmpty(list)); | |||||
log.debug("【list】= {}", list); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 分页排序查询 | |||||
*/ | |||||
@Test | |||||
public void testQueryByPageAndSort() { | |||||
initData(); | |||||
int count = userService.count(new QueryWrapper<>()); | |||||
Page<User> userPage = new Page<>(1, 5); | |||||
userPage.setDesc("id"); | |||||
IPage<User> page = userService.page(userPage, new QueryWrapper<>()); | |||||
Assert.assertEquals(5, page.getSize()); | |||||
Assert.assertEquals(count, page.getTotal()); | |||||
log.debug("【page.getRecords()】= {}", page.getRecords()); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 自定义查询 | |||||
*/ | |||||
@Test | |||||
public void testQueryByCondition() { | |||||
initData(); | |||||
QueryWrapper<User> wrapper = new QueryWrapper<>(); | |||||
wrapper.like("name", "Save1").or().eq("phone_number", "17300000001").orderByDesc("id"); | |||||
int count = userService.count(wrapper); | |||||
Page<User> userPage = new Page<>(1, 3); | |||||
IPage<User> page = userService.page(userPage, wrapper); | |||||
Assert.assertEquals(3, page.getSize()); | |||||
Assert.assertEquals(count, page.getTotal()); | |||||
log.debug("【page.getRecords()】= {}", page.getRecords()); | |||||
} | |||||
/** | |||||
* 初始化数据 | |||||
*/ | |||||
private void initData() { | |||||
testSaveList(); | |||||
} | |||||
} | |||||
``` | |||||
## 参考 | |||||
- mybatis-plus官方文档:http://mp.baomidou.com/ | |||||
@@ -0,0 +1,76 @@ | |||||
<?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> | |||||
<artifactId>spring-boot-demo-orm-mybatis-plus</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-orm-mybatis-plus</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
<mybatis.plus.version>3.0.5</mybatis.plus.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.baomidou</groupId> | |||||
<artifactId>mybatis-plus-boot-starter</artifactId> | |||||
<version>${mybatis.plus.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>mysql</groupId> | |||||
<artifactId>mysql-connector-java</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.guava</groupId> | |||||
<artifactId>guava</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-orm-mybatis-plus</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,26 @@ | |||||
package com.xkcoding.orm.mybatis.plus; | |||||
import org.mybatis.spring.annotation.MapperScan; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
/** | |||||
* <p> | |||||
* 启动器 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus | |||||
* @description: 启动器 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 16:48 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@SpringBootApplication | |||||
public class SpringBootDemoOrmMybatisPlusApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoOrmMybatisPlusApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,39 @@ | |||||
package com.xkcoding.orm.mybatis.plus.config; | |||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.apache.ibatis.reflection.MetaObject; | |||||
import org.springframework.stereotype.Component; | |||||
import java.util.Date; | |||||
/** | |||||
* <p> | |||||
* 通用字段填充 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.config | |||||
* @description: 通用字段填充 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 17:40 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Slf4j | |||||
@Component | |||||
public class CommonFieldHandler implements MetaObjectHandler { | |||||
@Override | |||||
public void insertFill(MetaObject metaObject) { | |||||
log.info("start insert fill ...."); | |||||
this.setFieldValByName("createTime", new Date(), metaObject); | |||||
this.setFieldValByName("lastUpdateTime", new Date(), metaObject); | |||||
} | |||||
@Override | |||||
public void updateFill(MetaObject metaObject) { | |||||
log.info("start update fill ...."); | |||||
this.setFieldValByName("lastUpdateTime", new Date(), metaObject); | |||||
} | |||||
} |
@@ -0,0 +1,42 @@ | |||||
package com.xkcoding.orm.mybatis.plus.config; | |||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; | |||||
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor; | |||||
import org.mybatis.spring.annotation.MapperScan; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.Configuration; | |||||
import org.springframework.transaction.annotation.EnableTransactionManagement; | |||||
/** | |||||
* <p> | |||||
* mybatis-plus 配置 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.config | |||||
* @description: mybatis-plus 配置 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 17:29 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.plus.mapper"}) | |||||
@EnableTransactionManagement | |||||
public class MybatisPlusConfig { | |||||
/** | |||||
* 性能分析拦截器,不建议生产使用 | |||||
*/ | |||||
@Bean | |||||
public PerformanceInterceptor performanceInterceptor(){ | |||||
return new PerformanceInterceptor(); | |||||
} | |||||
/** | |||||
* 分页插件 | |||||
*/ | |||||
@Bean | |||||
public PaginationInterceptor paginationInterceptor() { | |||||
return new PaginationInterceptor(); | |||||
} | |||||
} |
@@ -0,0 +1,88 @@ | |||||
package com.xkcoding.orm.mybatis.plus.entity; | |||||
import com.baomidou.mybatisplus.annotation.TableField; | |||||
import com.baomidou.mybatisplus.annotation.TableName; | |||||
import lombok.AllArgsConstructor; | |||||
import lombok.Builder; | |||||
import lombok.Data; | |||||
import lombok.NoArgsConstructor; | |||||
import java.io.Serializable; | |||||
import java.util.Date; | |||||
import static com.baomidou.mybatisplus.annotation.FieldFill.INSERT; | |||||
import static com.baomidou.mybatisplus.annotation.FieldFill.INSERT_UPDATE; | |||||
/** | |||||
* <p> | |||||
* 用户实体类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.entity | |||||
* @description: 用户实体类 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 16:49 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
@Builder | |||||
@TableName("orm_user") | |||||
public class User implements Serializable { | |||||
private static final long serialVersionUID = -1840831686851699943L; | |||||
/** | |||||
* 主键 | |||||
*/ | |||||
private Long id; | |||||
/** | |||||
* 用户名 | |||||
*/ | |||||
private String name; | |||||
/** | |||||
* 加密后的密码 | |||||
*/ | |||||
private String password; | |||||
/** | |||||
* 加密使用的盐 | |||||
*/ | |||||
private String salt; | |||||
/** | |||||
* 邮箱 | |||||
*/ | |||||
private String email; | |||||
/** | |||||
* 手机号码 | |||||
*/ | |||||
private String phoneNumber; | |||||
/** | |||||
* 状态,-1:逻辑删除,0:禁用,1:启用 | |||||
*/ | |||||
private Integer status; | |||||
/** | |||||
* 创建时间 | |||||
*/ | |||||
@TableField(fill = INSERT) | |||||
private Date createTime; | |||||
/** | |||||
* 上次登录时间 | |||||
*/ | |||||
private Date lastLoginTime; | |||||
/** | |||||
* 上次更新时间 | |||||
*/ | |||||
@TableField(fill = INSERT_UPDATE) | |||||
private Date lastUpdateTime; | |||||
} |
@@ -0,0 +1,22 @@ | |||||
package com.xkcoding.orm.mybatis.plus.mapper; | |||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||||
import com.xkcoding.orm.mybatis.plus.entity.User; | |||||
import org.springframework.stereotype.Component; | |||||
/** | |||||
* <p> | |||||
* UserMapper | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.mapper | |||||
* @description: UserMapper | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 16:57 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Component | |||||
public interface UserMapper extends BaseMapper<User> { | |||||
} |
@@ -0,0 +1,20 @@ | |||||
package com.xkcoding.orm.mybatis.plus.service; | |||||
import com.baomidou.mybatisplus.extension.service.IService; | |||||
import com.xkcoding.orm.mybatis.plus.entity.User; | |||||
/** | |||||
* <p> | |||||
* User Service | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.service | |||||
* @description: User Service | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 18:10 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public interface UserService extends IService<User> { | |||||
} |
@@ -0,0 +1,24 @@ | |||||
package com.xkcoding.orm.mybatis.plus.service.impl; | |||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||||
import com.xkcoding.orm.mybatis.plus.entity.User; | |||||
import com.xkcoding.orm.mybatis.plus.mapper.UserMapper; | |||||
import com.xkcoding.orm.mybatis.plus.service.UserService; | |||||
import org.springframework.stereotype.Service; | |||||
/** | |||||
* <p> | |||||
* User Service | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.service.impl | |||||
* @description: User Service | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 18:10 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Service | |||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { | |||||
} |
@@ -0,0 +1,51 @@ | |||||
spring: | |||||
datasource: | |||||
url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 | |||||
username: root | |||||
password: root | |||||
driver-class-name: com.mysql.cj.jdbc.Driver | |||||
type: com.zaxxer.hikari.HikariDataSource | |||||
initialization-mode: always | |||||
continue-on-error: true | |||||
schema: | |||||
- "classpath:db/schema.sql" | |||||
data: | |||||
- "classpath:db/data.sql" | |||||
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 | |||||
logging: | |||||
level: | |||||
com.xkcoding: debug | |||||
com.xkcoding.orm.mybatis.plus.mapper: trace | |||||
mybatis-plus: | |||||
mapper-locations: classpath:mappers/*.xml | |||||
#实体扫描,多个package用逗号或者分号分隔 | |||||
typeAliasesPackage: com.xkcoding.orm.mybatis.plus.entity | |||||
global-config: | |||||
# 数据库相关配置 | |||||
db-config: | |||||
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; | |||||
id-type: auto | |||||
#字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断" | |||||
field-strategy: not_empty | |||||
#驼峰下划线转换 | |||||
table-underline: true | |||||
#是否开启大写命名,默认不开启 | |||||
#capital-mode: true | |||||
#逻辑删除配置 | |||||
#logic-delete-value: 1 | |||||
#logic-not-delete-value: 0 | |||||
db-type: mysql | |||||
#刷新mapper 调试神器 | |||||
refresh: true | |||||
# 原生配置 | |||||
configuration: | |||||
map-underscore-to-camel-case: true | |||||
cache-enabled: true |
@@ -0,0 +1,2 @@ | |||||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001'); | |||||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); |
@@ -0,0 +1,13 @@ | |||||
DROP TABLE IF EXISTS `orm_user`; | |||||
CREATE TABLE `orm_user` ( | |||||
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', | |||||
`name` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名', | |||||
`password` VARCHAR(32) NOT NULL COMMENT '加密后的密码', | |||||
`salt` VARCHAR(32) NOT NULL COMMENT '加密使用的盐', | |||||
`email` VARCHAR(32) NOT NULL UNIQUE COMMENT '邮箱', | |||||
`phone_number` VARCHAR(15) NOT NULL UNIQUE COMMENT '手机号码', | |||||
`status` INT(2) NOT NULL DEFAULT 1 COMMENT '状态,-1:逻辑删除,0:禁用,1:启用', | |||||
`create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', | |||||
`last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', | |||||
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' | |||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; |
@@ -0,0 +1,16 @@ | |||||
package com.xkcoding.orm.mybatis.plus; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class SpringBootDemoOrmMybatisPlusApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |
@@ -0,0 +1,153 @@ | |||||
package com.xkcoding.orm.mybatis.plus.service.impl; | |||||
import cn.hutool.core.collection.CollUtil; | |||||
import cn.hutool.core.date.DateTime; | |||||
import cn.hutool.core.util.IdUtil; | |||||
import cn.hutool.crypto.SecureUtil; | |||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||||
import com.baomidou.mybatisplus.core.metadata.IPage; | |||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||||
import com.xkcoding.orm.mybatis.plus.SpringBootDemoOrmMybatisPlusApplicationTests; | |||||
import com.xkcoding.orm.mybatis.plus.entity.User; | |||||
import com.xkcoding.orm.mybatis.plus.service.UserService; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.assertj.core.util.Lists; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import java.util.List; | |||||
import java.util.stream.Collectors; | |||||
/** | |||||
* <p> | |||||
* User Service 测试 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.mybatis.plus.service.impl | |||||
* @description: User Service 测试 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/8 18:13 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Slf4j | |||||
public class UserServiceImplTest extends SpringBootDemoOrmMybatisPlusApplicationTests { | |||||
@Autowired | |||||
private UserService userService; | |||||
/** | |||||
* 测试Mybatis-Plus 新增 | |||||
*/ | |||||
@Test | |||||
public void testSave() { | |||||
String salt = IdUtil.fastSimpleUUID(); | |||||
User testSave3 = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave3@xkcoding.com").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).build(); | |||||
boolean save = userService.save(testSave3); | |||||
Assert.assertTrue(save); | |||||
log.debug("【测试id回显#testSave3.getId()】= {}", testSave3.getId()); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 批量新增 | |||||
*/ | |||||
@Test | |||||
public void testSaveList() { | |||||
List<User> userList = Lists.newArrayList(); | |||||
for (int i = 4; i < 14; i++) { | |||||
String salt = IdUtil.fastSimpleUUID(); | |||||
User user = User.builder().name("testSave" + i).password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave" + i + "@xkcoding.com").phoneNumber("1730000000" + i).status(1).lastLoginTime(new DateTime()).build(); | |||||
userList.add(user); | |||||
} | |||||
boolean batch = userService.saveBatch(userList); | |||||
Assert.assertTrue(batch); | |||||
List<Long> ids = userList.stream().map(User::getId).collect(Collectors.toList()); | |||||
log.debug("【userList#ids】= {}", ids); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 删除 | |||||
*/ | |||||
@Test | |||||
public void testDelete() { | |||||
boolean remove = userService.removeById(1L); | |||||
Assert.assertTrue(remove); | |||||
User byId = userService.getById(1L); | |||||
Assert.assertNull(byId); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 修改 | |||||
*/ | |||||
@Test | |||||
public void testUpdate() { | |||||
User user = userService.getById(1L); | |||||
Assert.assertNotNull(user); | |||||
user.setName("MybatisPlus修改名字"); | |||||
boolean b = userService.updateById(user); | |||||
Assert.assertTrue(b); | |||||
User update = userService.getById(1L); | |||||
Assert.assertEquals("MybatisPlus修改名字", update.getName()); | |||||
log.debug("【update】= {}", update); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 查询单个 | |||||
*/ | |||||
@Test | |||||
public void testQueryOne() { | |||||
User user = userService.getById(1L); | |||||
Assert.assertNotNull(user); | |||||
log.debug("【user】= {}", user); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 查询全部 | |||||
*/ | |||||
@Test | |||||
public void testQueryAll() { | |||||
List<User> list = userService.list(new QueryWrapper<>()); | |||||
Assert.assertTrue(CollUtil.isNotEmpty(list)); | |||||
log.debug("【list】= {}", list); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 分页排序查询 | |||||
*/ | |||||
@Test | |||||
public void testQueryByPageAndSort() { | |||||
initData(); | |||||
int count = userService.count(new QueryWrapper<>()); | |||||
Page<User> userPage = new Page<>(1, 5); | |||||
userPage.setDesc("id"); | |||||
IPage<User> page = userService.page(userPage, new QueryWrapper<>()); | |||||
Assert.assertEquals(5, page.getSize()); | |||||
Assert.assertEquals(count, page.getTotal()); | |||||
log.debug("【page.getRecords()】= {}", page.getRecords()); | |||||
} | |||||
/** | |||||
* 测试Mybatis-Plus 自定义查询 | |||||
*/ | |||||
@Test | |||||
public void testQueryByCondition() { | |||||
initData(); | |||||
QueryWrapper<User> wrapper = new QueryWrapper<>(); | |||||
wrapper.like("name", "Save1").or().eq("phone_number", "17300000001").orderByDesc("id"); | |||||
int count = userService.count(wrapper); | |||||
Page<User> userPage = new Page<>(1, 3); | |||||
IPage<User> page = userService.page(userPage, wrapper); | |||||
Assert.assertEquals(3, page.getSize()); | |||||
Assert.assertEquals(count, page.getTotal()); | |||||
log.debug("【page.getRecords()】= {}", page.getRecords()); | |||||
} | |||||
/** | |||||
* 初始化数据 | |||||
*/ | |||||
private void initData() { | |||||
testSaveList(); | |||||
} | |||||
} |