【配置】Spring和MyBatis整合
spring配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <context:property-placeholder location="classpath:application.properties" ignore-unresolvable="true"/>
<util:properties id="fmqProperties" location="classpath:fmq.properties" />
<!--
<context:property-placeholder location="classpath:fmq.properties" ignore-unresolvable="true"/>
--> <bean id="misDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${oracle.jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${oracle.jdbc.url}"></property>
<property name="user" value="${oracle.jdbc.username}"></property>
<property name="password" value="${oracle.jdbc.password}"></property>
<property name="initialPoolSize" value="10"></property>
<property name="maxPoolSize" value="15"></property>
<property name="maxIdleTime" value="30"></property> <!-- seconds -->
<property name="acquireIncrement" value="5"></property> <!-- increment connections when valid pooled connection size is 0 -->
<property name="idleConnectionTestPeriod" value="60"></property> <!-- test idle connection in pool per 60 seconds -->
</bean> <bean id="misSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="misDataSource" />
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
</bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="misSessionFactory" />
</bean> <bean id="baseDao" class="com.highershine.gdna2.dao.GdnaBaseDao">
<property name="sqlSessionTemplate" ref="sqlSession"></property>
</bean> <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="misDataSource"></property>
</bean> <tx:annotation-driven transaction-manager="transactionManager"/> <context:annotation-config />
<context:component-scan base-package="com.highershine.gdna2" /> </beans>
此处Dao继承org.mybatis.spring.support.SqlSessionDaoSupport
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.List; import javax.annotation.Resource; import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository; /**
* <p>
* Dao基类
* </p>
*
*
* @author lizhihua
*
*/
@Repository("baseDao")
public class GdnaBaseDao extends SqlSessionDaoSupport { @Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
super.setSqlSessionFactory(sqlSessionFactory);
}
/**
*插入数据
* @param statement
* @return
*/
public int insert(String statement)throws Exception{
return getSqlSession().insert(statement);
} /**
*根据对象插入数据
* @param statement
* @param parameter
* @return
*/
public int insert(String statement, Object parameter) throws Exception{
return getSqlSession().insert(statement, parameter);
}
/**
*删除数据
* @param statement
* @return
*/
public int delete(String statement) throws Exception{
return getSqlSession().delete(statement);
} /**
*根据条件删除数据
* @param statement
* @param parameter
* @return
*/
public int delete(String statement, Object parameter) throws Exception {
return getSqlSession().delete(statement, parameter);
} /**
*更新数据
* @param statement
* @return
*/
public int update(String statement) throws Exception{
return getSqlSession().update(statement);
} /**
*根据条件更新数据
* @param statement
* @param parameter
* @return
*/
public int update(String statement, Object parameter) throws Exception{
return getSqlSession().update(statement, parameter);
} /**
*查询单个实体
* @param <T>
* @param statement
* @return
*/
@SuppressWarnings("unchecked")
public <T> T selectOne(String statement) throws Exception {
return (T) getSqlSession().selectOne(statement);
} /**
*根据条件查询单个实体
* @param <T>
* @param statement
* @param parameter
* @return
*/
@SuppressWarnings("unchecked")
public <T> T selectOne(String statement, Object parameter) throws Exception {
return (T) getSqlSession().selectOne(statement, parameter);
}
/**
*查询列表
* @param <T>
* @param statement
* @return
*/
public <T> List<T> selectList(String statement) throws Exception {
return getSqlSession().selectList(statement);
} /**
*根据条件查询列表
* @param <T>
* @param statement
* @param parameter
* @return
*/
public <T> List<T> selectList(String statement, Object parameter) throws Exception{
return getSqlSession().selectList(statement, parameter);
} /**
*根据条件及分页信息查询列表
* @param <T>
* @param statement
* @param parameter
* @param rowBounds
* @return
*/
public <T> List<T> selectList(String statement, Object parameter,
RowBounds rowBounds) throws Exception{
return getSqlSession().selectList(statement, parameter, rowBounds);
} /**
*创建Blob对象
* @param inputStream
* @return
*/
public Blob createBlob(InputStream inputStream) throws Exception{
if (inputStream == null)
return null; byte[] buffer = new byte[1024];
int readlen = 0;
try {
Blob blob = getSqlSession().getConnection().createBlob();
OutputStream outputStream = blob.setBinaryStream(1); readlen = inputStream.read(buffer, 0, 1024);
while (readlen > 0) {
outputStream.write(buffer, 0, readlen);
outputStream.flush();
readlen = inputStream.read(buffer, 0, 1024);
}
return blob;
} catch (Exception e) {
}
return null;
} }
【配置】Spring和MyBatis整合的更多相关文章
- 【Spring 持久层】Spring 与 Mybatis 整合
持久层整合总述 1.Spring 框架为什么要与持久层技术进行整合? JavaEE开发需要持久层进行数据库的访问操作 JDBC.Hibernate.MyBatis 进行持久开发过程存在大量的代码冗余 ...
- Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...
- spring, spring mvc, mybatis整合文件配置详解
转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
- Mybatis学习--spring和Mybatis整合
简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...
- 九 spring和mybatis整合
1 spring和mybatis整合 1.1 整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...
- Mybatis学习(7)spring和mybatis整合
整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- SpringMVC, Spring和Mybatis整合案例一
一 准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二 整合思路 Dao层: 1.SqlMapConfig. ...
随机推荐
- 1084. Broken Keyboard (20)-水题
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm&g ...
- webpack简单原理及用法
前言 如果你已经对Webpack精通了或者至少一直在工作中使用它,请关闭当前浏览器标签,无视这篇文章. 这篇文章本意是写给我自己看的,作为一篇Cookbook供快速查询和上手用.原因是虽然工作中会涉及 ...
- 《Linux内核分析》第二周笔记 操作系统是如何工作的
操作系统是如何工作的 一.函数调用堆栈 1.三个法宝 计算机是如何工作的?(总结)——三个法宝(存储程序计算机.函数调用堆栈.中断机制) 1)存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: ...
- 《Linux内核分析》第一周学习小结 计算机是如何工作的?
<Linux内核分析>第一周.计算机是如何工作的? 20135204 郝智宇 一.存储程序计算机工作模型 1. 冯诺依曼体系结构: 数字计算机的数制采用二进制:计算机应该按照程 ...
- 第三周 构造一个简单的Linux系统MenuOS
一. Linux内核源代码简介 稳定版内核:Linux-3.18.6 Linux内核源代码的目录结构: arch目录:在Linux内核源代码里占有的比重很大,因为Linux内核支持很多的体系结构, ...
- struts2中的方法的调用
转载:http://blog.csdn.net/hephec/article/details/41808585 在Struts2中方法调用概括起来主要有三种形式: 第一种方式:指定method属性 & ...
- 使用maven的插件进行maven项目的打包
1 maven项目打包的插件有3种 maven-jar-plugin maven-assembly-plugin maven-shade-plugin 2 maven-jar-plugin 现在要新增 ...
- [转帖] K8S 常用命令
k8s常用命令 原贴地址 查看集群信息: [root@kubernetes-master pods]# kubectl cluster-info kubectl cluster-info展示结果 k ...
- Jenkins配置匿名用户拥有只读权限
场景:查看cucumber reporting测试报告时需要登陆,比较麻烦 解决:允许匿名用户拥有只读权限 操作:Jenkins->系统管理->全局安全配置->授权策略,勾选“All ...
- 10缓冲流、转换流、序列化流、Files
十.流 10.1 缓冲流 10.1.1 概述 缓冲流是对4个基本的FileXxx流的增强,所以也是4个流,按照数据类型进行分类 ...