JAVA框架 Spring 和Mybatis整合(動態代理)

1、使用傳統方式的dao的書寫方式,不建議。目前採用的是動態代理的方式交給mybatis進行處理。java

首先回顧下動態代理要求:spring

  1)子配置文件的中,namespace須要是接口的全路徑,id是接口的方法名稱 這兩項惟一肯定咱們的調用的接口。sql

  2)子mapper文件的名稱要和接口的名稱保持一致。數據庫

  3)參數和返回值要和方法的保持一致。apache

2、整合api

1)dao代碼:mybatis

dao的代碼,只須要保留接口便可。app

2)service的代碼:ide

實現類發生變化。測試

動態代理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>

 

 

 

四:探討

  一、這個時候SqlMapConfig 文件是空的。能夠去掉 在applicationContext.xml文件中添加mapper文件改成實際的子mapper配置文件路徑 這個是錯誤的,由於若是有多個mapper文件咋辦????

   二、mybaits的使用的動態代理 被代理的接口的,的動態代理對象的id是接口的名稱的首字母小寫。

相關文章
相關標籤/搜索