码迷,mamicode.com
首页 > 编程语言 > 详细

spring和mybatis集成

时间:2016-07-24 12:01:10      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

-------------------applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 属性资源文件的来源 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!--
            配置连接池需要加入jar
            com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
            com.springsource.org.apache.commons.pool-1.3.0.jar
         -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driverClassName}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>
    <!-- mybatis的sqlsessionFactory被spring容器管理 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置mybatis (mapper)映射器路径-->
        <property name="mapperLocations" value="classpath:com/tabchanj/sm/mapper/**/*.xml" />
        <!-- 配置mybatis 类型别名-->
        <property name="typeAliasesPackage" value="com.tabchanj.sm.domain" />
        <!-- 
            配置mybatis主配置文件(可选) 
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        -->
    </bean>
    <!-- 通过 MapperFactoryBean为我们创建Mapper代理对象-->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 为那个Mapper映射器创建代理类 -->
        <property name="mapperInterface" value="com.tabchanj.sm.domain.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 使用aop应用我们的事务管理器 -->
        
    <!-- 事务管理器aop通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="fin*" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
    <!-- 定义aop中切入点 -->
        <aop:pointcut expression="execution(* com.tabchanj.sm.service.*.*(..))"
            id="pointcut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
    </aop:config>
    
    <!-- 
        <bean id="service" class="xx">
            <property name="userMapper" ref="userMapper"/>
        </bean>
     -->
</beans>

 

---------------------jar包-----------------------------
asm-3.3.1.jar cglib-2.2.2.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar com.springsource.org.apache.commons.logging-1.1.1.jar com.springsource.org.apache.commons.pool-1.3.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar javassist-3.17.1-GA.jar log4j-1.2.17.jar mybatis-3.2.1.jar mybatis-spring-1.2.0.jar mysql-connector-java-5.1.10-bin.jar slf4j-api-1.7.2.jar slf4j-log4j12-1.7.2.jar spring-aop-3.2.0.RELEASE.jar spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar spring-jdbc-3.2.0.RELEASE.jar spring-tx-3.2.0.RELEASE.jar
------------db.properties------------------------
db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/mybatis db.username=root db.password=1111
//测试
@Test
public void testName() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(ac.getBean("sqlSessionFactory")); UserMapper mapper = (UserMapper)ac.getBean("userMapper"); List<User> users = mapper.list(); for (User user : users) { System.out.println(user); }

 

spring和mybatis集成

标签:

原文地址:http://www.cnblogs.com/tabchanj/p/5700316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!