zoukankan      html  css  js  c++  java
  • mybatis mapper.xml

      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 
      6 
      7 <!-- 1. namespace必需是接口的全路径名 -->
      8 <!-- 2. 接口的方法名必需与映射文件的sql id一致 -->
      9 <!-- 3. 接口的输入参数必需与映射文件的parameterType类型一致 -->
     10 <!-- 4. 接口的返回类型必须与映射文件的resultType类型一致 -->
     11 
     12 
     13 <!-- 1. 创建UserMapper.xml映射文件(把原来的user.xml复制按开发规则要求修改一下) -->
     14 <!-- 2. 创建UserMapper接口(把原来的UserDao.java复制按开发规则要求修改一下) -->
     15 <!-- 3. 加载UserMapper.xml -->
     16 
     17 
     18 <mapper namespace="mybatis.studentMapper">
     19     <select id="getUserById" parameterType="int"
     20         resultType="cn.edu.ahtcm.student">
     21         SELECT * FROM STUDENT WHERE SID = #{id1}
     22 
     23     </select>
     24 
     25     <!-- 程序没有报错,但是后台数据库的记录也没有加进去 -->
     26     <!-- 没有加进去的原因是没有提交事务 -->
     27     <!-- 返回值被放在传入参数的属性里,而不是返回值,返回值返回的是影响的行数 -->
     28     <insert id="insertStudent" parameterType="cn.edu.ahtcm.student"
     29         useGeneratedKeys="false" keyProperty="id">
     30 
     31         <selectKey keyProperty="sid" resultType="String"
     32             order="AFTER">
     33             <!-- SELECT LAST_INSERT_ID() -->
     34             <!-- SELECT UUID() -->
     35         </selectKey>
     36         INSERT INTO `S`(SNAME,GENDER,CLASS_ID)
     37         VALUES(#{sname},#{gender},#{class_id})
     38     </insert>
     39 
     40     <!-- 修改 -->
     41     <update id="updateBySID" parameterType="cn.edu.ahtcm.student">
     42 
     43         UPDATE STUDENT SET
     44         SNAME=#{sname} WHERE SID=#{sid}
     45     </update>
     46 
     47     <!-- 删除 -->
     48 
     49     <delete id="deleteBySID" parameterType="cn.edu.ahtcm.student">
     50         DELETE FROM STUDENT WHERE
     51         SID =#{sid}
     52     </delete>
     53     <!-- 查询结果为列表,返回类型定位单个元素类型即可 -->
     54     <select id="getAllStudent" parameterType="cn.edu.ahtcm.student"
     55         resultType="cn.edu.ahtcm.student">
     56         SELECT * FROM STUDENT
     57         <include refid="sql"></include>
     58 
     59     </select>
     60 
     61 
     62     <!-- 定义sql片段,可以通过include的id使用SQL片段 -->
     63     <sql id="sql">
     64         WHERE SID BETWEEN 0 AND 10
     65     </sql>
     66 
     67 
     68     <!-- foreach可以读取数组或列表自动生成 in ()的sql语法 -->
     69     <select id="getUserBySIds" parameterType="queryvo"
     70         resultType="cn.edu.ahtcm.student">
     71 
     72         SELECT
     73         *
     74         FROM USER
     75         <!-- where会自动加上where同处理多余的and -->
     76         <where>
     77             <!-- id IN(1,10,25,30,34) -->
     78             <!-- foreach循环标签 collection:要遍历的集合,来源入参 open:循环开始前的sql separator:分隔符 close:循环结束拼接的sql -->
     79             <foreach item="uid" collection="ids" open="id IN("
     80                 separator="," close=")">
     81                 #{uid}
     82             </foreach>
     83         </where>
     84     </select>
     85 
     86 
     87     <!-- 一对一的关系映射 -->
     88     <!-- 1.resultType的返回映射,映射至设计好的类(在继承原来的类,并增加需要的属性),在SQL语句是是用联接 -->
     89     <!-- 2.使用resultMap在xml内设计映射规则(继承原来的类,添加关系映射的引用) -->
     90 
     91     <!-- resultMap -->
     92     <!-- type:映射成的pojo类型 -->
     93     <!-- id:resultMap唯一标识 -->
     94 
     95     <resultMap type="cn.edu.ahtcm.student" id="studentMap">
     96         <!-- id标签用于绑定主键 -->
     97         <!-- <id property="id" column="id"/> -->
     98 
     99         <!-- 使用result绑定普通字段 -->
    100         <result property="userId" column="user_id" />
    101         <result property="number" column="number" />
    102         <result property="createtime" column="createtime" />
    103         <result property="note" column="note" />
    104 
    105         <!-- association:配置一对一关联 -->
    106         <!-- property:绑定的用户属性 -->
    107         <!-- javaType:属性数据类型,支持别名 -->
    108         <!-- 使用时需要在resultMap 的type指定的类中包含下列association中JavaType指定的类的引用 -->
    109         <association property="user"
    110             javaType="cn.edu.ahtcm.student">
    111             <id property="id" column="user_id" />
    112 
    113             <result property="username" column="username" />
    114             <result property="address" column="address" />
    115             <result property="sex" column="sex" />
    116         </association>
    117 
    118         <!-- collection:配置一对多关系 -->
    119         <!-- property:列表的属性名 -->
    120         <!-- ofType:property的数据类型,支持别名 -->
    121         <!-- 使用时需要在resultMap 的type指定的类中包含下列collection中ofType指定的类的列表的引用 -->
    122         <collection property="orders" ofType="order">
    123             <!-- id标签用于绑定主键 -->
    124             <id property="id" column="oid" />
    125             <!-- 使用result绑定普通字段 -->
    126             <result property="userId" column="id" />
    127             <result property="number" column="number" />
    128             <result property="createtime" column="createtime" />
    129         </collection>
    130 
    131 
    132     </resultMap>
    133 
    134     <!-- 使用resultMap -->
    135     <select id="getOrderListResultMap" resultMap="orderMap">
    136         SELECT * FROM
    137         `student`
    138     </select>
    139 
    140 
    141 </mapper>

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <!-- 1. namespace必需是接口的全路径名 --><!-- 2. 接口的方法名必需与映射文件的sql id一致 --><!-- 3. 接口的输入参数必需与映射文件的parameterType类型一致 --><!-- 4. 接口的返回类型必须与映射文件的resultType类型一致 -->

    <!-- 1. 创建UserMapper.xml映射文件(把原来的user.xml复制按开发规则要求修改一下) --><!-- 2. 创建UserMapper接口(把原来的UserDao.java复制按开发规则要求修改一下) --><!-- 3. 加载UserMapper.xml -->

    <mapper namespace="mybatis.studentMapper"><select id="getUserById" parameterType="int"resultType="cn.edu.ahtcm.student">SELECT * FROM STUDENT WHERE SID = #{id1}
    </select>
    <!-- 程序没有报错,但是后台数据库的记录也没有加进去 --><!-- 没有加进去的原因是没有提交事务 --><!-- 返回值被放在传入参数的属性里,而不是返回值,返回值返回的是影响的行数 --><insert id="insertStudent" parameterType="cn.edu.ahtcm.student"useGeneratedKeys="false" keyProperty="id">
    <selectKey keyProperty="sid" resultType="String"order="AFTER"><!-- SELECT LAST_INSERT_ID() --><!-- SELECT UUID() --></selectKey>INSERT INTO `S`(SNAME,GENDER,CLASS_ID)VALUES(#{sname},#{gender},#{class_id})</insert>
    <!-- 修改 --><update id="updateBySID" parameterType="cn.edu.ahtcm.student">
    UPDATE STUDENT SETSNAME=#{sname} WHERE SID=#{sid}</update>
    <!-- 删除 -->
    <delete id="deleteBySID" parameterType="cn.edu.ahtcm.student">DELETE FROM STUDENT WHERESID =#{sid}</delete><!-- 查询结果为列表,返回类型定位单个元素类型即可 --><select id="getAllStudent" parameterType="cn.edu.ahtcm.student"resultType="cn.edu.ahtcm.student">SELECT * FROM STUDENT<include refid="sql"></include>
    </select>

    <!-- 定义sql片段,可以通过include的id使用SQL片段 --><sql id="sql">WHERE SID BETWEEN 0 AND 10</sql>

    <!-- foreach可以读取数组或列表自动生成 in ()的sql语法 --><select id="getUserBySIds" parameterType="queryvo"resultType="cn.edu.ahtcm.student">
    SELECT*FROM USER<!-- where会自动加上where同处理多余的and --><where><!-- id IN(1,10,25,30,34) --><!-- foreach循环标签 collection:要遍历的集合,来源入参 open:循环开始前的sql separator:分隔符 close:循环结束拼接的sql --><foreach item="uid" collection="ids" open="id IN("separator="," close=")">#{uid}</foreach></where></select>

    <!-- 一对一的关系映射 --><!-- 1.resultType的返回映射,映射至设计好的类(在继承原来的类,并增加需要的属性),在SQL语句是是用联接 --><!-- 2.使用resultMap在xml内设计映射规则(继承原来的类,添加关系映射的引用) -->
    <!-- resultMap --><!-- type:映射成的pojo类型 --><!-- id:resultMap唯一标识 -->
    <resultMap type="cn.edu.ahtcm.student" id="studentMap"><!-- id标签用于绑定主键 --><!-- <id property="id" column="id"/> -->
    <!-- 使用result绑定普通字段 --><result property="userId" column="user_id" /><result property="number" column="number" /><result property="createtime" column="createtime" /><result property="note" column="note" />
    <!-- association:配置一对一关联 --><!-- property:绑定的用户属性 --><!-- javaType:属性数据类型,支持别名 --><!-- 使用时需要在resultMap 的type指定的类中包含下列association中JavaType指定的类的引用 --><association property="user"javaType="cn.edu.ahtcm.student"><id property="id" column="user_id" />
    <result property="username" column="username" /><result property="address" column="address" /><result property="sex" column="sex" /></association>
    <!-- collection:配置一对多关系 --><!-- property:列表的属性名 --><!-- ofType:property的数据类型,支持别名 --><!-- 使用时需要在resultMap 的type指定的类中包含下列collection中ofType指定的类的列表的引用 --><collection property="orders" ofType="order"><!-- id标签用于绑定主键 --><id property="id" column="oid" /><!-- 使用result绑定普通字段 --><result property="userId" column="id" /><result property="number" column="number" /><result property="createtime" column="createtime" /></collection>

    </resultMap>
    <!-- 使用resultMap --><select id="getOrderListResultMap" resultMap="orderMap">SELECT * FROM`student`</select>

    </mapper>

  • 相关阅读:
    模块化工具require 学习笔记
    学习Jade模板引擎
    通过border来实现各种三角符号
    使用vscode 编译 sass
    Javascript 运行机制
    Vue调试工具 vue-devtools
    MVVM框架
    通信类
    面向对象
    原型和原型链
  • 原文地址:https://www.cnblogs.com/yrxc/p/13469694.html
Copyright © 2011-2022 走看看