zoukankan      html  css  js  c++  java
  • MyBatis映射文件UserMapper.xml(mysql环境)

    引言

    Mybatis的真正强大,在于她对SQL的映射,这也是她吸引人的地方。实现相同的功能,她要比直接使用JDBC省去95%的代码量。而且将SQL语句独立在Java代码之外,为程序的修改和纠错提供了更大的灵活性,可以直接修改SQL语句,而无需重新编译Java程序。

    SQL映射文件也是XML格式,其顶级元素有以下几个:

    • select - 映射sql查询语句
    • insert - 映射sql插入语句
    • update - 映射sql更新语句
    • delete - 映射sql删除语句
    • sql - 就像程序中可以复用的函数一样,这个元素下放置可以被其他语句重复引用的sql语句
    • resultMap - 用来描述如何从数据库查询结果集中来加载对象
    • cache - 给定命名空间的缓存配置
    • cache-ref - 其他命名空间缓存配置引用

    1.mapper配置xml文件和接口关联。

    1 <!--namespace务必和接口的全类名一致 -->
    2 <mapper namespace="cn.bdqn.li.UserMapper">

    2.resultMap映射。存在条件:当数据库字段名和实体类中的属性名不同的情况下。

    1 <!--字段名和属性名不同时,使用resultMap映射-->
    2     <resultMap id="userMap" type="User">
    3         <id property="id" column="xsdm"/>
    4         <result property="name" column="xsmc"/>
    5     </resultMap>

    3.insert into 添加语句(带主键回写)

    <!--id务必和接口中的方法名称对应
        如果参数类型是一个对象,那么sql语句中#{对象的属性名}
             KeyProperty属性表示:   数据表主键对应的实体对象属性名称
             parameterType:要执行的dao中的方法的参数,如果是类的话,必须使用全路径类
            (全路径名可以使用在mybatis.xml文件中配置的别名代替)
    -->
    <!--新增用户的同时 拿到数据库中的id
     01.完成新增操作之后并没有把连接还给连接池
     02.而是接着使用连接去查询id
     SELECT @@IDENTITY
     SELECT LAST_INSERT_ID()   都可以获取刚刚插入数据的主键
    
      mysql中使用 order="AFTER", 主键自增,必须是插入数据成功之后才能获取id
      oracle中使用 order="BEFORE" ,必须先从序列中获取id,才能新增!
    
      useGeneratedKeys:  mybatis会根据数据库的不同获取主键===》 主键回填
     -->
    
    
        <insert id="addUser" parameterType="User"  keyProperty="id" useGeneratedKeys="true">
            insert into xs(xsmc)
            values(#{name})
             <!-- 回显,添加数据后,返回对象的id值 -->
            <selectKey resultType="int" keyProperty="id" order="AFTER">
                select @@IDENTITY
            </selectKey>
        </insert>

    4.查询语句

      

      1  <!--查询指定的user对象
      2             当数据库中的字段和实体类中的属性名相同时,使用resultType=“实体类名”。
      3         -->
      4     <select id="selectUserById" parameterType="int" resultType="User">
      5         SELECT  xsmc  from xs  where xsdm=#{id}
      6     </select>
      7     <!--查询指定的user对象
      8         当数据库中的字段和实体类中的属性名不相同时,使用resultMap=“上文写的resultMap的id名称”。
      9     -->
     10     <select id="selectUserById1" parameterType="int" resultMap="userMap">
     11         SELECT  xsmc  from xs  where xsdm=#{id}
     12     </select>
     13 
     14     <!--查询所有的用户信息-->
     15     <select id="selectAllUsers" resultMap="userMap">
     16         SELECT  xsdm,xsmc  from xs
     17     </select>
     18 
     19     <!--根据名称进行模糊查询-->
     20     <select id="selectByNamelike"  resultMap="userMap">
     21         SELECT  xsdm,xsmc  from xs
     22         WHERE   xsmc like concat('%',#{zzz},'%')
     23     </select>
     24     <!--多条件链接查询1(使用list集合)-->
     25     <select id="findUserByParams" resultMap="userMap">
     26         SELECT xsdm,xsmc from xs
     27         where xsmc LIKE concat('%',#{name},'%') AND xsdm >#{id}
     28     </select>
     29     <!--多条件链接查询2(使用Map集合)-->
     30     <select id="findUserByParams2" resultType="map">
     31         SELECT xsdm,xsmc from xs
     32         where xsmc LIKE concat('%',#{resultName},'%') AND xsdm >#{resultId}
     33     </select>
     34     <!--多条件链接查询3(使用索引)-->
     35     <select id="findUserByParams3" resultMap="userMap">
     36         SELECT xsdm,xsmc from xs
     37         where xsmc LIKE concat('%',#{0},'%') AND xsdm >#{1}
     38     </select>
     39     <!--if判断和where标签拼接-->
     40     <select id="selectAllUsersByIf" resultMap="userMap">
     41         select xsdm,xsmc from xs
     42         <where>
     43         <if test="name!=null and name!=''">
     44             and xsmc like concat('%',#{name},'%')
     45         </if>
     46             <if test="id!=null and id!=''">
     47               and xsdm>#{id}
     48             </if>
     49         </where>
     50     </select>
     51     <!--choose选择标签(和Java中的switch语句一样的效果)-->
     52     <select id="selectAllUserByChoose" resultMap="userMap">
     53         select xsdm,xsmc from xs
     54         <where>
     55         <choose>
     56             <when test="id>10">
     57                 xsmc like concat('%',#{name},'%');
     58             </when>
     59             <when test="id>60">
     60                 xsdm>30;
     61             </when>
     62             <otherwise>
     63                 xsdm=1
     64             </otherwise>
     65 
     66         </choose>
     67         </where>
     68     </select>
     69     <!--foreach循环遍历数组-->
     70     <select id="selectAllUserByFor" resultMap="userMap">
     71         select * from xs
     72         <if test="array.length>0">
     73             where xsdm in
     74             <foreach collection="array" item="myid" open="(" separator="," close=")">
     75                 #{myid}
     76             </foreach>
     77         </if>
     78     </select>
     79     <!--foreach循环遍历List-->
     80     <select id="selectAllUserByForList" resultMap="userMap">
     81         select * from xs
     82         <if test="list.size>0">
     83             where xsdm in
     84             <foreach collection="list" item="myid" open="(" separator="," close=")">
     85                 #{myid}
     86             </foreach>
     87         </if>
     88     </select>
     89     <!--foreach循环遍历ArraryList集合-->
     90     <insert id="selectAllUserByForListUser" parameterType="java.util.ArrayList" >
     91         insert INTO xs(xsdm,xsmc) VALUES
     92         <if test="list.size>0">
     93             <foreach collection="list" item="user" separator="," index="index">
     94                (#{user.id},#{user.name})
     95             </foreach>
     96         </if>
     97     </insert>
     98     <!--foreach循环遍历Map集合-->
     99     <select id="selectAllUserByForMap" resultType="map">
    100         select * from xs
    101         <if test="map.keys.size>0">
    102             where xsdm in
    103             <foreach collection="map.values" item="user" open="(" separator="," close=")">
    104                 #{user.id}
    105             </foreach>
    106         </if>
    107     </select> 
    属性描述
    id 在命名空间中唯一的标识符,可以被用来引用这条语句。
    parameterType 将会传入这条语句的参数类的完全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过 TypeHandler 推断出具体传入语句的参数,默认值为 unset。
    resultType 从这条语句中返回的期望类型的类的完全限定名或别名。注意如果是集合情形,那应该是集合可以包含的类型,而不能是集合本身。使用 resultType 或 resultMap,但不能同时使用。
    resultMap 外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,对其有一个很好的理解的话,许多复杂映射的情形都能迎刃而解。使用 resultMap 或 resultType,但不能同时使用。
    flushCache 将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false。
    useCache 将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true。
    timeout 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。
    fetchSize 这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset(依赖驱动)。
    statementType STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。
    resultSetType FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为 unset (依赖驱动)。
    databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。
    resultOrdered 这个设置仅针对嵌套结果 select 语句适用:如果为 true,就是假设包含了嵌套结果集或是分组了,这样的话当返回一个主结果行的时候,就不会发生有对前面结果集的引用的情况。这就使得在获取嵌套的结果集的时候不至于导致内存不够用。默认值:false
    resultSets 这个设置仅对多结果集的情况适用,它将列出语句执行后返回的结果集并每个结果集给一个名称,名称是逗号分隔的。

      5.删除语句

        

    1    <!--删除功能  用户传递的是 一个变量! 这时候sql语句中的#{xxx}
    2       xxx只是一个占位符
    3       只有一个参数的时候,可以省略parameterType-->
    4     <delete id="deleteUser" parameterType="int">
    5         delete from xs where xsdm=#{xxx}
    6     </delete>

      

      

    6.修改语句

      

    1 <!--修改 用户传递的是 一个对象!这时候sql语句中的#{属性值}
    2       属性值必须和实体类中的属性一致 -->
    3     <update id="updateUser" parameterType="User">
    4         update   xs  set  xsmc=#{name}
    5         where xsdm=#{name}
    6     </update>
  • 相关阅读:
    SuperMap-iServer-单点登录功能验证(CAS)
    Oracle数据库的链接数目超标
    转载---javascript 定时器总结
    JMeter使用文档
    转载--改变ubuntu默认编码为GBK
    遥感数据下载
    Supermap iCloudManager -负载均衡
    XCode: 如何添加自定义代码片段
    imageNamed和dataWithContentsOfFile的区别(1)
    imageNamed 与 imageWithContentsOfFile的区别
  • 原文地址:https://www.cnblogs.com/BlueSee/p/8359356.html
Copyright © 2011-2022 走看看