Springboot2注解使用Mybatis动态SQL

知识点:
(1)注解写动态SQL,用

(2)SQL的拼接可以使用+号,也可以使用逗号。我这里使用的是逗号,要使用+号可以把

(2)实现IN查询中 > 符号需要转义为 > ,其中foreach的collection直接写成@param中的值即可。

(3)这是一种使用注解完全替代XML的方法,稍微复杂的SQL语句推荐使用XML方式。

1.简单的sql
//从***数据表获取统计数据 @Select("select count(*) from issues where issue_type = #{type}") String getIssueCount(String type);

2.动态SQL使用

//获取时间段内issue详细信息(可根据项目名、开发者名、问题类型获取)
    @Select({"<script>",
            "select id,committername,type,count,projectname,file,line,message,creationdate,updatedate from issue",
            "where creationdate BETWEEN #{startDate} AND #{endDate}",
            "<if test='committer != null and committer.length &gt; 0'>",
            "AND committername IN",
            "<foreach item='item' index='index' collection='committer' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</if>",
            "<if test='type != null and type.length &gt; 0'>",
            "AND type IN",
            "<foreach item='item' index='index' collection='type' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</if>",
            "<if test='project != null and project.length &gt; 0'>",
            "AND projectname IN",
            "<foreach item='item' index='index' collection='project' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</if>",
            "</script>"})
    List<IssueModel> getDetailIssue(@Param("startDate") String startDate, @Param("endDate")String endDate,
                                    @Param("committer") String[] committer, @Param("type") String[] type,
                                    @Param("project") String[] project);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot使用MyBatis实现动态SQL的详细流程如下: 1. 引入MyBatisMyBatis-Plus依赖:在pom.xml文件中添加MyBatisMyBatis-Plus的依赖。 ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> ``` 2. 配置数据源:在application.properties中配置数据源。 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 创建实体类:创建需要查询的实体类。 ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 4. 创建Mapper接口:创建Mapper接口,并使用@Mapper注解将其注册到Spring容器中。 ```java @Mapper public interface UserMapper extends BaseMapper<User> { List<User> selectByCondition(@Param("name") String name, @Param("email") String email, @Param("age") Integer age); } ``` 5. 创建Mapper.xml文件:在resources/mapper目录下创建Mapper.xml文件,并编动态SQL语句。 ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="selectByCondition" resultType="User"> SELECT * FROM user <where> <if test="name != null"> AND name = #{name} </if> <if test="email != null"> AND email LIKE '%${email}%' </if> <if test="age != null"> AND age = #{age} </if> </where> </select> </mapper> ``` 6. 配置MapperScan:在启动类中添加@MapperScan注解,用于扫描Mapper接口。 ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 7. 调用Mapper接口:在业务代码中调用Mapper接口的方法,传入参数即可实现动态SQL查询。 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> selectByCondition(String name, String email, Integer age) { return userMapper.selectByCondition(name, email, age); } } ``` 通过以上步骤,就可以在Spring Boot使用MyBatis实现动态SQL的查询功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值