項(xiàng)目地址:傳送門(mén)
一祝懂、搭建開(kāi)發(fā)環(huán)境
1.1、使用IDEA新建一個(gè)項(xiàng)目奶浦,項(xiàng)目結(jié)構(gòu)如下:
其中用到的pox.xml內(nèi)容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shi</groupId>
<artifactId>spring4-mybatis3</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring4-mybatis3</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 添加Spring-core包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- 添加spring-context包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- 添加spring-tx包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- 添加spring-jdbc包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- 為了方便進(jìn)行單元測(cè)試恰梢,添加spring-test包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!--添加spring-web包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!--添加aspectjweaver包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<!-- 添加mybatis的核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 添加mybatis與Spring整合的核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 添加servlet3.0核心包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.2-b01</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 添加mysql驅(qū)動(dòng)包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- 添加druid連接池包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.12</version>
</dependency>
<!-- 添加junit單元測(cè)試包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring4-mybatis3</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
pox.xml主要分兩大部分藻烤,上面的10行-111行主要是為了添加項(xiàng)目jar包依賴(lài),下面的114行-124行主要是為了集成generator插件(可以通過(guò)映射數(shù)據(jù)庫(kù)表字段生成實(shí)體類(lèi))爽雄;
添加pox.xml文件只有蝠检,在IDEA的右側(cè)打開(kāi)Maven Projects模塊,點(diǎn)擊左上方的刷新控件挚瘟,刷新整個(gè)項(xiàng)目叹谁。
1.2饲梭、右鍵項(xiàng)目,選擇open module setting本慕,點(diǎn)擊左上角的+符號(hào)排拷,選擇maven,添加generator插件
二锅尘、創(chuàng)建數(shù)據(jù)庫(kù)和表(針對(duì)MySQL)
2.1 在MySQL數(shù)據(jù)庫(kù)中執(zhí)行如下SQL語(yǔ)句:
Create DATABASE spring4_mybatis3;
USE spring4_mybatis3;
DROP TABLE IF EXISTS user;
CREATE TABLE user (
user_id char(32) NOT NULL,
user_name varchar(30) DEFAULT NULL,
user_birthday date DEFAULT NULL,
user_salary double DEFAULT NULL,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
三监氢、使用generator生成數(shù)據(jù)庫(kù)映射代碼
2.2 手動(dòng)創(chuàng)建【src/main/resources】、【src/main/java】藤违、【src/test/resources】浪腐、【src/test/java】這幾個(gè)source folder,如下圖所示:
2.3 在main/resources中添加generatorConfig.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng)包位置 -->
<classPathEntry location="D:\Java\IdeaProjects\MyBatis\lib\mysql-connector-java-5.1.7-bin.jar" />
<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 數(shù)據(jù)庫(kù)鏈接URL顿乒、用戶名议街、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/spring4_mybatis3" userId="root" password="123456">
<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成實(shí)體類(lèi)的包名和位置,這里配置將生成的實(shí)體類(lèi)放在com.shi.domain這個(gè)包下 -->
<javaModelGenerator targetPackage="com.shi.domain" targetProject="D:\Java\IdeaProjects\MyBatis\MyBatis_08\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的SQL映射文件包名和位置璧榄,這里配置將生成的SQL映射文件放在com.shi.mapping這個(gè)包下 -->
<sqlMapGenerator targetPackage="com.shi.mapping" targetProject="D:\Java\IdeaProjects\MyBatis\MyBatis_08\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置特漩,這里配置將生成的dao類(lèi)放在com.shi.dao這個(gè)包下 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.shi.dao" targetProject="D:\Java\IdeaProjects\MyBatis\MyBatis_08\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
運(yùn)行之前配置好的generator_08插件,會(huì)在項(xiàng)目的java目錄中根據(jù)創(chuàng)建好的數(shù)據(jù)庫(kù)表生成MyBatis的表對(duì)應(yīng)的實(shí)體類(lèi):
生成的代碼和映射文件一般都不用改骨杂,可以直接應(yīng)用到項(xiàng)目當(dāng)中涂身。下面我們看一眼由generator插件生成的代碼和映射文件:
- 生成的dao類(lèi)
生成的UserMapper是一個(gè)接口,里面定義了一些操作user表的增刪改查方法搓蚪。
package com.shi.dao;
import com.shi.domain.User;
public interface UserMapper {
int deleteByPrimaryKey(String userId);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(String userId);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
- 生成的實(shí)體類(lèi)
User類(lèi)是user表的對(duì)應(yīng)的實(shí)體類(lèi)蛤售,User類(lèi)中定義的屬性和user表中的字段一一對(duì)應(yīng)。
package com.shi.domain;
import java.util.Date;
public class User {
private String userId;
private String userName;
private Date userBirthday;
private Double userSalary;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId == null ? null : userId.trim();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
public Double getUserSalary() {
return userSalary;
}
public void setUserSalary(Double userSalary) {
this.userSalary = userSalary;
}
}
- 生成的SQL映射文件
<?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.shi.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.shi.domain.User" >
<id column="user_id" property="userId" jdbcType="CHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_birthday" property="userBirthday" jdbcType="DATE" />
<result column="user_salary" property="userSalary" jdbcType="DOUBLE" />
</resultMap>
<sql id="Base_Column_List" >
user_id, user_name, user_birthday, user_salary
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from user
where user_id = #{userId,jdbcType=CHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from user
where user_id = #{userId,jdbcType=CHAR}
</delete>
<insert id="insert" parameterType="com.shi.domain.User" >
insert into user (user_id, user_name, user_birthday,
user_salary)
values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE},
#{userSalary,jdbcType=DOUBLE})
</insert>
<insert id="insertSelective" parameterType="com.shi.domain.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
user_id,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="userBirthday != null" >
user_birthday,
</if>
<if test="userSalary != null" >
user_salary,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null" >
#{userId,jdbcType=CHAR},
</if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="userBirthday != null" >
#{userBirthday,jdbcType=DATE},
</if>
<if test="userSalary != null" >
#{userSalary,jdbcType=DOUBLE},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.shi.domain.User" >
update user
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userBirthday != null" >
user_birthday = #{userBirthday,jdbcType=DATE},
</if>
<if test="userSalary != null" >
user_salary = #{userSalary,jdbcType=DOUBLE},
</if>
</set>
where user_id = #{userId,jdbcType=CHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.shi.domain.User" >
update user
set user_name = #{userName,jdbcType=VARCHAR},
user_birthday = #{userBirthday,jdbcType=DATE},
user_salary = #{userSalary,jdbcType=DOUBLE}
where user_id = #{userId,jdbcType=CHAR}
</update>
</mapper>
UserMapper.xml這個(gè)文件的內(nèi)容是編寫(xiě)操作user表的SQL語(yǔ)句妒潭,重點(diǎn)說(shuō)一下UserMapper.xml配置中需要注意的幾個(gè)小細(xì)節(jié)問(wèn)題:
1悴能、UserMapper.xml的<mapper>標(biāo)簽的namespace必須是UserMapper接口的全類(lèi)名,<mapper namespace="com.shi.dao.UserMapper" >
2雳灾、UserMapper.xml的定義操作數(shù)據(jù)庫(kù)的<select><delete><update><insert>這些標(biāo)簽的id屬性的值必須和UserMapper接口定義的方法名一致漠酿,如下圖所示:
四、Spring與MyBatis整合
4.1佑女、添加Service對(duì)象
1记靡、在src/main/java目錄下創(chuàng)建一個(gè)com.shi.service包,然后在com.shi.service包創(chuàng)建一個(gè)UserServiceI接口团驱,如下所示:
package com.shi.service;
import com.shi.domain.User;
public interface UserServiceI {
/**
* 添加用戶
* @param user
*/
void addUser(User user);
/**
* 根據(jù)用戶id獲取用戶
* @param userId
* @return
*/
User getUserById(String userId);
}
2摸吠、在src/main/java目錄下創(chuàng)建一個(gè)com.shi.service.impl包,然后在com.shi.service.impl包創(chuàng)建一個(gè)針對(duì)UserServiceI接口的實(shí)現(xiàn)類(lèi):UserServiceImpl嚎花,如下所示:
package com.shi.service.impl;
import com.shi.dao.UserMapper;
import com.shi.domain.User;
import com.shi.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author AFinalStone
* 使用@Service注解將UserServiceImpl類(lèi)標(biāo)注為一個(gè)service
* service的id是userService
*/
@Service("userService")
public class UserServiceImpl implements UserServiceI {
/**
* 使用@Autowired注解標(biāo)注userMapper變量寸痢,
* 當(dāng)需要使用UserMapper時(shí),Spring就會(huì)自動(dòng)注入U(xiǎn)serMapper
*/
@Autowired
private UserMapper userMapper;//注入dao
public void addUser(User user) {
userMapper.insert(user);
}
public User getUserById(String userId) {
return userMapper.selectByPrimaryKey(userId);
}
}
4.2紊选、編寫(xiě)相關(guān)配置文件
1啼止、dbconfig.properties
在src/main/resources目錄下創(chuàng)建一個(gè)dbconfig.properties文件道逗,用于編寫(xiě)連接MySQL數(shù)據(jù)庫(kù)的相關(guān)信息,dbconfig.properties的內(nèi)容如下:
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/spring4_mybatis3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=123456
2献烦、spring.xml(spring框架的配置文件)
在src/main/resources目錄下創(chuàng)建一個(gè)spring.xml文件滓窍,spring.xml文件就是針對(duì)Spring框架編寫(xiě)的核心配置文件,我們的spring.xml文件的配置非常簡(jiǎn)單巩那,就兩個(gè)配置吏夯。
spring.xml的內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 引入dbconfig.properties屬性文件 -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<!-- 自動(dòng)掃描(自動(dòng)注入),com.shi.service這個(gè)包以及它的子包的所有使用@Service注解標(biāo)注的類(lèi) -->
<context:component-scan base-package="com.shi.service" />
</beans>
3即横、spring-mybatis.xml(spring與mybatis整合的配置文件)
在src/main/resources目錄下創(chuàng)建一個(gè)spring-mybatis.xml文件噪生,spring-mybatis.xml文件就是針對(duì)Spring框架與Mybatis框架整合編寫(xiě)的配置文件,spring-mybatis.xml的內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- JNDI方式配置數(shù)據(jù)源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
<!-- ========================================配置數(shù)據(jù)源========================================= -->
<!-- 配置數(shù)據(jù)源东囚,使用的是alibaba的Druid(德魯伊)數(shù)據(jù)源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 初始化連接大小 -->
<property name="initialSize" value="0" />
<!-- 連接池最大使用連接數(shù)量 -->
<property name="maxActive" value="20" />
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="20" />
<!-- 連接池最小空閑 -->
<property name="minIdle" value="0" />
<!-- 獲取連接最大等待時(shí)間 -->
<property name="maxWait" value="60000" />
<!--
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
-->
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置間隔多久才進(jìn)行一次檢測(cè)跺嗽,檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個(gè)連接在池中最小生存的時(shí)間页藻,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打開(kāi)removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒桨嫁,也就是30分鐘 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 -->
<property name="logAbandoned" value="true" />
<!-- 監(jiān)控?cái)?shù)據(jù)庫(kù) -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!-- ========================================分隔線========================================= -->
<!-- ========================================針對(duì)myBatis的配置項(xiàng)============================== -->
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 實(shí)例化sqlSessionFactory時(shí)需要使用上述配置好的數(shù)據(jù)源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<!-- 自動(dòng)掃描com/shi/mapping/目錄下的所有SQL映射的xml文件, 省掉Configuration.xml里的手工配置
value="classpath:com/shi/mapping/*.xml"指的是classpath(類(lèi)路徑)com.shi.mapping包中的所有xml文件
UserMapper.xml位于com.shi.mapping包下,這樣UserMapper.xml就可以被自動(dòng)掃描
-->
<property name="mapperLocations" value="classpath:com/shi/mapping/UserMapper.xml" />
</bean>
<!-- 配置掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描com.shi.dao這個(gè)包以及它的子包下的所有映射接口類(lèi) -->
<property name="basePackage" value="com.shi.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- ========================================分隔線========================================= -->
<!-- 配置Spring的事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<!-- 攔截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.shi.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
<!-- 配置druid監(jiān)控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.shi.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>
注意:需要我們把pom.xml依賴(lài)添加到項(xiàng)目當(dāng)中
4.3 進(jìn)行單元測(cè)試
經(jīng)過(guò)以上兩個(gè)步驟份帐,spring4與mybatis3的整合算是全部完成了瞧甩。接下來(lái)我們要做的工作就算進(jìn)行單元測(cè)試,測(cè)試一下spring4與mybatis3的整合是否成功弥鹦。
1、在src/main/java目錄下創(chuàng)建一個(gè)com.shi.service包爷辙,然后在com.shi.service包創(chuàng)建一個(gè)UserServiceI接口彬坏,如下所示: