查询专栏:自定义查询sql
文章目录
- 1. 在mapper接口中定义接口
- 2. 在xml文件中书写sql
- 3. 全局配置
- 3.1 配置xml的位置
- 3.2 配置实体类的位置
- 4. 在实体类中测试
- 5. 控制台输出
1. 在mapper接口中定义接口
/*** 查询所有 有条件会自动拼接在where 后边当条件 单表操作** @param wrapper* @return*///自定义sql 无分页List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
2. 在xml文件中书写sql
<?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.example.mybatisplus.sampleschapter1.dao.UserMapper"><select id="selectAll" resultType="com.example.mybatisplus.sampleschapter1.entity.User">select * from user ${ew.customSqlSegment}</select></mapper>
3. 全局配置
3.1 配置xml的位置
3.2 配置实体类的位置
- 可以在xml文件中直接写实体类名,省略前面的包路径
mybatis-plus:mapper-locations: classpath*:/mapper/**Mapper.xml#实体扫描,多个package用逗号或者分号分隔typeAliasesPackage: com.example.mybatisplus.sampleschapter1.entity
4. 在实体类中测试
/*** 自定义sql*/@Testpublic void selectMy() {LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();lambdaQuery.likeRight(User::getName, "王").and(lqw -> lqw.lt(User::getAge, 40).or().isNotNull(User::getEmail));//调用自定义sqlList<User> userList = userMapper.selectAll(lambdaQuery);userList.forEach(System.out::println);}
// select * from user WHERE name LIKE ? AND ( age < ? OR email IS NOT NULL )
5. 控制台输出
想学习更多微服务、分布式、中间件、数据库、项目快速构建等系列技术
请访问http://gblfy.com
让我们一起进步!!!