7、mybatis-plus中wrapper的使用

1、例子1,查询非空和大于

QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
        .isNotNull("name")
        .isNotNull("email")
        .ge("age",12);
userMapper.selectList(wrapper).forEach(System.out::println);

2、例子2,查询单个物体,如:名字为

QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","chen");
User user = userMapper.selectOne(wrapper);
System.out.println(user);

3、例子3,查询一个范围内的物体个数

QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age",20,30);
Interger count= userMapper.selectCount(wrapper);
System.out.println(count);

4、例子4,模糊查询

不包含:notLike

右包含,如:t%,也就是t开头;

左包含,如:%t,也就是t结尾;

两边包含,如:%t%,也就任何位置包含了t

5、例子5,子查询

6、根据ID排序

 或者

 

MyBatis-Plus配置自定义SQL插入语句通常可以通过实现`IsqlInjector`接口来自定义SQL片段,然后在Mapper接口使用自定义的SQL语句进行插入操作。以下是基本步骤: 1. 创建一个自定义的`IsqlInjector`实现类,该类继承自`DefaultSqlInjector`。在该类重写`inspectInject`方法,添加自定义的SQL注入器。 ```java public class CustomSqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); // 添加自定义的插入方法 methodList.add(new InsertBatchSomeColumn()); return methodList; } } ``` 2. 在自定义的`AbstractMethod`实现类,重写`InjectMappedStatement`方法,用于定义自定义的插入SQL语句。 ```java public class InsertBatchSomeColumn extends AbstractMethod { @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { // 自定义SQL逻辑 String sql = "INSERT INTO " + tableInfo.getTableName() + " (<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\",\" >" + " <if test=\"item.column1 != null\">column1,</if>" + " <if test=\"item.column2 != null\">column2,</if>" + "</foreach>) VALUES " + " <foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\",\" close=\")\"> " + " <if test=\"item.column1 != null\">#{item.column1},</if>" + " <if test=\"item.column2 != null\">#{item.column2},</if>" + "</foreach>"; // 获取列名和值 // ... // 注入MappedStatement // ... return new MappedStatement(); } } ``` 3. 在MyBatis的配置文件,将`CustomSqlInjector`设置为自定义的SQL注入器。 ```xml <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <!-- 其他配置项 --> <!-- 自定义SQL注入器 --> <property name="sqlInjector" ref="customSqlInjector" /> </bean> <bean id="customSqlInjector" class="你的包名.CustomSqlInjector"></bean> ``` 4. 在Mapper接口使用自定义的插入方法。 ```java public interface YourMapper extends BaseMapper<YourEntity> { // 使用自定义的插入方法 int insertBatchSomeColumn(List<YourEntity> list); } ``` 这样配置后,就可以使用自定义的`insertBatchSomeColumn`方法进行批量插入操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值