JAVA框架 Spring 和Mybatis整合(动态代理)

一、使用传统方式的dao的书写方式,不建议。目前采用的是动态代理的方式交给mybatis进行处理。

首先回顾下动态代理要求:

  1)子配置文件的中,namespace需要是接口的全路径,id是接口的方法名称 这两项唯一确定我们的调用的接口。

  2)子mapper文件的名称要和接口的名称保持一致。

  3)参数和返回值要和方法的保持一致。

二、整合

1)dao代码:

dao的代码,只需要保留接口即可。

2)service的代码:

实现类发生变化。

动态代理dao的id的名字是: 类名首字母小写

 1 package jd.com.service;
 2 
 3 import jd.com.dao.trDao;
 4 import jd.com.dao.user;
 5 import org.springframework.stereotype.Service;
 6 
 7 import javax.annotation.Resource;
 8 
 9 
10 @Service(value = "serv")
11 public class serFindByIdImpl implements trService {
12 
13     @Resource(name="trDao")
14     private  trDao trDao;
15 
16     @Override
17     public user serFindById(Integer id) {
18         user us=this.trDao.findUserByName(2);
19         return us;
20     }
21 }

 测试类:

 1 package jd.com.testDemo;
 2 
 3 import jd.com.dao.user;
 4 import jd.com.service.serFindByIdImpl;
 5 
 6 import org.junit.jupiter.api.Test;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 import javax.annotation.Resource;
11 
12 
13 public class testDemo {
14 
15 
16     @Test
17     public  void test1(){
18         ApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext.xml");
19         serFindByIdImpl serv= (serFindByIdImpl) ap.getBean("serv");
20         user us=serv.serFindById(2);
21         System.out.println(us);
22 
23 
24 
25     }
26 }

 三 、配置文件

applicationContext.xml 加入mapper的包的动态扫描

1     <!--开启mapper包扫描-->
2     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
3         <property name="basePackage" value="jd.com.dao" />
4     </bean>

需要使用类:MapperScannerConfigurer在spring和mybaits整合的包里。

需要配置属性basepack  需要扫描mapper.xml配置文件的包的目录,如果有多个mapper文件的话,可以在value出以逗号隔开写多个包。

配置了上面配置  不需要在SqlMapConfig.xml写入引入的文件。否则会加载2次。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!--总的约束-->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 5        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 8     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
10     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
11 <!--加载配置文件,定义的properites文件
12     引入外部文件使用classpath关键字。
13 -->
14 
15     <context:component-scan base-package="jd.com" />
16     <aop:aspectj-autoproxy />
17 
18 <context:property-placeholder location="classpath:/db.properties" />
19     <!--定义数据库连接池-->
20     <bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
21         <!--支持el表达式-->
22         <property name="driverClassName" value="${jdbc.driver}"/>
23         <property name="url" value="${jdbc.url}" />
24         <property name="username" value="${jdbc.username}" />
25         <property name="password" value="${jdbc.password}" />
26         <property name="maxTotal" value="10" />
27         <property name="maxIdle" value="3" />
28     </bean>
29     <!--配置mapper
30             其中:org.mybatis.spring.SqlSessionFactoryBean 是SqlSessionFactory的实现类,该类在mybatis-spring.1.2.2.jar里。
31     -->
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
33         <!--依赖DataSource-->
34         <property name="dataSource" ref="basicDataSource"/>
35          <!--加载mybaits的配置文件-->
36         <property name="configLocation" value="classpath:SqlMapConfig.xml" />
37     </bean>
38     <!--开启mapper包扫描-->
39     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
40         <property name="basePackage" value="jd.com.dao" />
41     </bean>
42 
43 
44 </beans>

子配置文件:需要namespace、id、parameterType、resultType要和接口的保持一致。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!--和主配置文件约束是不一样的-->
 6 <mapper namespace="jd.com.dao.trDao">
 7     <select id="findUserByName" parameterType="int" resultType="jd.com.dao.user" >
 8         SELECT * FROM username WHERE  id=#{id};
 9     </select>
10 </mapper>

四:探讨

  1、这个时候SqlMapConfig 文件是空的。可以去掉 在applicationContext.xml文件中添加mapper文件改为实际的子mapper配置文件路径 这个是错误的,因为如果有多个mapper文件咋办????

   2、mybaits的使用的动态代理 被代理的接口的,的动态代理对象的id是接口的名称的首字母小写。

原文地址:https://www.cnblogs.com/evilliu/p/8967372.html