天天看點

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

通過 mapper 接口加載映射檔案,這對于後面 ssm三大架構 的整合是非常重要的。那麼什麼是通過 mapper 接口加載映射檔案呢?

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

  以前的做法:

  

mybatis 詳解(六)------通過mapper接口加載映射檔案
  改進做法:使用 mapper 接口來加載映射檔案

1、定義 userMapper 接口

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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 接口(單個加載映射檔案)

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

3、編寫UserMapper.xml 檔案

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<?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()

throws

Exception{

//擷取mapper接口

UserMapper userMapper = session.getMapper(UserMapper.

class

);

User user = userMapper.selectUserById(

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接口的全類名

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

  2、UserMapper接口中的方法名和 UserMapper.xml 檔案中定義的 id 一緻

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

  4、UserMapper接口傳回資料類型要和 UserMapper.xml 中定義的 resultType 一緻

繼續閱讀