下面是被测试的类:
1packagecn.mrf.mybatis.dao;
2
3importjava.util.List;
4
5importorg.apache.ibatis.session.SqlSession;
6importorg.apache.ibatis.session.SqlSessionFactory;
7
8importcn.mrf.mybatis.po.User;
9/**
10*
11*@ClassName:UserDaoImpl
12*@Description:接口实现类
13*@authormrf
14*@date2015-9-19下午05:57:03
15*
16*/
17publicclassUserDaoImplimplementsUserDao{
18
19//需要向dao实现类中注入SqlSessionFactory
20//这里通过构造方法注入
21privateSqlSessionFactorysqlSessionFactory;
22
23publicUserDaoImpl(SqlSessionFactorysqlSessionFactory){
24this.sqlSessionFactory=sqlSessionFactory;
25}
26
27
28@Override
29publicUserfindUserById(intid)throwsException{
30//sqlSession是线程不安全的,所以单独使用
31SqlSessionsqlSession=sqlSessionFactory.openSession();
32
33Useruser=sqlSession.selectOne("test.findUserById",id);
34
35//释放资源
36sqlSession.close();
37returnuser;
38}
39
40@Override
41publicList<User>findUserByName(Stringname)throwsException{
42SqlSessionsqlSession=sqlSessionFactory.openSession();
43
44List<User>list=sqlSession.selectList("test.findUserByName",name);
45
46//释放资源
47returnlist;
48}
49
50@Override
51publicvoidinsertUser(Useruser)throwsException{
52SqlSessionsqlSession=sqlSessionFactory.openSession();
53//执行插入
54sqlSession.insert("test.insertUser",user);
55
56//提交事物
57sqlSession.commit();
58//释放资源
59sqlSession.close();
60
61
62}
63
64@Override
65publicvoiddeleteUser(intid)throwsException{
66SqlSessionsqlSession=sqlSessionFactory.openSession();
67
68//执行插入操作
69sqlSession.delete("test.deleteUser",id);
70
71//提交事务
72sqlSession.commit();
73
74//释放资源
75sqlSession.close();
76}
77
78}