SpringBoot + MyBatis

一、SpringBoot +MyBatis项目搭建

1.1 新建一个SpringBoot项目,并在pom.xml下加入以下代码

 <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>2.0.1</version>
   </dependency>

1.2 application.properties文件下配置(使用的是MySql数据库)

# 注:我的SpringBoot 是2.0以上版本,数据库驱动如下
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=your_username
spring.datasource.password=your_password

# 可将 com.dao包下的dao接口的SQL语句打印到控制台,学习MyBatis时可以开启
logging.level.com.dao=debug

1.3 Sql动态映射

@Results(id="userResults", value={
            @Result(property="id",   column="t_id"),
            @Result(property="age",  column="t_age"),
            @Result(property="name", column="t_name"),
    })

 @ResultMap("userResults")

二、MyBatis动态SQL

2.1 foreach

  循环,可对传入和集合进行遍历。一般用于批量更新和查询语句的 in

<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    #{item}
</foreach>

注:

    (1) item:集合的元素,访问元素的Filed 使用 #{item.Filed}

  (2) index: 下标,从0开始计数

  (3) collection:传入的集合参数

  (4) open:以什么开始

  (5) separator:以什么作为分隔符

  (6) close:以什么结束

  例如 传入的list 是一个 List<String>: ["a","b","c"],则上面的 foreach 结果是: ("a", "b", "c")

2.2 动态SQL例子

 /**
     * if 对内容进行判断
     * 在注解方法中,若要使用MyBatis的动态SQL,需要编写在<script></script>标签内
     * 在 <script></script>内使用特殊符号,则使用java的转义字符,如  双引号 "" 使用&quot;&quot; 代替
     * concat函数:mysql拼接字符串的函数
     */
    @Select("<script>"
            + "select t_id, t_name, t_age                          "
            + "from sys_user                                       "
            + "<where>                                             "
            + "  <if test='id != null and id != &quot;&quot;'>     "
            + "    and t_id = #{id}                                "
            + "  </if>                                             "
            + "  <if test='name != null and name != &quot;&quot;'> "
            + "    and t_name like CONCAT('%', #{name}, '%')       "
            + "  </if>                                             "
            + "</where>                                            "
            + "</script>                                           ")
    @Results(id="userResults", value={
        @Result(property="id",   column="t_id"),
        @Result(property="name", column="t_name"),
        @Result(property="age",  column="t_age"),
    })
    List<User> selectUserWithIf(User user);
    
    /**
     * choose when otherwise 类似Java的Switch,选择某一项
     * when...when...otherwise... == if... if...else... 
     */
    @Select("<script>"
            + "select t_id, t_name, t_age                                     "
            + "from sys_user                                                  "
            + "<where>                                                        "
            + "  <choose>                                                     "
            + "      <when test='id != null and id != &quot;&quot;'>          "
            + "            and t_id = #{id}                                   "
            + "      </when>                                                  "
            + "      <otherwise test='name != null and name != &quot;&quot;'> "
            + "            and t_name like CONCAT('%', #{name}, '%')          "
            + "      </otherwise>                                             "
            + "  </choose>                                                    "
            + "</where>                                                       "
            + "</script>                                                      ")
    @ResultMap("userResults")
    List<User> selectUserWithChoose(User user);
    
    /**
     * set 动态更新语句,类似<where>
     */
    @Update("<script>                                           "
            + "update sys_user                                  "
            + "<set>                                            "
            + "  <if test='name != null'> t_name=#{name}, </if> "
            + "  <if test='age != null'> t_age=#{age},    </if> "
            + "</set>                                           "
            + "where t_id = #{id}                               "
            + "</script>                                        ")
    int updateUserWithSet(User user);
    
    /**
     * foreach 遍历一个集合,常用于批量更新和条件语句中的 IN
     * foreach 批量更新
     */
    @Insert("<script>                                  "
            + "insert into sys_user                    "
            + "(t_id, t_name, t_age)                   "
            + "values                                  "
            + "<foreach collection='list' item='item'  "
            + " index='index' separator=','>           "
            + "(#{item.id}, #{item.name}, #{item.age}) "
            + "</foreach>                              "
            + "</script>                               ")
    int insertUserListWithForeach(List<User> list);
    
    /**
     * foreach 条件语句中的 IN
     */
    @Select("<script>"
            + "select t_id, t_name, t_age                             "
            + "from sys_user                                          "
            + "where t_name in                                        "
            + "  <foreach collection='list' item='item' index='index' "
            + "    open='(' separator=',' close=')' >                 "
            + "    #{item}                                            "
            + "  </foreach>                                           "
            + "</script>                                              ")
    @ResultMap("userResults")
    List<User> selectUserByINName(List<String> list);
    
    /**
     * bind 创建一个变量,绑定到上下文中
     */
    @Select("<script>                                              "
            + "<bind name=\"lname\" value=\"'%' + name + '%'\"  /> "
            + "select t_id, t_name, t_age                          "
            + "from sys_user                                       "
            + "where t_name like #{lname}                          "
            + "</script>                                           ")
    @ResultMap("userResults")
    List<User> selectUserWithBind(@Param("name") String name);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值