1
2
3
4
5
6
7
|
public
interface
EmployeeMapper {
/*
* 增删改查方法
* */
public
Employee getEmployeeById(Integer id);
public
void
insertEmp(Employee employee);
}
|
1
2
3
4
5
6
7
|
select * from student where id = #{id}
insert into student(name,password,email) values(#{name},#{password},#{email})
|
编写测试单元:
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
|
private
EmployeeMapper mapper =
null
;
private
SqlSession session =
null
;
@Before
public
void
testBefore(){
//1.获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2.利用sqlSessionFactory创建一个session对象,表示和数据库的一次会话
session = sqlSessionFactory.openSession();
//3.用session对象获取mapper接口的代理对象
//因为sql映射文件给相应的接口创建了一个代理对象,所以mapper接口类不需要实现类
mapper = session.getMapper(EmployeeMapper.
class
);
}
@Test
public
void
testSelect(){
mapper = session.getMapper(EmployeeMapper.
class
);
//4.通过mapper接口的代理对象就可以对数据库进行增删改查操作
Employee employee = mapper.getEmployeeById(
4
);
System.out.println(employee);
}
@Test
public
void
testInsert(){
mapper.insertEmp(emp);
int
id = emp.getId();
System.out.println(id);
}
@After
public
void
testAfter(){
//增删改需要提交事务
session.commit();
session.close();
}
//@Before、@After自动在@Test之前和之后运行
//查询不需要提交事务,增删改都需要提交事务
|
2.获取自增主键值【当向数据库中插入一条数据的时候,默认是拿不到主键的值的, 需要设置如下两个属性才可以拿到主键值!】
1
2
3
4
|
insert into tbl_employee(last_name,email,gender) values(#{lastName},#{gender},#{email})
|
1
2
3
4
5
6
|
name,password,email
insert into student(
|
1
2
3
4
|
public
void
updateEmp(
@Param
(
"id"
)Integer id,
@Param
(
"name"
)String name,
@Param
(
"password"
)String password,
@Param
(
"email"
)String email);
|
1
2
3
|
update student set name=#{name},password=#{password},email=#{email} where id=#{id}
|
- POJO参数:如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入POJO
#{属性名}:取出传入的POJO的属性值
1
|
public
void
insertEmp(Employee employee);
|
1
2
3
4
5
6
|
name,password,email
insert into student(
|
1
2
3
4
5
|
@Test
public
void
testReturnVal(){
Employee employee = mapper.getEmployeeById(
30
);
System.out.println(employee);
}
|
- Map:如果多个参数不是业务模型中的数据,没有对应的pojo,不经常使用,为了方便,我们也可以传入Map
#{key}:根据 key 取出map中对应的值
1
|
public
void
updateName(Map
|
1
2
3
|
update student set name=#{name} where id=#{id}
|
1
2
3
4
5
6
7
|
@Test
public
void
testMap(){
Map
new
HashMap<>();
map.put(
"id"
,
33
);
map.put(
"name"
,
"刘德华"
);
mapper.updateName(map);
}
|
1
2
3
|
update student set name=#{name},password=#{password},email=#{email} where id=#{id}
|
${}:取出的值直接拼装在sql语句中,会有安全问题
1
2
3
|
update student set name=
'${name}'
,password=
'${password}'
,email=
'${email}'
where id=
'${id}'
|
1
|
public
List
|
1
2
3
|
select * from student
|
1
2
3
4
5
6
7
|
@Test
public
void
testReturnList(){
List
for
(Employee employee : emps) {
System.out.println(employee);
}
}
|
2.返回记录为一个Map
只能查询单条数据,如果多条的话,多个key 值,找不到
1
|
public
Map
|
resultType 是 Map 的全类名
1
2
3
|
select * from student where id = #{id}
|
key:列名;value:值
1
2
3
4
5
6
7
8
9
|
@Test
public
void
testReturnMap(){
Map
30
);
Set
for
(Entry
System.out.println(entry.getKey()+
":"
+entry.getValue());
}
}
|
1
2
3
4
|
|
3.利用ResultMap:
1
|
public
Employee getEmpInfoById(Integer id);
|
1
2
3
4
5
6
7
8
9
10
11
|
select * from student where id = #{id}
|
1
2
3
4
5
|
@Test
public
void
testReturnMap(){
Employee emp = mapper.getEmpInfoById(
30
);
System.out.println(emp);
}
|