mybatis开发原始dao层请阅读我的上一篇博客:mybatis开发dao层的两种方式(原始dao层开发)
接上一篇博客继续介绍mybatis开发dao层的第二种方式:mapper动态代理方式
mapper接口开发方法只需要程序员编写mapper接口(相当于dao接口),由mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上一篇博客中dao接口实现类方法。
mapper接口开发需要遵循以下规范:
(1)mapper.xml文件中的namespace与mapper接口的类路径相同。
(2)mapper接口方法名和mapper.xml中定义的每个statement的id相同
(3)mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parametertype的类型相同
(4)mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resulttype的类型相同
1、定义mapper映射文件usermapper.xml(内容同user.xml),需要修改namespace的值为 usermapper接口路径。将usermapper.xml放在classpath 下sqlmapperr目录下。
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
|
<?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.xyfer.mapper.usermapper" > <!-- 根据id查询用户 --> <select id= "getuserbyid" parametertype= "int" resulttype= "com.xyfer.po.user" > select * from user where id = #{id} </select> <!-- 添加用户 --> <insert id= "insertuser" parametertype= "com.xyfer.po.user" > <selectkey keyproperty= "id" order= "after" resulttype= "java.lang.integer" > select last_insert_id() </selectkey> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) </insert> <!-- 修改用户 --> <update id= "updateuser" parametertype= "com.xyfer.po.user" > update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update> <!-- 删除用户 --> <delete id= "deleteuserbyid" parametertype= "int" > delete from user where id=#{id} </delete> </mapper> |
2、usermapper.java接口文件
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.xyfer.mapper; import com.xyfer.po.user; public interface usermapper { public user getuserbyid( int id); //根据id值查询一个用户 public void insertuser(user user); //新增一个用户 public void updateuser(user user); //修改一个用户 public void deleteuser( int id); //删除一个用户 } |
接口定义有如下特点:
(1)usermapper接口方法名和mapper.xml中定义的statement的id相同
(2)usermapper接口方法的输入参数类型和mapper.xml中定义的statement的parametertype的类型相同
(3) usermapper接口方法的输出参数类型和mapper.xml中定义的statement的resulttype的类型相同
3、在sqlmapconfig.xml文件中加载usermapper.xml文件
1
2
3
|
<mappers> <mapper resource= "usermapper.xml" /> </mappers> |
4、测试
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package com.xyfer.mapper; import static org.junit. assert .*; import java.io.inputstream; import java.util.date; import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.before; import org.junit.test; import com.xyfer.po.user; import junit.framework.testcase; public class usermappertest extends testcase{ private sqlsessionfactory sqlsessionfactory; protected void setup() throws exception { sqlsessionfactorybuilder sessionfactorybuilder = new sqlsessionfactorybuilder(); inputstream inputstream = resources.getresourceasstream( "sqlmapconfig.xml" ); sqlsessionfactory = sessionfactorybuilder.build(inputstream); } @test public void testgetuserbyid() { //获取sqlsession sqlsession sqlsession = sqlsessionfactory.opensession(); //获取usermapper接口代理对象 usermapper usermapper = sqlsession.getmapper(usermapper. class ); //调用代理对象方法 user user = usermapper.getuserbyid( 10 ); //打印结果 system.out.println(user); //关闭sqlsession sqlsession.close(); } @test public void testinsertuser() { //获取sqlsession sqlsession sqlsession = sqlsessionfactory.opensession(); //获取usermapper接口代理对象 usermapper usermapper = sqlsession.getmapper(usermapper. class ); //新建一个对象 user user = new user(); user.setusername( "小谢" ); user.setsex( "男" ); user.setbirthday( new date()); user.setaddress( "浙江省杭州市" ); //调用代理对象方法 usermapper.insertuser(user); //关闭sqlsession sqlsession.close(); } @test public void testupdateuser() { //获取sqlsession sqlsession sqlsession = sqlsessionfactory.opensession(); //获取usermapper接口代理对象 usermapper usermapper = sqlsession.getmapper(usermapper. class ); //新建一个对象 user user = new user(); user.setusername( "小谢" ); user.setsex( "男" ); user.setbirthday( new date()); user.setaddress( "上海市" ); //调用代理对象方法 usermapper.updateuser(user); //关闭sqlsession sqlsession.close(); } @test public void testdeleteuser() { //获取sqlsession sqlsession sqlsession = sqlsessionfactory.opensession(); //获取usermapper接口代理对象 usermapper usermapper = sqlsession.getmapper(usermapper. class ); //调用代理对象方法 usermapper.deleteuser( 6 ); //关闭sqlsession sqlsession.close(); } } |
以上步骤,完成mybatis框架以mapper动态代理方式开发dao层,并对数据库进行增删改查操作。
需要注意的是,上文提到的config文件夹和sqlmapper文件夹均为资源文件夹(source folder),默认会加载该路径下的文件。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/xyfer1018/p/10117227.html