这个可得好好介绍一下了
但很多开源项目都还是采用的ssm中的mybatis
但这个就是对mybatis的优化优化又优化
既然语法优化简便了 那自然就有其语法等规范
参考: 官网 https://baomidou.com/pages/2976a3/#spring-boot
首先肯定是依赖啦啦啦
我的对应springboot版本为2.2.5.RELEASE
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
简化了
看看mapper
只有这一个
package ji.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import ji.entity.User;
import org.springframework.stereotype.Repository;
//继承自BaseMapper 简化了CURD的编写
@Repository
public interface UserMapper extends BaseMapper<User> {
}
然后来测试一下
我们都没有写哪些xml文件映射啥的
哦哦哦还有得配置下mybatis-plus的东西
还是死东西:
对应改一下账号密码数据库名字
spring.application.name=demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT&2B8&userUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#逻辑删除
mybatis-plus.global-config.db-config.logic-delete-value=1
# 逻辑已删除值(默认为 1)
mybatis-plus.global-config.db-config.logic-not-delete-value=0
# 逻辑未删除值(默认为 0)
server.servlet.context-path=/fang
server.port=666
需要用到的数据表
先来看简单的增删改查测试:
真正的一般得写service层 然后controller调用
我们就简单在test文件里检验一下:
package ji;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ji.entity.User;
import ji.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
class DemoApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
// Wrapper条件构造器
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
@Test
public void insert() {
User user1 = new User(4, "哇哇哇哇", "147258",null);
User user2 = new User();
user2.setName("john");
user2.setPwd("888888");
userMapper.insert(user1);
userMapper.insert(user2);
}
@Test
public void update() {
User user = new User();
user.setId(4);
user.setName("mike");
userMapper.updateById(user);
}
@Test
public void selectByid() {
User user1 = userMapper.selectById(2);
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
System.out.println(user1);
//增强版forEach
users.forEach(System.out::println);
}
//条件查询
@Test
public void selectConditons() {
HashMap<String, Object> hashMap = new HashMap<>();
//查询密码为123的user
hashMap.put("pwd",123);
List<User> users = userMapper.selectByMap(hashMap);
users.forEach(System.out::println);
}
//分页查询
@Test
public void selectPage() {
Page<User> page = new Page<>(1,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
}
//删除
@Test
public void delete() {
userMapper.deleteById(2);
userMapper.deleteBatchIds(Arrays.asList(1,2));
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("pwd",888888);
userMapper.deleteByMap(hashMap);
}
}
要注意的是删除和分页查询 需要配置一些东西:
在config里面
package ji.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("ji.mapper")
public class MybatisPlusInterceptor {
//分页查询
@Bean
public PaginationInterceptor paginationInnerInterceptor() {
return new PaginationInterceptor();
}
自动注入:
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}
}
还有在检测逻辑删除(相当于就是update方法)时候会报错:
参考:https://www.jb51.net/article/198784.htm
你会注意到我的表中字段 delete错误的 会冲突语法
更改它
哦哦哦 还有
我在进行分页查询的时候
Page
userMapper.selectPage(page,null);
注意更改范型
是你的实体类
package ji.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
//逻辑删除
@TableLogic
private Integer del_ete;
}
还有就是一些子查询或者什么条件 like之类的查询:
package ji;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ji.entity.User;
import ji.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class WrapperTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
// Wrapper条件构造器
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.isNotNull("name")
.ge("age", 18);
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
@Test
public void testWrapper2() {
//查询name=高远的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name", "高远");
User user = userMapper.selectOne(wrapper);
System.out.println(user);
}
@Test
public void testWrapper3() {
//查询age在15-20之间的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age", 15, 20);//区间
Integer count = userMapper.selectCount(wrapper);//输出查询的数量selectCount
System.out.println(count);
}
@Test
public void testWrapper4() {
//模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
// .like("name", "芳")
//likeRight 则为%在右边 高%
.likeRight("name", "高");
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
}
@Test
public void testWrapper5() {
//模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
//id 在子查询中查出来
wrapper.inSql("id", "select id from user where id<5");
List<Object> objects = userMapper.selectObjs(wrapper);
objects.forEach(System.out::println);
}
@Test
public void testWrapper6() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//通过id进行降序排序
wrapper.orderByDesc("id");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
}
大致就这
主要看以后的实际应用 就是分析他人的开源项目 看能不能将其更改为mybatis-plus使用