zoukankan      html  css  js  c++  java
  • Mybatis总结一之SQL标签方法

    ---恢复内容开始---

       定义:mapper.xml映射文件中定义了操作数据库的sql,并且提供了各种标签方法实现动态拼接sql。每个sql是一个statement,映射文件是mybatis的核心。

    一,内容标签

    1.NamePlace

    NamePlace命名空间作用是对sql进行分类化管理。若使用Dao开发方式,映射文件的namespace可以任意命名,如果采用的是mapper接口代理的方法开发,Mapper的映射文件中namespace必须为接口的全名。

    1 <?xml version="1.0" encoding="UTF-8" ?>
    2 <!DOCTYPE mapper
    3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5 <mapper namespace="Mapper.EmpMapper">
    6     //CURD操作标签
    7         //if片段
    8 </mapper>

    2.CRUD标签

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper
     3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <mapper namespace="Mapper.EmpMapper">
     6     <!-- 查询 -->
     7     <select id="" parameterType="" resultType=""></select>
     8 
     9     <!-- 添加 -->
    10     <insert id="" parameterType=""></insert>
    11 
    12     <!-- 删除 -->
    13     <delete id="" parameterType=""></delete>
    14 
    15     <!-- 更新 -->
    16     <update id="" parameterType=""></update>
    17 </mapper>

    3、标签调用方法

    (1)selectOne与selectList方法

    selectOne表示查询出一条结果进行映射,使用selectOne查询多条记录会抛出异常。selectureList表示查询一个列表进行映射,对于使用selectOne可以实现的插叙,使用selectList必然也可以实现。

    (1)代理对象内部调用

    动态代理对象调用sqlSession.selectOne()和sqlSessionList()是根据mapper接口方法的返回值决定的,如果mapper方法返回单个pojo对象,代理对象内部通过selectOne查询数据库。如果mapper()方法返回集合对象,代理对象内部通过selectList查询数据库。

    二、动态SQL标签

    1、if标签

     1 //进行空字符串校验
     2 <select id="findUserList" parameterType="user" resultType="user">
     3     select * from user  where 1=1
     4         <if test="id!=null and id!=''">
     5             and id=#{id}
     6         </if>
     7         <if test="username!=null and username!=''">
     8             and username like '%${username}%'
     9         </if>
    10 </select>

    2、where标签

     1 //<where/>可以自动处理第一个and
     2 <select id="findUserList" parameterType="user" resultType="user">
     3     select * from user
     4         <where>
     5             <if test="id!=null and id!=''">
     6                 and id=#{id}
     7             </if>
     8             <if test="username!=null and username!=''">
     9                 and username like '%${username}%'
    10             </if>
    11         </where>
    12 </select>

    3、sql片段

     1 //建立sql片段
     2 <sql id="query_user_where">
     3     <if test="id!=null and id!=''">
     4         and id=#{id}
     5     </if>
     6     <if test="username!=null and username!=''">
     7         and username like '%${username}%'
     8     </if>
     9 </sql>
    10 
    11 //使用include引用sql片段
    12 <select id="findUserList" parameterType="user" resultType="user">
    13     select * from user
    14         <where>
    15             <include refid="query_user_where"/>
    16         </where>
    17 </select>
    18 
    19 //引用其它mapper.xml的sql片段
    20 <include refid="namespace.sql片段"/>

    三、foreach标签

    1、通过sql传递数据或list,mybatis使用foreach参数定义如下:Collection指定输入对象中集合属性,item每个遍历生成对象,open开始遍历时拼接的串,close结束遍历时拼接的串,separator:遍历的两个对象需要拼接的串。

    (sql)语句

    1 SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
    2 SELECT * FROM USERS WHERE username LIKE '%张%' id IN (10,89,16)

    (vo)类

    1 public class QueryVo{
    2     private User user;
    3         private UserCustom userCustom;
    4     //传递多个用户id
    5     private List<Integer> ids;
    6     set()/get()  ...
    7 }

    (映射文件)

     1 复制代码
     2 <select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
     3      SELECT * FROM USER
     4      <where>
     5         <!-- 使用实现下边的sql拼接: AND (id=1 OR id=10 OR id=16) -->
     6 
     7         <if test="ids!=null and ids.size>0">
     8 
     9            <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
    10 
    11                   id=#{user_id}
    12 
    13            </foreach>
    14 
    15        </if>
    16      </where> 
    17 </select>
    18 
    19  
    20 
    21 <!-- 使用实现下边的sql拼接: and id IN(1,10,16)—>
    22 
    23 <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
    24 
    25      #{user_id}
    26 
    27 </foreach>

    (测试代码)

    List<Integer> ids = new ArrayList<Integer>();
    ids.add(1);//查询id为1的用户
    ids.add(10); //查询id为10的用户
    queryVo.setIds(ids);
    List<User> list = userMapper.findUserList(queryVo);

     2、传递单个list

    (Mapper映射文件)

     1 <select id="selectUserByList" parameterType="java.util.List" resultType="user">
     2     select * from user
     3       <where>
     4       <!-- 传递List,List中是pojo -->
     5       <if test="list!=null">
     6          <foreach collection="list" item="item" open="and id in( "separator="," close=")">
     7             #{item.id}
     8          </foreach>
     9        </if>
    10      </where>
    11 </select>

    (Mapper)接口

    1 public List<User> selectUserByList(List userlist);

    (测试)

     1 //构造查询条件List
     2 List<User> userlist = new ArrayList<User>();
     3 User user = new User();
     4 user.setId(1);
     5 userlist.add(user);
     6 
     7 user = new User();
     8 user.setId(2);
     9 userlist.add(user);
    10 //传递userlist列表查询用户列表
    11 List<User>list = userMapper.selectUserByList(userlist);

    3、传递pojo类数组

    (Mapper映射文件)

    参数含义:index为数组的下标,item为数组每个元素的名称,名称随意,open循环开始,close循环结束,separator中间分隔输出。

     1 <select id="selectUserByArray" parameterType="Object[]" resultType="user">
     2     select * from user
     3       <where>
     4          <!-- 传递pojo类数组 -->
     5         <if test="array!=null">
     6             <foreach collection="array" index="index" item="item"
     7                          open="and id in("separator=","close=")">
     8                     #{item.id}
     9             </foreach>
    10         </if>
    11        </where>
    12 </select>

    (mapper接口)

    1 public List<User> selectUserByArray(Object[] userlist)

    (测试)

     1 //构造查询条件List
     2 Object[] userlist = new Object[2];
     3 User user = new User();
     4 user.setId(1);
     5 userlist[0]=user;
     6 
     7 user = new User();
     8 user.setId(2);
     9 userlist[1]=user;
    10 
    11 //传递user对象查询用户列表
    12 List<User>list = userMapper.selectUserByArray(userlist);

    4、传递字符串类数组

    (1)Mapper映射文件

     1 复制代码
     2 <select id="selectUserByArray" parameterType="Object[]" resultType="user">
     3     select * from user
     4     <where>
     5         <!-- 传递字符串数组 -->
     6         <if test="array!=null">
     7             <foreach collection="array"index="index"item="item"
     8                     open="and id in("separator=","close=")">
     9                     #{item}
    10             </foreach>
    11         </if>
    12     </where>
    13 </select>

    如果数组中是简单类型则写为#{item},不用通过ognl获取对象属性值

    (2)mapper接口

    1 public List<User> selectUserByArray(Object[] userlist)

    (3)测试

    1 //构造查询条件List
    2 Object[] userlist = new Object[2];
    3 userlist[0]=”1”;
    4 userlist[1]=”2”;
    5 //传递user对象查询用户列表
    6 List<User>list = userMapper.selectUserByArray(userlist);
  • 相关阅读:
    小学生入门激光SLAM<二>、2.1-前端点云帧间匹配
    小学生入门激光SLAM<一>、激光雷达去除畸变
    imgui开发笔记<1>、ubuntu环境下快速应用
    ROS问题记录
    LibTorch实战六:U2-Net理论详解<一>
    git-clone小技巧
    深度学习模型部署概述
    debian10 docker 安装 jenkins
    FastDFS集群部署
    python中的metaclass
  • 原文地址:https://www.cnblogs.com/yanpingping/p/10891143.html
Copyright © 2011-2022 走看看