Springboot整合Mybatis-plus
mybatis-plus官网地址:https://mp.baomidou.com/guide/
1.基本配置
创建一个springboot项目,本次使用的springboot版本是 2.4.2,配置mapper包扫描
@SpringBootApplication
@MapperScan("com.xxx.springboot.mapper")
public class SpringbootdataApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdataApplication.class, args);
}
}
注意: BaseMapper 引入的包是:com.baomidou.mybatisplus.core.mapper.BaseMapper;
service引入的包是: com.baomidou.mybatisplus.extension.service
1.1引入依赖
<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>
<!-- mybatisplus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
1.2application.properties配置
spring.datasource.url=jdbc:mysql://localhost:3306/permission
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.servlet.context-path=/web
server.port=8090
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.xxx.entity
1.3entity类
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user") //标识使用的数据库表
public class User {
@TableId(type = IdType.AUTO) //自动映射数据库主键
private Integer id;
//@TableField("数据库列名")
private String name;
private String password;
private Integer deptId;
private Integer delFlag;
}
1.4mapper层
mapper层的接口继承BaseMapper泛型类传递一个需要执行增删改查的实体类
public interface UserMapper extends BaseMapper<User> {
}
1.5Service层
service层的接口继承IService泛型类传递一个需要执行增删改查的实体类
public interface IUserService extends IService<User> {
}
1.6service的实现层
service层的实现类继承IService泛型类传递一个需要执行增删改查的实体类和mapper接口
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService{
}
2.简单crud案例
使用 Service CRUD 接口
@RequestMapping("query")
public void testQuery(){
System.out.println("--- testQuery method test ---");
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("dept_id",1);
List<User> userList = userService.list(wrapper);
userList.forEach(System.out::println);
}
@RequestMapping("save")
public void testSave(){
System.out.println("--- testSave method test ---");
User user = new User("张三丰1", "123456", 1, 0);
boolean result = userService.save(user);
System.out.println(result);
}
@RequestMapping("update")
public void testUpdate(){
System.out.println("--- testUpdate method test ---");
User user = new User(52,"张三丰弟子张翠山", "123456", 1, 0);
boolean result = userService.updateById(user);
System.out.println(result);
}
@RequestMapping("delete")
public void testDelete(Integer id){
System.out.println("--- testDelete method test ---");
boolean result = userService.removeById(id);
System.out.println(result);
}
3.分页查询
spring-boot添加配置
@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
带条件的分页查询
@RequestMapping("query")
public IPage<User> testQuery(){
System.out.println("--- selectAll method test ---");
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.likeRight("name","Iverson"); //模糊查询 相当于 Iverson%
// 条件分页查询
Page<User> page = new Page<>();
page.addOrder(OrderItem.desc("id"));
page.setSize(3);
page.setCurrent(2);
Page<User> userPage = userService.page(page,wrapper);
return userPage;
}
4.配置日志
我们所有的sql现在是不可见的,我们希望知道它是怎么执行的,所以需要配置日志打印。
Mybatis - Plus
# 配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Mybatis
# 配置日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl