mybatis 非註解開發配置

mybatis 非註解開發,常用配置

實體類字段:userId,userName,userBirthday,userSex,userAddress
表字段:id,username,birthday,sex,address

mybatis實體類的屬性名(如在列名前面加上user)和數據庫表的列名對應不上

解決方案:
1、SQL語句方案:起別名 執行效率最高(開發效率低)

	<!--查詢所有用戶-->
    <select id="findAllUser" resultType="cn.td.domain.User">
      select id as userId,username as userName,birthday as userBirthday,sex as userSex,address as userAddress from user;
    </select>

2、mybatis提供的解決方案:配置resultMap(查詢結果集與實體類的對應關係)

<resultMap id="userMap" type="cn.td.domain.Use">
   <!--首先配置主鍵的對應關係-->
    <id property="userId" column="id"></id>
    非主鍵字段的對應關係
    <result property="userName" column="username"/>
    <result property="userBirthday" column="birthday"/>
    <result property="userSex" column="sex"/>
    <result property="userAddress" column="address"/>
</resultMap>
<!--使用resultMap來指定對應關係-->
<select id="findAllUser" resultMap="userMap">
    select * from user;
</select>

mybatis動態SOL

1 、抽取重複SQL語句

<!--抽取重複SQL語句-->
    <sql id="selectDefault">
        select id,username,birthday,sex,address from user
    </sql>

2、if 標籤

<!--根據條件查詢-->
<select id="findUserByCondition" parameterType="cn.td.domain.User" resultType="cn.td.domain.User">
   <!--引入抽取的重複SQL語句-->
    <include refid="selectDefault"/>
    <where>
        <!--if 標籤中test的值是parameterType的屬性值-->
        <if test="username != null">
            and username = #{username}
        </if>
    </where>
</select>

3、foreach標籤

<!--根據queryVo中提供的id集合查詢用戶信息-->
<select id="findUserInIds" parameterType="cn.td.domain.QueryVo" resultType="cn.td.domain.User">
    <!--引入sql語句-->
    <include refid="selectDefault"/>
    <where>
        <if test="ids!=null and ids.size()>0">
        	 <!--
                collection:parameterType中提供的集合屬性名
                open:表示該語句以什麼開始 close:表示該語句以什麼結束
                item:表示本次迭代獲取的元素,若collection爲List、Set或者數組,則表示其中的元素;若collection爲map,則代表key-value的value,該參數爲必選
                separator:每次迭代後給sql語句加上的分割符
              -->
            <foreach collection="ids" open="and id in(" close=")" item="uid" separator=",">
                #{uid}
            </foreach>
        </if>
    </where>
</select>

mybatis多表操作

user實體類字段:id,username,birthday,sex,address
user表字段:id,username,birthday,sex,address
accout實體類字段:id,uid,money
accout表字段:ID,UID,MONEY

一對一操作

查詢所有賬戶(Account)幷包含用戶(User)名稱和地址信息
1、實現一個Account實體類的子類(不常用)

AccountUser extends Account

mybatis映射文件配置

<!--通過Account子類實現多表查詢(不常用) 查詢所有賬戶幷包含用戶名稱和地址信息 -->
<select id="findAllAccountContainsUser" resultType="cn.td.domain.AccountUser">
    SELECT account.*,user.`username`,user.`address` FROM account JOIN user ON account.`UID`=user.`id`
</select>

2、從表實體包含主表實體的對象引用實現多表查詢

public class Account implements Serializable {
    private static final long serialVersionUID = 1362784392528572971L;
    private int id;
    private int uid;
    private double money;

    /**
     * 一對一關係映射:從表實體包含主表實體的對象引用實現多表查詢
     */
    private User user;
    .......
}

mybatis映射文件配置

 <!--定義封裝account和user的resultMap-->
 <resultMap id="accountUserMap" type="cn.td.domain.Account">
     <id property="id" column="ID"/>
     <result property="uid" column="UID"/>
     <result property="money" column="MONEY"/>
     <!--一對一的關係映射:配置封裝user的內容-->
     <association property="user" column="uid">
         <id property="id" column="id"></id>
         <!-- 非主鍵字段的對應關係-->
         <result property="username" column="username"/>
         <result property="birthday" column="birthday"/>
         <result property="sex" column="sex"/>
         <result property="address" column="address"/>
     </association>
 </resultMap>
 <select id="findAllAccount" resultMap="accountUserMap">
      <!--引入sql語句-->
      SELECT account.*,user.`username`,user.`address` FROM account JOIN user ON account.`UID`=user.`id`
 </select>

一對多操作

主表實體包含從表實體的集合引用

public class User implements Serializable {
    private static final long serialVersionUID = -1052198782505629130L;
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    /**
     * 一對多關係映射:主表實體包含從表實體的集合引用
     */
    private List<Account> Accounts;
    .......
}

mybatis映射文件配置

<!--定義user的resultMap-->
<resultMap id="userAccountMap" type="cn.td.domain.User">
    <id property="id" column="id"></id>
    <!-- 非主鍵字段的對應關係-->
    <result property="username" column="username"/>
    <result property="birthday" column="birthday"/>
    <result property="sex" column="sex"/>
    <result property="address" column="address"/>
    <!--配置user對象中accounts集合的映射-->
    <!--property:user中集合屬性名。ofType:集合中元素的類型-->
    <collection property="accounts" ofType="cn.td.domain.Account">
        <id property="id" column="ID"/>
        <result property="uid" column="UID"/>
        <result property="money" column="MONEY"/>
    </collection>
</resultMap>
<!--查詢所有用戶-->
<select id="findAllUser" resultMap="userAccountMap">
    SELECT `user`.*,account.`ID`,account.`MONEY` FROM `user` LEFT JOIN account ON user.`id`=account.`UID`
