简介
简单的 搭建 springBoot -MyBatis Plus 项目 的简单的 使用,MyBatis Plus 支持 CRUD,启动之后,会自动注入基本的CRUD,性能基本无消耗,直接面向对象操作,说直白的就是基本的增删改查和分页
Mybatis plus 3.0
Mybatis 官方 文档地址:https://mp.baomidou.com/
特性:
项目搭建:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.1.0.Final</org.mapstruct.version>
<lombok.version>1.16.20</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>${mysql.version}</scope>
</dependency>
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
application
server.tomcat.uri-encoding=UTF-8
server.tomcat.max-threads=1000
server.tomcat.min-spare-threads=30
server.port=8091
server.servlet.context-path=/hopson
spring.profiles.active=test
#配置mybatis的扫描的包的文件的入口
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath\:mybatis/mapper/*.xml
application-test
#server.port = 8091
#mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://39.97.96.194:3306/renren_security?characterEncoding=utf-8&useSSL=false
spring.datasource.username=hopson
spring.datasource.password=hopson
#mybatis-plus 配置
mybatis-plus.mapper-locations=classpath*:/mybatis/mapper/**Mapper.xml
mybatis-plus.type-aliases-package=com.bims.hopson.*.poEntity
#
spring.datasource.initialSize=5
#
spring.datasource.minIdle=5
#
spring.datasource.maxActive=20
#
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.jackson.serialization.indent_output=true
//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.bims.hopson.*.service.*.*Dao")//扫描 service 和 dao
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
@Data
@TableName("sys_user")//myBatis plus 注解 表名称
public class SysUserPoEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户ID
*/
@TableId(value = "user_id")
//@TableField(value = "user_id")
private Long userId;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
/**
* 盐
*/
private String salt;
/**
* 邮箱
*/
private String email;
/**
* 手机号
*/
private String mobile;
/**
* 用户名
*/
private String name;
/**
* 状态 0:禁用 1:正常
*/
private Integer status;
/**
* 角色ID列表
*/
@TableField(exist=false)
private List<Long> roleIdList;
/**
* 创建时间
*/
@TableField(value = "create_time")
private Date createTime;
/**
* 部门ID
*/
@TableField(value = "dept_id")
private Long deptId;
/**
* 部门名称
*/
@TableField(exist=false)
private String deptName;
}
<?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.bims.hopson.standard.dao.SysUserDao">
</mapper>
@Mapper
public interface SysUserDao extends BaseMapper<SysUserPoEntity> {
}
service
public interface SysUserService extends IService<SysUserPoEntity> {
//查询所有
List<SysUserPoEntity> list ();
//分页查询
IPage<SysUserPoEntity> pageList();
}
impl
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserPoEntity> implements SysUserService {
@Override
public List<SysUserPoEntity> list() {
QueryWrapper<SysUserPoEntity> wrapper = new QueryWrapper<SysUserPoEntity>();
return baseMapper.selectList(wrapper);
}
@Override
public IPage<SysUserPoEntity> pageList() {
QueryWrapper<SysUserPoEntity> wrapper = new QueryWrapper<SysUserPoEntity>();
Page<SysUserPoEntity> page = new Page<SysUserPoEntity>(1,2);
baseMapper.selectPage(page,wrapper);
return baseMapper.selectPage(page,wrapper);
}
}
@RestController
@RequestMapping("/user")
public class SysUserController{
private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
@Autowired
private SysUserService service;
/**
* 所有用户列表 分页
*/
@RequestMapping("/page")
public R page(){
IPage<SysUserPoEntity> list = service.pageList();
return R.ok().put("list", list);
}
/**
* 所有用户列表
*/
@RequestMapping("/list")
public R list(){
List<SysUserPoEntity> list = service.list();
return R.ok().put("list", list);
}
}