- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- ">
- <!-- 设置注解配置包扫描位置 -->
- <context:component-scan base-package="cn.zj.mybatis"/>
- <!-- 配置读取 db.properties 数据库配置文件 -->
- <context:property-placeholder location="classpath:db.properties"/>
- <!-- 配置数据源连接池 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driverClassName}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- <property name="maxActive" value="${jdbc.maxActive}"/>
- </bean>
- <!--
- 配置 MyBatis 框架的 SqlSessionFactoryBean 类, 创建
- SqlSessionFactory 工厂对象
- -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 1. 注入数据源 -->
- <property name="dataSource" ref="dataSource"/>
- <!-- 2. 配置映射文件 -->
- <property name="mapperLocations">
- <array>
- <!-- <value>classpath:cn/zj/mybatis/mapper/UserMapper.xml</value> -->
- <!-- 可以使用通配符 * 读取 目录下面所有的配置文件 -->
- <value>classpath:cn/zj/mybatis/mapper/*Mapper.xml</value>
- </array>
- </property>
- <!-- 3. 配置别名使用包扫描 -->
- <property name="typeAliasesPackage" value="cn.zj.mybatis.pojo"/>
- <!-- 4. 读取 mybat-config.xml 配置文件, 此配置文件可能还会配一些 mybatis 框架的
- 其他个性化配置
- 实际项目开发可能不用配置
- -->
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- </bean>
- <!-- SqlSession 不需要单独配置并创建了, 每次功能操作都需要新的 SqlSession 操作对象
- 所以在创建代理对象的自动创建了
- -->
- <!-- 创建 UserMapper 代理对象 - 创建单个 Mapper 对象
- 使用桥梁包 org.mybatis.spring.mapper.MapperFactoryBean<T> 创建 UserMapper 代理对象
- -->
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <!-- 注入 SqlSessionFacotry 对象 -->
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- <!-- 注入 UserMapper 接口类型: 底层创建 UserMapper 的代理对象 -->
- <property name="mapperInterface" value="cn.zj.mybatis.mapper.UserMapper"/>
- </bean>
- <!-- 批量创建 Mapper 代理对象 , 使用包扫描创建 Mapper 代理对象 -->
- </beans>
来源: http://www.bubuko.com/infodetail-3094550.html