spring和mybatise的整合
2016-04-04 18:59
447 查看
第一步,通常是先确定使用框架,然后找到框架所需要的JAR文件,切记,需要什么就加什么
第二步,一定先分析框架有那么些配置文件,并且先找出来并进行初始化,删除不必要的东西。
第三步,就可以开始整合
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 启动@AspectJ框架的支持,如果你的项目中,
没有使用JAVA代码所写的切面,就不需要配置 -->
<aop:aspectj-autoproxy/>
<!-- 配置自动扫包规则 -->
<context:component-scan base-package="com.lovo.sm">
</context:component-scan>
<!-- 配置数据源 DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- &在配置文件中代表& -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis115?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="lovo"/>
</bean>
<!-- 配置Session工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
<!-- 将Mapper配置文件与Session进行关联 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lovo.sm.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理方式一,适合于单数据库,使用注解的方式来管理事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
第二步,一定先分析框架有那么些配置文件,并且先找出来并进行初始化,删除不必要的东西。
第三步,就可以开始整合
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 启动@AspectJ框架的支持,如果你的项目中,
没有使用JAVA代码所写的切面,就不需要配置 -->
<aop:aspectj-autoproxy/>
<!-- 配置自动扫包规则 -->
<context:component-scan base-package="com.lovo.sm">
</context:component-scan>
<!-- 配置数据源 DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- &在配置文件中代表& -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis115?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="lovo"/>
</bean>
<!-- 配置Session工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
<!-- 将Mapper配置文件与Session进行关联 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lovo.sm.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理方式一,适合于单数据库,使用注解的方式来管理事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
相关文章推荐
- Eclipse上安装springsource-tool-suite
- 重读《Java编程思想(第四版)》(1-9章)
- Java基础-多线程-③线程同步之synchronized
- Java基础-多线程-②多线程安全问题
- java bridge method
- Java基础-多线程-①线程的创建和启动
- 学Java必看:Java最常用方法总结(ImportNew年度好文)
- java实现菱形输出
- Java Servlet API中文说明文档
- Spring+SpringMVC+Hibernate+JPA+SpringData+Ehcache+C3p0+MySQL项目搭建
- 蛇形矩阵 Java实现
- struts2笔记06-ServletXxxAware接口
- spring事务管理全面分析
- java 中的访问修饰符权限等级
- struts2笔记05-ServletActionContext
- 九九乘法表
- java中关于json传图片的方法
- java堆内存的划分
- Java并发编程系列之十五:Executor框架
- java类加载机制