MapperScannerConfigurer 扫描配置
能扫描接口,基于接口创建一个代理对象
public class MapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {
private String basePackage;
}
配置dao接口扫描,底层会基于dao接口创建这个接口的代理对象,这个代理对象内部会通过mybatis访问数据库
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="BasePackage" value=".dao"/>
<
!--下面这行可以不写,但是有多个 sqlSessionFactory会报错 -->
<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
扫描.dao是为了拿到dao接口,底层系统通过jdk的api-(Proxy的newProxyInstance创建代理实例)为接口创建代理对象,这个对象还需要注入sqlSessionFactory,对分布式系统sqlSessionFactory对象可能有多个
底层在创建dao接口代理对象的时候,在代理类里面要通过
sqlSessionFactory来openSession,通过sqlSession访问数据库,最后释放资源。所以要关联sqlSessionFactory,扫描dao接口的目的是创建实现类,创建实现类的目的是访问数据库,访问数据库要通过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>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="1800" />
<property name="MaxActive" value="10" />
</bean>
<!--整合SqlSessionFactoryBean对象(通过此对象创建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接口扫描。底层会基于接口创建这个接口的代理对象,这个代理对象的内部会通过mybatis访问数据库 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="BasePackage" value="**.dao" />
<!--下面这行可以不写,但是有多个 sqlSessionFactory会报错 -->
<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:数据访问对象
* 定义访问数据库的相关方法
*/
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:日志实体对象 封装系统的日志信息 实现序列化接口,便于网络传输
*/
public class SysLog implements Serializable {
private static final long serialVersionUID = -496934815638734874L;
private Integer id;
/**
* 操作用户
*/
private String username;
/**
* 执行的操作
*/
private String operation;
/**
* 执行这个操作的方法
*/
private String method;
/**
* 调用方法传入的参数
*/
private String params;
/**
* 方法的执行时间
*/
private Long time;
/**
* 用户的ip地址
*/
private String ip;
/**
* 这个日志的创建时间
*/
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();
}
}