Mapper.xml映射文件

Mapper.xml映射文件:
 <select>  resultType如果返回的是集合,那么应该设置为集合包含的类型而不是集合本身的类型
如果参数类型是pojo类型,参数名必须是pojo中的属性名
<insert>
session.commit();
session = factory.openSession();

driver:com.mysql.jdbc.Driver
url:jdbc:mysql://127.0.0.1:3306/test
动态sql:
小于号
<![CDATA[<=]]>
:

<where>:
<if test="sal != null">
and sal <![CDATA[<=]]>#{sal}         //and,or会自动给去掉
</where>
choose when otherwise:
when otherwise中只走一个
select * from emp where
<choose>
    <when test="sal != null">
        sal <![CDATA[<=]]> #{sal}
    </when>
    <when test="ename != null">
        ename like concat('%',#{ename},'%')
    </when>
    <otherwise>
        deptno=#{deptno}
    <otherwise>
</choose>

trim标签  组合 if标签 主要功能 可以在自己包含的内容前面加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;
            可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides

insert into emp
<trim prefix="("  suffix=")" suffixOverrides = ",">
    <if test = "empno != null">
        empno,
    <if test = "ename != null">
        ename,
    <if test = "mgr != null">
        mgr,
    <if test = "sal != null">
        sal,
    <if test = "deptno != null">
        deptnol,
</trim>
values
<trim prefix="("  suffix=")" suffixOverrides = ",">
    <if test = "empno !=null">
        #{empno},
    <if test = "ename != null">
        #{ename},
    <if test = "mgr != null">
        #{mgr},
    <if test = "sal != null">
        #{sal},
    <if test = "deptno != null">
        #{deptnol},
</trim>


<set>标签: 如果包含的语句以逗号结束会把逗号省略掉

update   emp
<set>
    <if test="sal != null">
        sal=#{sal},
    </if>
    <if test="mgr != null">
        mgr=#{mgr},
    </if>
    <if test="ename != null">
        ename=#{ename},
    </if>
</set>
<where>
    <if test="empno != null">
        and empno = #{empno}
    </if>
    <if test="job != null">
        and job =  #{job}
    </if>
</where>

<foreach>主要用在构建in条件中,它可以在SQL语句中迭代一个集合
item 表示集合中每一个元素进行迭代时的别名
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置
[LIST]:

select * from emp 
<where>
    <foreach collection = "list" open = "(" close = ")" item="empno" seperator="or">
        empno=#{empno}
    </foreach>
</where>

select * from emp where empno in 
    <foreach collection = "list" open = "(" close = ")" item="empno" seperator=",">
        #{empno}
    </foreach>
</where>


[array]:
paramaterType = List

select * from emp where empno in 
    <foreach collection = "array" open = "(" close = ")" item="empno" seperator=",">
        #{empno}
    </foreach>
</where>
[map]:

(Map<String,List<Integer> map>)
多条件复杂查询的时候
List<Emp> selectEmpByMapinfo(Map<String,object>);

<select id = "selectEmpByMapinfo" paramaterType="java.util.Map" resultType="emp">

    select * from emp
    <where>
        deptno=#{deptnokey}
        and
        ename like '%${enamekey}%'
        and
        empno in
        <foreach collection="empnos" open="(" close=")" separator="," item="no">
            #{no}
        </foreach>
    </where>
</select>.


map.put("deptnokey",10)
map.put("enamekey","enamekey")
map.put(empnos,list)
[SQL片段]:

<sql id="sqlid">
    select * from emp
</sql>
<include refid="sqlid"></include>


[bind]标签

<select>
    <bind name="name" value="'%' + ename + '%'">
    select * from emp
    <where>
        <if test="sal != null">
            and sal <![CDATA[ <= ]]>#{sal]
        </if>
        <if test="ename != null">
            and ename like #{name}
        </if>
    </where>
</select>

resultType实现一对一 
多对多:

<collection property="ods" ofType="orderDetail" >
</collection>
<association property="items" javaType="items">
</association>


SqlSessionFactory factory;
Inputstream stream = Resources.getResourceasStream("SqlMapConfig.xml");
factory = SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder().build(stream)
 
page工具类实现分页:page工具类:
PageHelper.startPage((page- 1)* size,size)

相关推荐

  1. mapperXML标签总结

    2024-03-18 06:54:07       17 阅读
  2. Mapper.xml映射文件

    2024-03-18 06:54:07       17 阅读
  3. MyBatis之配置文件映射文件

    2024-03-18 06:54:07       26 阅读
  4. XML 映射文件(Mapper 文件)的命名空间

    2024-03-18 06:54:07       11 阅读
  5. 描述Hibernate的映射文件和配置文件的作用

    2024-03-18 06:54:07       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-18 06:54:07       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-18 06:54:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-18 06:54:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-18 06:54:07       18 阅读

热门阅读

  1. 蓝桥杯day3刷题日记--P9240 冶炼金属

    2024-03-18 06:54:07       15 阅读
  2. python request pandas excel 接口自动化测试框架

    2024-03-18 06:54:07       16 阅读
  3. 【XML】xml转Freemind思维导图

    2024-03-18 06:54:07       17 阅读
  4. string和stringbuilder

    2024-03-18 06:54:07       15 阅读
  5. Unity转换字符串中文繁简体

    2024-03-18 06:54:07       18 阅读
  6. python图形化编程turtle小乌龟

    2024-03-18 06:54:07       15 阅读
  7. 大语言模型相关工具使用链接

    2024-03-18 06:54:07       19 阅读
  8. LLaMA-2 简介:开源大型语言模型的新篇章

    2024-03-18 06:54:07       18 阅读
  9. Linux初级知识大全(一)

    2024-03-18 06:54:07       20 阅读