MapperScannerConfigurer 掃描配置
能掃描接口锋玲,基于接口創(chuàng)建一個(gè)代理對(duì)象
public class MapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {
private String basePackage;
}
配置dao接口掃描,底層會(huì)基于dao接口創(chuàng)建這個(gè)接口的代理對(duì)象蜓氨,這個(gè)代理對(duì)象內(nèi)部會(huì)通過mybatis訪問數(shù)據(jù)庫
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="BasePackage" value=".dao"/>
<
!--下面這行可以不寫,但是有多個(gè) sqlSessionFactory會(huì)報(bào)錯(cuò) -->
<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
掃描.dao是為了拿到dao接口,底層系統(tǒng)通過jdk的api-(Proxy的newProxyInstance創(chuàng)建代理實(shí)例)為接口創(chuàng)建代理對(duì)象横漏,這個(gè)對(duì)象還需要注入sqlSessionFactory,對(duì)分布式系統(tǒng)sqlSessionFactory對(duì)象可能有多個(gè)
底層在創(chuàng)建dao接口代理對(duì)象的時(shí)候熟掂,在代理類里面要通過
sqlSessionFactory來openSession,通過sqlSession訪問數(shù)據(jù)庫缎浇,最后釋放資源。所以要關(guān)聯(lián)sqlSessionFactory赴肚,掃描dao接口的目的是創(chuàng)建實(shí)現(xiàn)類素跺,創(chuàng)建實(shí)現(xiàn)類的目的是訪問數(shù)據(jù)庫,訪問數(shù)據(jù)庫要通過mybatis,mybatis需要sqlSessionFactory
/spring-ioc-v3/src/main/resources/spring-configs.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<util:properties id="cfg" location="classpath:config.properties"></util:properties>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close" lazy-init="false">
<property name="driverClassName" value="#{cfg.jdbcDriver}"></property>
<property name="url" value="#{cfg.jdbcUrl}"></property>
<property name="username" value="#{cfg.jdbcUsername}"></property>
<property name="password" value="#{cfg.jdbcPassword}"></property>
<!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
<property name="maxWait" value="1800" />
<property name="MaxActive" value="10" />
</bean>
<!--整合SqlSessionFactoryBean對(duì)象(通過此對(duì)象創(chuàng)建SqlSession) -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
lazy-init="true">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/sys/*Mapper.xml" />
</bean>
<!-- 配置dao接口掃描誉券。底層會(huì)基于接口創(chuàng)建這個(gè)接口的代理對(duì)象指厌,這個(gè)代理對(duì)象的內(nèi)部會(huì)通過mybatis訪問數(shù)據(jù)庫 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="BasePackage" value="**.dao" />
<!--下面這行可以不寫,但是有多個(gè) sqlSessionFactory會(huì)報(bào)錯(cuò) -->
<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
/spring-ioc-v3/src/main/resources/config.properties
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8
jdbcUsername=root
jdbcPassword=123456
/spring-ioc-v3/src/main/resources/mapper/sys/SysLogMapper.xml
<?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="dao.SysLogDao">
<select id="findPageObjects" resultType="entity.SysLog">
select * from sys_logs
</select>
</mapper>
/spring-ioc-v3/src/main/java/dao/SysLogDao.java
package dao;
import java.util.List;
import entity.SysLog;
/**
* DAO:數(shù)據(jù)訪問對(duì)象
* 定義訪問數(shù)據(jù)庫的相關(guān)方法
*/
public interface SysLogDao {
List<SysLog>findPageObjects();
}
/spring-ioc-v3/src/main/java/entity/SysLog.java
package entity;
import java.io.Serializable;
import java.util.Date;
/**
* POJO:日志實(shí)體對(duì)象 封裝系統(tǒng)的日志信息 實(shí)現(xiàn)序列化接口横朋,便于網(wǎng)絡(luò)傳輸
*/
public class SysLog implements Serializable {
private static final long serialVersionUID = -496934815638734874L;
private Integer id;
/**
* 操作用戶
*/
private String username;
/**
* 執(zhí)行的操作
*/
private String operation;
/**
* 執(zhí)行這個(gè)操作的方法
*/
private String method;
/**
* 調(diào)用方法傳入的參數(shù)
*/
private String params;
/**
* 方法的執(zhí)行時(shí)間
*/
private Long time;
/**
* 用戶的ip地址
*/
private String ip;
/**
* 這個(gè)日志的創(chuàng)建時(shí)間
*/
private Date createdTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
@Override
public String toString() {
return "SysLog [id=" + id + ", username=" + username + ", operation=" + operation + ", method=" + method
+ ", params=" + params + ", time=" + time + ", ip=" + ip + ", createdTime=" + createdTime + "]";
}
}
package test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBase {
protected ClassPathXmlApplicationContext ctx;
@Before
public void init(){
ctx=new ClassPathXmlApplicationContext("spring-configs.xml");
}
@After
public void close(){
ctx.close();
}
}