将Mybatis和Spring 框架集成到一起
来源:互联网 发布:javascript数字格式化 编辑:程序博客网 时间:2024/06/06 04:49
1).集成Spring
1-1).引入jar包
spring_home/libs/*.jar(不包含源码和说明jar包)
commons-logging-1.1.1.jar
1-2).引入配置文件
2-1). 引入jar包
MYBATISHOME/mybatis-3.2.1.jar
MYBATISHOME/lib/*.jar
mysql-connector-java-5.0.6-bin.jar
2-2).引入框架的配置文件
1).主配置文件 mybatis-config.xml
3).集成两个框架
3-1).引入jar包
mybatis-spring-1.2.0.jar
C3p0连接池:c3p0-0.9.1.2.jar
AOP:libs/aopalliance-1.0.jar,libs/aspectjweaver.jar
3-2).修改spring的配置文件,增加集成的相关配置
4).增加程序代码,进行测试
1-1).引入jar包
spring_home/libs/*.jar(不包含源码和说明jar包)
commons-logging-1.1.1.jar
1-2).引入配置文件
applicationContext.xml
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: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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="no" default-lazy-init="false" > <context:component-scan base-package="com.spring.pmes.*" /> <!-- 读取外部配置文件的属性,这个配置文件配置了连接数据库的信息 --> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:DBConfig.properties</value> </property> </bean><!-- 数据源,连接池 c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><!-- <property name="initialPoolSize" value="${initialPoolSize}"></property><property name="maxPoolSize" value="${maxPoolSize}"></property><property name="maxIdleTime" value="${maxIdleTime}"></property><property name="checkoutTimeout" value="${checkoutTimeout}"></property> --></bean><!-- 数据源,连接池 DBCP <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"><property name="driverClassName" value="${driver}"></property><property name="url" value="${url}"></property><property name="username" value="${user}"></property><property name="password" value="${password}"></property><property name="initialSize" value="${initialSize}"></property><property name="maxActive" value="${maxActive}"></property><property name="maxIdle" value="${maxIdle}"></property><property name="maxWait" value="${maxWait}"></property></bean>--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations"> <list> <value>classpath:mapper-user.xml</value> </list> </property></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 事务通知 Advice, Aspect是切面,是一个抽象的概念,Advice是具体存在的Aspect AOP增强器--><tx:advice id="transactionAdvice" transaction-manager="transactionManager"><tx:attributes><!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/><tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/><tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/> --><tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/><tx:method name="query*" read-only="true"/></tx:attributes></tx:advice><!-- 切入* com.spring.service.*.*(..)第一个* 表示的是方法的返回值第二个* 表示的是service包中的所有类第三个*表示的是所有类中的所有方法 --><aop:config><aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.spring.pmes.service.*.*(..))"/></aop:config> </beans>2).集成Mybatis
2-1). 引入jar包
MYBATISHOME/mybatis-3.2.1.jar
MYBATISHOME/lib/*.jar
mysql-connector-java-5.0.6-bin.jar
2-2).引入框架的配置文件
1).主配置文件 mybatis-config.xml
2).映射文件 例如mapper-user.xml
mybatis-config.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.spring.pmes.User" alias="UserBean"/> </typeAliases>--></configuration>
mapper-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="test"> <!-- 查询标签 id : sql映射ID,可以和SQL进行绑定,当访问这个ID时,等同于访问绑定的sql文 resultType : 将查询结果转换为指定的类型(类的完整名称)。 --><!-- <select id="queryByUserId" resultType="com.spring.pmes.User">select * from t_user where id=#{id}</select> --></mapper>
3).集成两个框架
3-1).引入jar包
mybatis-spring-1.2.0.jar
C3p0连接池:c3p0-0.9.1.2.jar
AOP:libs/aopalliance-1.0.jar,libs/aspectjweaver.jar
3-2).修改spring的配置文件,增加集成的相关配置
4).增加程序代码,进行测试
3-2).在applicationContext.xml 中已有体现
阅读全文
0 0
- 将Mybatis和Spring 框架集成到一起
- Spring集成MyBatis框架
- Spring集成MyBatis框架
- 采用spring mvc 和mybatis框架 将excel数据导入到Mysql数据库
- mybatis和spring集成
- Spring+Spring MVC+Shiro+Mybatis框架集成
- CXF框架集成到Spring
- spring MVC和Mybatis集成
- mybatis 和集成Spring使用
- spring MVC和Mybatis集成
- mybatis和spring集成核心
- spring和mybatis的集成
- Spring+SpringMVC+Mybatis+Velocity框架集成
- ssm(spring+springMVC+Mybatis)框架集成Mongodb
- 【springmvc】Springmvc+spring+Mybatis框架集成
- SSM框架集成(一)mybatis+spring
- 将Flex与Spring框架集成
- 将Flex与Spring框架集成
- 微信小程序农阳历日历选择器农阳历日期互转
- 开源项目QtXlsxWriter编译成静态库(VS2013 + Qt5.4.2 64位)
- java加密、解密方法
- 变量和函数提升
- javascript函数之局部变量和全局变量
- 将Mybatis和Spring 框架集成到一起
- 多重渲染的同步写入
- 鼠标点击获取在某div的相对位置
- Intellij IDEA 2016.3.4 注册激活
- Leetcode算法学习日志-594 Longest Harmonious Subsequence
- java线程同步工具类总结
- Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上
- 【论文阅读笔记】Deep Learning based Recommender System: A Survey and New Perspectives
- 三子棋(智能版)