您的位置:首页 > 其它

MyBatis根据接口和xml实现Dao功能

2017-04-11 14:51 441 查看
方法名和Mapper中配置的id名必须一样。


实现:


一、配置Spring集成MyBatis:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"...

-------------------------------------------------

<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
...
-------------------------------------------------

<!-- 产生sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

--------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

要实现对数据库的操作必须要有sqlSession,而sqlSession是由sqlSessionFactory创建的。我们可以在spring配置好bean。
<!-- 自动扫描mapper接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.xiaojuzi.chaojijuhui.**.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory" />
1
2
3
4
1
2
3
4

这个配置就是配置映射文件的路径,这样做的好处就是不用再写Dao的实现类了,也就是说,我们写好接口,写好配置文件,会自动映射到对应的方法和sql语句。


二、开发mapper.xml映射文件

<mapper namespace="com.xiaojuzi.chaojijuhui.user.dao.UserDao">

-------------------------------------------------------
1
2
3
1
2
3



在这里只有一个UserDao(被代理的接口)。 

user.mapper.xml–namespace配置的就是UserDao的包全名。


三、开发mapper.java的接口

/**
* 根据用户的用户名查询用户
* @param user
* @return
*/
User queryUserByLoginName (String loginName);

/**
* 用户通过手机号码去修改密码
* @param userModel
* @return
*/
Boolean updatePasswordByMobile(UserModel userModel);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14

如果需要特定类型的参数,就自己再造一个POJO类(例如:UserModel)。
<sql id="userColumns">
u.id,
u.login_name as "loginName",
u.head_img as "headImg",
...
---------------------------------------------

<select id="queryUserByLoginName" resultType="User" parameterType="User">
SELECT <include refid="userColumns" />
FROM juhui_user u
WHERE u.login_name = #{loginName}
and u.del_flag = #{DEL_FLAG_NORMAL}
</select>

<update id="updatePasswordByMobile" parameterType="UserModel">
update juhui_user set
update_date=DATE_FORMAT(#{updateDate}, '%Y-%m-%d %H:%i:%S'),
salt = #{salt},
password = #{password}
where mobile = #{mobile}
</update>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

这里mapper.xml的(select、insert、update..)标签的id必须和DAO接口的方法名一样!


Mapper开发规则

1、 在mapper.xml中将namespace设置为mapper.Java的全限定名 

2、 将mapper.java接口的方法名和mapper.xml中statement的id保持一致。 

3、 将mapper.java接口的方法输入参数类型和mapper.xml中statement的parameterType保持一致 

4、 将mapper.java接口的方法输出 结果类型和mapper.xml中statement的resultType保持一致

注意遵循上边四点规范!

这样抛弃Dao实现类的写法: 

具有更好的可扩展性,提高了灵活度。


原理

再根据网上的一些知识点,讲一下原理:

mybatis通过JDK的动态代理方式,在启动加载配置文件时,根据配置mapper的xml去生成Dao的实现。

session.getMapper()使用了代理,当调用一次此方法,都会产生一个代理class的instance,看看这个代理class的实现.
public class MapperProxy implements InvocationHandler {
...
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
ClassLoader classLoader = mapperInterface.getClassLoader();
Class<?>[] interfaces = new Class[]{mapperInterface};
MapperProxy proxy = new MapperProxy(sqlSession);
return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!OBJECT_METHODS.contains(method.getName())) {
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
final Object result = mapperMethod.execute(args);
if (result == null && method.getReturnType().isPrimitive()) {
throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" +    method.getReturnType() + ").");
}
return result;
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

这里是用到了JDK的代理Proxy。 newMapperProxy()可以取得实现interfaces 的class的代理类的实例。

当执行interfaces中的方法的时候,会自动执行invoke()方法,其中public Object invoke(Object proxy, Method method, Object[] args)中 method参数就代表你要执行的方法.

MapperMethod类会使用method方法的methodName 和declaringInterface去取 sqlMapxml 取得对应的sql,也就是拿declaringInterface的类全名加上 sql-id..

转载:http://blog.csdn.net/qq_26525215
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: