MyBatis根据接口和xml实现Dao功能
2017-04-11 14:51
441 查看
方法名和Mapper中配置的id名必须一样。
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。
2
3
4
1
2
3
4
这个配置就是配置映射文件的路径,这样做的好处就是不用再写Dao的实现类了,也就是说,我们写好接口,写好配置文件,会自动映射到对应的方法和sql语句。
2
3
1
2
3
在这里只有一个UserDao(被代理的接口)。
user.mapper.xml–namespace配置的就是UserDao的包全名。
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)。
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接口的方法名一样!
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的实现.
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
实现:
一、配置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
相关文章推荐
- mybatis如何根据mapper接口生成其实现类
- 使用MyBatis的Generator自动创建实体类和dao的接口与xml
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
- mybatis如何根据mapper接口生成其实现类(springboot)
- mybatis如何根据mapper接口生成其实现类
- mybatis如何根据mapper接口生成其实现类
- mybatis原始dao开发改进(dao接口和dao实现类)
- Mybatis使用Mapper代理的方式生成DAO接口的实现类对象
- mybatis如何根据mapper接口生成其实现类
- mybatis功能之spring自动代理完成dao的实现类功能
- mybatis功能之spring自动代理完成dao的实现类功能
- SprignMVC+myBatis整合+mybatis源码分析+动态代理实现流程+如何根据mapper接口生成其实现类
- mybatis如何根据mapper接口生成其实现类
- mybatis的两种实现方式:dao接口方式+dao实现方式
- idea中mybatis-generator插件实现数据库表自动生成相关接口、pojo、和xml配置
- mybatis的dao接口实现小结
- Java编程之TreeSet排序两种解决方法(1)元素自身具备比较功能,元素需要实现Comparable接口覆盖compare(2)创建根据自定义Person类的name进行排序的Comparator
- 17年2月26日Java后台学习记录,Mysql 多个timestamp,xml中使用小于号,Mybatisdao接口多个参数函数错误
- Mybatis如何根据mapper接口生成其实现类
- mybatis如何根据mapper接口生成其实现类