Mapper XML 文件

    • 文件内容结构
      • <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        <mapper namespace="cn.test.mapper.UserMapper">
            <insert id="add" parameterType="User">
                insert into stu (name,clsid)
                	value (#{name},#{clsid})
            </insert>
            <update id="update" parameterType="User">
                update stu set name=#{name} ,clsid=#{clsid}
                	where id = #{id}
            </update>
            <delete id="delete" parameterType="int">
                delete from stu where id = #{id}
            </delete>
            <select id="select" parameterType="int" resultType="User">
               select * from stu where id = #{id}
            </select>
        </mapper>

    •  Mapper元素只有一个属性namespace,它有两个作用:
              一 是用于区分不同的mapper
              (在不同的mapper文件里,子元素的id可以相同,mybatis通过namespace和子元素的id联合区分),
              二是与接口关联
              (应用程序通过接口访问mybatis时,mybatis通过接口的完整名称查找对应的mapper配置,因此namespace的命名务必小心一定要某接口同名)


    resultMap – 最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加 载你的对象。
    sql – 可以重用的 SQL 块,也可以被其他语句引用。
    insert – 映射插入语句
    update – 映射更新语句
    delete – 映射删除语句
    select – 映射查询语句

    sql这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。比如

    <sql id="userColumns"> id,username,password </sql>
    Select
    <select id="selectUsers" resultType="map">
      select <include refid="userColumns"/>
      from some_table
      where id = #{id}
    </select>

    Parameters

    <insert id="insertUser" parameterType="User">
      insert into users (id, username, password)
      values (#{id}, #{username}, #{password})
    </insert>

    Result Maps

    <resultMap id="userResultMap" type="User">
      <id property="id" column="user_id" />
      <result property="username" column="username"/>
      <result property="password" column="password"/>
    </resultMap>
    <select id="selectUsers" resultMap="userResultMap">
      select user_id, user_name, hashed_password
      from some_table
      where id = #{id}
    </select>
    
    
    
    
    
    
    
    

ResultMap高级结果映射

<!-- 当数据库中的字段信息和对象的二属性不一致时需要通过resultMap来映射 -->
	    <resultMap type="Address" id="addressMap" atuoMapping="true">
	    	<id column="a_id" property="id"/>
	    	<result column="post_code" property="postcode"/>
	    	<!-- 使用关联 -->
	    	<association property="user" javaType="User">
	    		<id column="user_id" property="id" />
	    		<result column="username" property="username" />
	    		<result column="nickname" property="nickname" />
	    	</association>
	    </returnMap>
	    <select id="load" parameterType="int" resultMap="addressMap">
	    	select * ,t1.id as 'a_id' from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id = 1;
	    </select>


    • 1
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值