spring整合mybatis
来源:互联网 发布:皇甫圣华淘宝 编辑:程序博客网 时间:2024/06/07 13:42
一、导入jar包
1、mybatis-spring-1.2.5.jar
2、spring-jdbc-4.3.5.jar
3、Druid-1.0.26.jar//使用的是阿里巴巴旗下的Druid数据库连接池
二、用spring管理SqlSessionFactory
spring.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"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.oa"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加载jdbc属性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:dbconfig.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 数据库基本信息配置 --> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="driverClassName" value="${driverClassName}" /> <property name="filters" value="${filters}" /> <!-- 最大并发连接数 --> <property name="maxActive" value="${maxActive}" /> <!-- 初始化连接数量 --> <property name="initialSize" value="${initialSize}" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${maxWait}" /> <!-- 最小空闲连接数 --> <property name="minIdle" value="${minIdle}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="${logAbandoned}" /> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybaits/SqlMapConfig.xml"></property> </bean></beans>
这样,SqlSessionFactory的bean就在spring中创建好了,
三、现在把1、得到dao层接口的实现类和2、事务处理也交给spring处理:
1、
<!-- 创建dao层Mapper接口的实现类的bean --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.oa.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
现在直接在service中注入Mapper就可以了:
2、在spring中配置事务:
<!-- 配置事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
头文件:
现在要导入jar包:
spring-tx.jar
spring-aop.jar
spring-aspectjweaver.jar
事务配置好之后:
<!-- 事务管理开始 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务如何管理 --> <tx:advice id="myadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"></tx:method> <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"></tx:method> <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"></tx:method> </tx:attributes> </tx:advice> <aop:config> <!-- 管哪里 --> <aop:pointcut expression="execution(*com.oa.service..*(..))" id="mycut"/> <!-- 通过myadvice管理mycut --> <aop:advisor advice-ref="myadvice" pointcut-ref="mycut"/> </aop:config> <!-- 事务管理结束 -->
事务层只关注事务:
package com.oa.service.impl;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.oa.dao.ProfessionalMapper;import com.oa.entity.Professional;import com.oa.service.ProService;@Service("proService")public class ProServiceImpl implements ProService{ @Autowired private SqlSessionFactory sqlSessionFactory; @Autowired private ProfessionalMapper mapper; /** * 增加一个专业 */ public int addPro(Professional pro) { int result = 0; Professional professional = mapper.selectByName(pro.getProname()); if(professional == null){ result = mapper.insert(pro); }else{ result=-1; } return result; } /** * 得到专业列表 */ public List<Professional> getAllPros() { List<Professional> pros = null; pros = mapper.selAllPros(); return pros; } /** * 删除一个专业 */ public int delPro(int proid) { int result = 0; result = mapper.deleteByPrimaryKey(proid); return result; } /** * 修改一个专业 */ public int editPro(Professional pro) { int result = 0; result = mapper.updateByPrimaryKey(pro); return result; } /** * 查找一个专业 */ public Professional selProById(int proid) { Professional pro = null; pro = mapper.selectByPrimaryKey(proid); return pro; }}
0 0
- Mybatis -- Mybatis整合Spring
- 【Mybatis】Mybatis整合spring
- 【Spring+Mybatis】Spring整合Mybatis
- MyBatis-----2、MyBatis整合Spring
- Spring整合Mybatis
- spring整合myBatis
- MyBatis-Spring框架整合
- MyBatis+Spring整合示例
- MyBatis+Spring整合示例
- MyBatis+Spring整合方案
- spring + struts2 + mybatis整合
- Spring整合Mybatis
- Mybatis整合Spring
- Spring Mybatis 整合
- spring整合MyBatis
- Mybatis整合Spring
- MyBatis+Spring整合示例
- maven+spring+mybatis整合
- 微信小程序入口在哪
- leetcode--109. Convert Sorted List to Binary Search Tree
- CNN+caffe学习4:自己训练网络全过程
- PythonUI_QtDesigner的ui文件转为py文件
- Note05--创建表
- spring整合mybatis
- 最新的spark2.1.0 ReleaseNote[Release date: 18/Dec/16]
- ZCMU-1425-Careless Tony
- python/pandas数据分析(十五)-聚合与分组运算实例
- linux中搭建svn版本控制器(centos)
- tensorflow相关问题
- Note06--Oracle中TO_DATE格式
- 手把手教你使用GitHub
- 常见Android Native崩溃及错误原因