【配置】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. ...
随机推荐
- Final发布——视频博客
1.视频链接 视频上传至优酷自频道,地址链接:http://v.youku.com/v_show/id_XMzk1OTIwNTUwMA==.html?spm=a2h0j.11185381.listit ...
- Week11分数
- linux 常用命令-用户、用户组管理
注:本篇只涉及常用命令,全部命令可以通过help帮助查看. (1)type useradd #查看命令属于内部命令还是外部命令,内部命令是嵌在linux的shell中,外部命令存储在路径中 (2) ...
- Adobe X沙箱
一.Adobe X沙箱简介 Adobe Reader X自从引入沙箱以来,对其攻击的难度就提高了很多.Reader X的沙箱是基于Google的Chrome沙箱,Chrome是开源的,Reader X ...
- 应对Gradle联网问题、长时间卡在resolve dependencies的思路
1.出现这种情况,在首先考虑网络问题,依赖下载不下来尝试shadowsocks,未果. 2.检查防火墙问题,更换host,无法解决. 3.新建Gradle工程,依然卡在resolve dependen ...
- 深入理解ajax系列第二篇——请求方式
前面的话 在上一篇中,概要地介绍了XHR对象的使用.本文将详细介绍使用XHR对象发送请求的两种方式——GET和POST.下面将以实例的形式来详细说明 GET GET是最常见的请求类型,最常用于向服务器 ...
- 【刷题】BZOJ 3512 DZY Loves Math IV
Description 给定n,m,求 模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sampl ...
- 【刷题】BZOJ 1924 [Sdoi2010]所驼门王的宝藏
Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...
- debian9使用systemd部署etcd集群
在centos上,是可以直接使用yum安装etcd的: # yum list | grep etcd etcd.x86_64 3.2.9-3.el7 @extras 但是,在debian上却没有安装包 ...
- Java 使用 Enum 实现单例模式
在这篇文章中介绍了单例模式有五种写法:懒汉.饿汉.双重检验锁.静态内部类.枚举.如果涉及到反序列化创建对象时推荐使用枚举的方式来实现单例,因为Enum能防止反序列化时重新创建新的对象.本文介绍 Enu ...