配置文件项目结构图
1)spring-dao.xml文件配置
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置整合mybatis过程-->
<!--配置数据库相关参数-->
<context:property-placeholder location="classpath:spring/jdbc.properties" />
<!--数据库的连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="1000"/>
<!--重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--mybatis全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--sql映射文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--扫描entity 使用别名 个人推荐写全类名-->
<property name="typeAliasesPackage" value="org.seckill.entity"/>
</bean>
<!--配置扫描DAO接口的包,动态实现DAO接口 注入到Spring容器中来-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="org.seckill.dao"/>
</bean>
</beans>
2)mybatis-config.xm配置
<?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>
<!-- 配置全局属性-->
<settings>
<!--使用jdbc的getGenerateKeys 获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列别名替换列名 默认true-->
<setting name="useColumnLabel" value="true"/>
<!--开启驼峰命名转换 create_time -> createTime-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>