一、Mapper接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package
com.liu.mapper;
import
java.util.Map;
import
org.apache.ibatis.annotations.Param;
import
com.liu.po.UserInfo;
public
interface
UserInfoMapper {
/**
* 如果一个参数 是通过类型匹配
* 多个参数必须指定参数名称
* @param userName
* @param userSex
* @return
*/
public
int
addUser1(
@Param
(
"name"
)String userName,
@Param
(
"userSex"
)String userSex);
/**
* 通过对象传递参数
* @param user
* @return
*/
public
int
addUser2(UserInfo user);
/**
* 通过map的方式传递参数
* @param map
* @return
*/
public
int
addUser3(Map<String,Object> map);
}
|
二、Mapper XML 映射文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?
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.liu.mapper.UserInfoMapper"
>
<!-- property对应实体类属性 column 对应着数据库字段 -->
<
resultMap
type
=
"UserInfo"
id
=
"userMap"
>
<
result
property
=
"userId"
column
=
"user_id"
></
result
>
<
result
property
=
"userName"
column
=
"user_name"
></
result
>
<
result
property
=
"userSex"
column
=
"user_sex"
></
result
>
</
resultMap
>
<!-- 添加 -->
<
insert
id
=
"addUser1"
parameterType
=
"String"
>
insert into user_info (user_name,user_sex) values(#{name},#{userSex})
</
insert
>
<!-- 添加 -->
<
insert
id
=
"addUser2"
parameterType
=
"UserInfo"
>
insert into user_info (user_name,user_sex) values(#{userName},#{userSex})
</
insert
>
<!-- 添加 -->
<
insert
id
=
"addUser3"
parameterType
=
"map"
>
insert into user_info (user_name,user_sex) values(#{name},#{sex})
</
insert
>
</
mapper
>
|
三、单元测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
|
package
com.liu;
import
java.util.HashMap;
import
java.util.Map;
import
org.apache.ibatis.session.SqlSession;
import
org.junit.After;
import
org.junit.Before;
import
org.junit.Test;
import
com.liu.mapper.UserInfoMapper;
import
com.liu.po.UserInfo;
import
com.liu.utils.DBFactory;
public
class
UserTest {
SqlSession session =
null
;
UserInfoMapper mapper =
null
;
@Before
public
void
testBefore() {
session = DBFactory.getSession();
mapper = session.getMapper(UserInfoMapper.
class
);
}
@After
public
void
testAfter() {
session.close();
}
@Test
public
void
addUser1(){
int
count = mapper.addUser1(
"金蝉子"
,
"男"
);
if
(count>
0
){
System.out.println(
"成功"
);
session.commit();
}
}
@Test
public
void
addUser2(){
UserInfo user =
new
UserInfo();
user.setUserName(
"白龙马"
);
user.setUserSex(
"不详"
);
int
count = mapper.addUser2(user);
if
(count>
0
){
System.out.println(
"成功"
);
session.commit();
}
}
@Test
public
void
addUser3(){
Map<String,Object> map =
new
HashMap<String, Object>();
map.put(
"name"
,
"无尽"
);
map.put(
"sex"
,
"不详"
);
int
count = mapper.addUser3(map);
if
(count>
0
){
System.out.println(
"成功"
);
session.commit();
}
}
}
|
本文转自 l363130002 51CTO博客,原文链接:http://blog.51cto.com/liuyj/1609916