雪洗中关村

导航

mybatis映射文件mapper.xml的写法(collections...)

转自:https://blog.csdn.net/two_people/article/details/51759881

在学习mybatis的时候我们通常会在映射文件这样写:

<?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="com.qbd.mapper.StudentMappers">
    <select id="findbyid" parameterType="Integer" resultMap="StudentResult">
        select *from student where id=#{id}
    </select>
    
    <select id="findbygradeid" parameterType="Integer" resultMap="StudentResult">
        select *from student where gid=#{gid}
    </select>
    
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addid" select="com.qbd.mapper.AddressMappers.findbyid">    
        </association>
        <association property="grade" column="gid" select="com.qbd.mapper.GradeMappers.findbyid">    
        </association>
    </resultMap>
</mapper>

来读取配置文件

另一种方法就是:

直接在mapper.xml中的这一部分写成dao<mapper namespace="com.qbd.mapper.StudentMappers">如下

<?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="com.qbd.ssm.dao.UserDao">
    <!-- 定义缓存  一般是一级缓存,如果用同一个sqlsession 那么相同查询直接会从缓存中查找 
    <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"></cache>
    -->
    <!-- 查找所有 -->
    <select id="find" parameterType="Map" resultMap="StudentResult">
        select * from user
        <where>
            <if test="uname!=null and uname!='' ">
                and uname like #{uname}
            </if>
        </where>
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>
    
    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from user
        <where>
            <if test="uname!=null and uname!='' ">
                and uname like #{uname}
            </if>
        </where>
    </select>
    <!-- 按照用户名和密码查找 -->
    <select id="getUser" resultMap="StudentResult" parameterType="Map">
        select *from user where uname=#{uname} and upassword=#{upassword}
    </select>
    <!-- 删除 -->
    <delete id="delete" parameterType="Map">
        delete from user where uid=#{uid}
    </delete>
    <!-- 修改 -->
    <update id="update" parameterType="User">
        update user
        <set>
            <if test="uname!=null">
                 uname=#{uname},
            </if>
            <if test="upassword!=null">
                upassword=#{upassword},
            </if>
            <if test="upower!=null">
                upower=#{upower},
            </if>
        </set>
        where uid=#{uid}
    </update>
    <!-- 增加 -->
    <insert id="add" parameterType="User">
        insert into user values(null,#{uname},#{upassword},#{upower})
    </insert>
    <resultMap type="User" id="StudentResult">
        <id property="uid" column="uid"/>
        <result property="uname" column="uname"/>
        <result property="upassword" column="upassword"/>
    </resultMap>
</mapper>

那么就不用写dao的实现在service中就能掉用       mybatis默认会把mapper.xml映射为dao的实现

 

posted on 2018-10-08 17:11  雪洗中关村  阅读(18315)  评论(0编辑  收藏  举报