mapper动态代理开发配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--配置数据源(dataSource) 注意使用的jdbc、c3p0、dbcp他们的name不同-->
<!--加载properties文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc_driver}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
</bean>
<!-- 工厂 -->
<bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:config/SqlMapConfig.xml"/>
</bean>

<!-- 原始Dao开发 -->
<bean id="userDao" class="com.mybatis.review.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="SqlSessionFactoryBean"/>
</bean>

<!-- mapper动态代理开发 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="SqlSessionFactoryBean"/>
<property name="mapperInterface" value="com.mybatis.review.mapper.UserMapper"/>
</bean> -->

<!-- mapper动态代理开发|扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 基本包 -->
<property name="basePackage" value="com.mybatis.review.mapper"/>
</bean>
</beans>

在这里注意注入自己的mapper属性时那么必须为 mapperInterface 中间的字母大小写不能写错

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置别名 -->
    <typeAliases>
        <!-- <typeAlias type="com.mybatis.review.entity.User" alias="User"/> -->
        <package name="com.mybatis.review.entity" />
    </typeAliases>
    <!-- 配置映射 -->
    <mappers>
        <!-- <mapper resource="com.mybatis.review.mapper.UserMapper.xml"/> -->
        <package name="com.mybatis.review.mapper"/>
    </mappers>
</configuration>

jdbc.properties(mysql)

jdbc_url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
jdbc_username=root
jdbc_password=root
jdbc_driver=com.mysql.jdbc.Driver

 

posted @ 2019-04-01 17:29  杜小二  阅读(119)  评论(0编辑  收藏  举报