</select>

多對多操作

user實體類字段:id,username,birthday,sex,address
user表字段:id,username,birthday,sex,address
role實體類字段:id,roleName,roleDesc
role表字段:ID,ROLE_NAME,ROLE_DESC
中間表 user_role 字段:UID RID

根據角色查詢用戶
角色表實體類

public class Role implements Serializable {
    private static final long serialVersionUID = -5862141085802279565L;
    private Integer id;
    private String roleName;
    private String roleDesc;
    //關係映射:主表實體包含從表實體的集合引用
    private List<User> userList;
.......
}

mybatis映射文件配置

<!--定義user的resultMap-->
<resultMap id="roleMap" type="cn.td.domain.Role">
    <id property="id" column="ID"></id>
    <!-- 非主鍵字段的對應關係-->
    <result property="roleName" column="ROLE_NAME"/>
    <result property="roleDesc" column="ROLE_DESC"/>
    <collection property="userList" ofType="cn.td.domain.User">
        <id property="id" column="id"></id>
        <!-- 非主鍵字段的對應關係-->
        <result property="username" column="username"/>
        <result property="birthday" column="birthday"/>
        <result property="sex" column="sex"/>
        <result property="address" column="address"/>
    </collection>
</resultMap>
<!--查詢所有角色-->
<select id="findAllRole" resultMap="roleMap">
  SELECT
  `role`.*,
  `user`.*
  FROM
  `role`
  LEFT OUTER JOIN user_role ON `role`.`ID` = user_role.`RID`
  LEFT OUTER JOIN `user` ON `user`.`id` = user_role.`UID`
</select>

根據用戶查詢角色
用戶表實體類

public class User implements Serializable {
    private static final long serialVersionUID = 1497355656125086674L;
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
     //關係映射:主表實體包含從表實體的集合引用
    private List<Role> roleList;
......
}

mybatis映射文件配置

<resultMap id="userMap" type="cn.td.domain.User">
    <id property="id" column="id"></id>
    <!-- 非主鍵字段的對應關係-->
    <result property="username" column="username"/>
    <result property="birthday" column="birthday"/>
    <result property="sex" column="sex"/>
    <result property="address" column="address"/>
    <collection property="roleList" ofType="cn.td.domain.Role">
        <id property="id" column="ID"></id>
        <!-- 非主鍵字段的對應關係-->
        <result property="roleName" column="ROLE_NAME"/>
        <result property="roleDesc" column="ROLE_DESC"/>
    </collection>
</resultMap>
<!--查詢所有用戶-->
<select id="findAllUser" resultMap="userMap">
    SELECT
    `user`.*,
    `role`.*
    FROM
    `user`
    LEFT OUTER JOIN user_role ON `user`.`id` = user_role.`UID`
    LEFT OUTER JOIN `role` ON `role`.`ID` = user_role.`RID`
</select>

延遲加載

user實體類字段:id,username,birthday,sex,address
user表字段:id,username,birthday,sex,address
accout實體類字段:id,uid,money
accout表字段:ID,UID,MONEY

通過用戶id查找賬戶信息

userDao.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.td.dao.UserDao">
	<!--定義user的resultMap-->
	<resultMap id="userAccountMap" type="cn.td.domain.User">
	    <id property="id" column="id"></id>
	    <!-- 非主鍵字段的對應關係-->
	    <result property="username" column="username"/>
	    <result property="birthday" column="birthday"/>
	    <result property="sex" column="sex"/>
	    <result property="address" column="address"/>
	    <!--一對多配置user對象中accounts集合的映射-->
	    <!--property:user中集合屬性名。ofType:集合中元素的類型-->
	    <collection property="accounts" column="id" ofType="cn.td.domain.Account" select="cn.td.dao.AccountDao.findAccountById" />
	</resultMap>
	
	<!--查詢所有用戶-->
	<select id="findAllUser" resultMap="userAccountMap">
	    select * from user
	</select>
</mapper>

accountDao.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.td.dao.AccountDao">
    <!--根據用戶id查詢賬戶信息-->
    <select id="findAccountById" parameterType="Integer" resultType="cn.td.domain.Account">
        SELECT * FROM account where uid = #{uid}
    </select>
</mapper>

通過賬戶UID查找用戶

accountDao.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.td.dao.AccountDao">
    <!--定義封裝account和user的resultMap-->
    <resultMap id="accountUserMap" type="cn.td.domain.Account">
        <id property="id" column="ID"/>
        <result property="uid" column="UID"/>
        <result property="money" column="MONEY"/>
        <!--一對一的關係映射:配置封裝user的內容-->
        <!--select屬性:查詢用戶的唯一標識-->
        <association property="user" column="uid" javaType="cn.td.domain.User" select="cn.td.dao.UserDao.findUserById"/>
    </resultMap>
    <!--查詢所有用戶-->
    <select id="findAllAccount" resultMap="accountUserMap">
        select * from account
    </select>
</mapper>

userDao.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.td.dao.UserDao">
    <!--通過用戶名查詢用戶-->
    <select id="findUserById" parameterType="Integer" resultType="cn.td.domain.User">
       select * from user where id = #{id};
    </select>
</mapper>

開啓二級緩存

在要開啓二級緩存的dao.xml 配置 標籤,在要開啓的方法上添加useCache="true"屬性

<?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.td.dao.UserDao">
    <!--開啓二級緩存-->
    <cache/>
    <!--通過用戶名查詢用戶 useCache="true":開啓二級緩存-->
    <select useCache="true"  id="findUserById" parameterType="Integer" resultType="cn.td.domain.User">
        SELECT * FROM `user` where id = #{id};
    </select>
</mapper>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章