二、Mybatis-Spring整合配置

二、Mybatis-Spring整合配置

1.spring-mybatis.xml配置文件
<!-- 自动扫描com.xxx包,将带有注解的类,纳入spring容器管理 -->  
<context:component-scan base-package="com.xxx" />

<!-- 加载jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 数据源相关配置 -->
<bean id="parentdataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="initialSize" value="2" />                <!-- 初始化连接数 -->
        <property name="minIdle" value="5" />                    <!-- 最小空闲连接数 -->
        <property name="maxIdle" value="10" />                    <!-- 最大空闲连接数 -->
        <property name="maxActive" value="20" />                <!-- 最大连接数 -->
        <property name="maxWait" value="1000" />                <!-- 最大获取连接池连接等待时间(毫秒) -->
        <property name="logAbandoned" value="true" />                <!-- 是否在自动回收超时连接的时候打印连接的超时错误 -->
        <property name="testWhileIdle" value="true" />                <!-- 空闲时是否验证, 若不通过断掉连接, 前提是空闲对象回收器开启状态 -->
        <property name="removeAbandoned" value="true" />            <!-- 是否自动回收超时连接 -->
        <property name="removeAbandonedTimeout" value="180" />            <!-- 超时时间(秒) -->                            
        <property name="timeBetweenEvictionRunsMillis" value="1800000" />   <!-- 空闲对象回收器由运行间隔(毫秒),若需要回收, 该值最好小于 minEvictableIdleTimeMillis值 -->
        <property name="minEvictableIdleTimeMillis" value="3600000" />      <!-- 被空闲对象回收器回收前在池中保持空闲状态的最小时间(毫秒) -->
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" parent="parentdataSource">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="defaultAutoCommit" value="false" /><!--默认true-->
</bean>

<bean name="sqlSessionFactory" id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/*.xml"/><!-- 扫描的UserMapper.xml配置文件 -->
    <property name="typeAliasesPackage" value="com.xxx.entity.user"/><!--写不写吧,在mapper.xml中写比较清楚-->
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.dao.mapper" /><!-- 扫描的UserMapper.java接口 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
</bean>

<!-- 开启注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

<!-- 配置声明式事务 -->
<aop:config>
    <aop:pointcut id="baseServiceMethods" expression="execution(* com.xxx.service..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />
</aop:config>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="add*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

2.User.java实体
public class User {
    private int id;
    private String name;
    get() set() toString()方法......
}

3.UserMapper.java接口
public interface UserMapper {
    User selectUser(@Param("id") id);
}

4.User.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="com.xxx.mapper.UserMapper">

    <select id="selectUser" parameterType="int" resultType="com.xxx.entity.User">

        select * from user where id = #{id}
    </select>

</mapper>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值