mybatis 詳解(六)------通過mapper接口加載映射文件

分類:IT技術 時間:2017-08-10

  通過 mapper 接口加載映射文件,這對於後面 ssm三大框架 的整合是非常重要的。那麽什麽是通過 mapper 接口加載映射文件呢?

  我們首先看以前的做法,在全局配置文件 mybatis-configuration.xml 通過 <mappers> 標簽來加載映射文件,那麽如果我們項目足夠大,有很多映射文件呢,難道我們每一個映射文件都這樣加載嗎,這樣肯定是不行的,那麽我們就需要使用 mapper 接口來加載映射文件

  以前的做法:

  

  改進做法:使用 mapper 接口來加載映射文件

1、定義 userMapper 接口

package com.ys.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.update;

import com.ys.po.User;

public interface UserMapper {
	//根據 id 查詢 user 表數據
	public User selectUserById(int id) throws Exception;

	//向 user 表插入一條數據
	public void insertUser(User user) throws Exception;
	
	//根據 id 修改 user 表數據
	public void updateUserById(User user) throws Exception;
	
	//根據 id 刪除 user 表數據
	public void deleteUserById(int id) throws Exception;
}

 

2、在全局配置文件 mybatis-configuration.xml 文件中加載 UserMapper 接口(單個加載映射文件)

 

3、編寫UserMapper.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="com.ys.mapper.UserMapper">

	
 	<!-- 根據 id 查詢 user 表中的數據
  	   id:唯一標識符,此文件中的id值不能重復
  	   resultType:返回值類型,一條數據庫記錄也就對應實體類的一個對象
  	   parameterType:參數類型,也就是查詢條件的類型
	-->
	<select id="selectUserById" 
  			resultType="com.ys.po.User" parameterType="int">
  		<!-- 這裏和普通的sql 查詢語句差不多,後面的 #{id}表示占位符,裏面不一定要寫id,寫啥都可以,但是不要空著 -->
    	select * from user where id = #{id1}
	</select>
	
	
	
	<!-- 根據 id 更新 user 表的數據 -->
  	<update id="updateUserById" parameterType="com.ys.po.User">
  		update user u
  			<!-- <set>
  				<if test="username != null and username != ''">
  					u.username = #{username},
  				</if>
  				<if test="sex != null and sex != ''">
  					u.sex = #{sex}
  				</if>
  			</set> -->
  			<trim prefix="set" suffixOverrides=",">
  				<if test="username != null and username != ''">
  					u.username = #{username},
  				</if>
  				<if test="sex != null and sex != ''">
  					u.sex = #{sex},
  				</if>
  			</trim>
  		
  		 where id=#{id}
  	</update>
  	
  	
  	<!-- 向 user 表插入一條數據 -->
  	<insert id="insertUser" parameterType="com.ys.po.User">
  		<!-- 將插入的數據主鍵返回到 user 對象中
  			 keyProperty:將查詢到的主鍵設置到parameterType 指定到對象的那個屬性
  			 select LAST_INSERT_ID():查詢上一次執行insert 操作返回的主鍵id值,只適用於自增主鍵
  			 resultType:指定 select LAST_INSERT_ID() 的結果類型
  			 order:AFTER,相對於 select LAST_INSERT_ID()操作的順序
  		 -->
  		<selectKey keyProperty="id" resultType="int" order="AFTER">
  			select LAST_INSERT_ID() 
  		</selectKey>
  		insert into user(username,sex,birthday,address)
  			value(#{username},#{sex},#{birthday},#{address})
  	</insert>
  	
  	
  	
  	<!-- 根據 id 刪除 user 表的數據 -->
  	<delete id="deleteUserById" parameterType="int">
  		delete from user where id=#{id}
  	</delete>
  	
</mapper>

  

4、測試

	//根據id查詢user表數據
	@Test
	public void testSelectUserById(){
		/*這個字符串由 userMapper.xml 文件中 兩個部分構成
			<mapper namespace="com.ys.po.userMapper"> 的 namespace 的值
			<select id="selectUserById" > id 值*/
		String statement = "com.ys.mapper.UserMapper.selectUserById";
		User user = session.selectOne(statement, 1);
		system.out.println(user);
		session.close();
	}

 

 

5、批量加載映射文件

  <mappers>
         <!--批量加載mapper
         	指定 mapper 接口的包名,mybatis自動掃描包下的mapper接口進行加載
           -->
         <package name="com.ys.mapper"/>
  </mappers>

  

 

6、註意 

  1、UserMapper 接口必須要和 UserMapper.xml 文件同名且在同一個包下,也就是說 UserMapper.xml 文件中的namespace是UserMapper接口的全類名

  

 

  2、UserMapper接口中的方法名和 UserMapper.xml 文件中定義的 id 一致

 

  3、UserMapper接口輸入參數類型要和 UserMapper.xml 中定義的 parameterType 一致

 

  4、UserMapper接口返回數據類型要和 UserMapper.xml 中定義的 resultType 一致


Tags: 文件 映射 加載 接口 mapper annotations

文章來源:


ads
ads

相關文章
ads

相關文章

ad