美文网首页
mybatis-1-UserDao.xml 范例

mybatis-1-UserDao.xml 范例

作者: blank_white | 来源:发表于2020-06-22 19:30 被阅读0次
    <?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="org.example.dao.UserDao">
    
    
    <!--    使用二级缓存-->
        <cache></cache>
    
        <select id="findAll2" resultType="User">
            select * from user;
        </select>
    
        <select id="findAll" resultType="org.example.domain.User" useCache="true">
            select * from user;
        </select>
    
        <insert id="insertUser" parameterType="org.example.domain.User">
            insert into user(username,birthday,sex,address)
            values(#{username},#{birthday},#{sex},#{address});
            <selectKey resultType="int" keyProperty="id" keyColumn="id" order="AFTER">
                select last_insert_id();
            </selectKey>
        </insert>
    
        <update id="upDateUser" parameterType="org.example.domain.User">
            update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}
            where id=#{id};
        </update>
    
        <delete id="delUser" parameterType="int">
            delete from user where id=#{id};
        </delete>
    
        <select id="findUser" parameterType="int" resultType="org.example.domain.User">
            select * from user where id=#{id};
        </select>
    
        <select id="findUserByName" parameterType="String" resultType="org.example.domain.User">
            select * from user where username like #{id};
        </select>
    
        <resultMap id="userVo" type="org.example.domain.ResultVo">
            <id property="userId" column="id"></id>
            <result property="userSex" column="sex"></result>
            <result property="userBirthday" column="birthday"></result>
        </resultMap>
        <select id="findUserByQueryVo" parameterType="org.example.domain.QueryVo" resultMap="userVo">
            select * from user where id=#{user.id};
        </select>
    
        <select id="findUserByCondition" parameterType="QueryVo" resultType="user">
            select * from user
            <where>
                <if test="user.username!=null" >
                    and username=#{user.username}
                </if>
                <if test="user.sex!=null">
                    and sex=#{user.sex}
                </if>
            </where>
        </select>
    
        <sql id="selectUser" >
            select * from user
        </sql>
    
    
        <select id="findUserByIds" resultType="user" parameterType="QueryVo">
            <include refid="selectUser"></include>
            <where>
                <if test="list!=null">
                    <foreach collection="list" open="id in(" close=")" separator="," item="id">
                        #{id}
                    </foreach>
                </if>
            </where>
        </select>
    
    
    
        <resultMap id="userroles" type="User" autoMapping="true">
            <id property="id" column="id"></id>
            <collection property="roles" ofType="Role" autoMapping="true">
                <id property="id" column="rid"></id>
            </collection>
        </resultMap>
    
        <select id="findAllwithRoles" resultMap="userroles" >
            select u.*,r.id as rid,r.rolename
            from user u
            left join user_role ur
            on u.id=ur.uid
            left join role r
            on ur.rid=r.id
        </select>
    
    
        <resultMap id="useraccount" type="User" autoMapping="true">
            <id property="id" column="id" ></id>
            <collection property="accounts" column="id" ofType="Account" select="org.example.dao.AccountDao.findAccountByUid" >
                <id property="id" column="id"></id>
            </collection>
        </resultMap>
    
        <select id="findAllwithAccountLazy" resultMap="useraccount" >
            select * from user
        </select>
    
    </mapper>
    
    
    
    package org.example.dao;
    
    import org.example.domain.QueryVo;
    import org.example.domain.ResultVo;
    import org.example.domain.User;
    
    import java.util.List;
    
    public interface UserDao {
    
        List<User> findAll();
    
        List<User> findAll2();
    
        int insertUser(User user);
    
        void upDateUser(User user);
    
        void delUser(int id);
    
        User findUser(int id);
    
        List<User> findUserByName(String name);
    
        ResultVo findUserByQueryVo(QueryVo queryVo);
    
        List<User> findUserByCondition(QueryVo vo);
    
        List<User> findUserByIds(QueryVo vo);
    
    
        List<User> findAllwithRoles();
    
        List<User> findAllwithAccountLazy();
    
    }
    
    
    <?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="org.example.dao.AccountDao">
    
        <resultMap id="user" type="User">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="birthday" column="birthday"></result>
        </resultMap>
    
        <resultMap id="AccountUser" type="Account">
            <id property="id" column="aid"></id>
            <result property="uid" column="uid"></result>
            <result property="money" column="money"></result>
            <association property="user" resultMap="user" >
            </association>
        </resultMap>
    
        <select id="findAll" resultMap="AccountUser" >
    --         select user.*,account.uid,account.money from account,user where account.id=user.id;
            select u.*,a.id as aid ,a.uid,a.money from account a,user u where a.uid=u.id;
    
        </select>
    
        <select id="findAccountByUid" resultType="Account" parameterType="int">
            select * from account where uid=#{uid}
        </select>
        
    </mapper>
    

    相关文章

      网友评论

          本文标题:mybatis-1-UserDao.xml 范例

          本文链接:https://www.haomeiwen.com/subject/lovowhtx.html