【配置】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. ...
随机推荐
- Beta版本冲刺(三)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- 老李的blog使用日记(2)
寥寥数语结束一个不曾期待的遇见,可还是剧情不会这样结束,他也会在我的时间里注册自己的专属账号,无论什么时候,他会时而需要被注视着,为了达到目的,即使不择手段,只为一次擦肩而过的邂逅,极短的一段时间,相 ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- [转帖][Bash Shell] Shell学习笔记
[Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html 阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...
- [财务知识] debt debit credit 的区别于联系
https://blog.csdn.net/sjpljr/article/details/70169303 剑桥词典解释分别为: Debt [C or U ] n.something, especia ...
- [转帖]2019 简易Web开发指南
2019 简易Web开发指南 2019年即将到来,各位同学2018年辛苦了. 不管大家2018年过的怎么样,2019年还是要继续加油的! 在此我整理了个人认为在2019仍是或者将成为主流的技术 ...
- java配置环境变量与常用技巧
一.java入门 --->java平台 •Java SE Java Platform,Standard Edition-Java平台标准版. •Java EE Java Platform,Ent ...
- linux下&、nohup与screen的比较
& 首先,linux进程是区分前台进程和后台进程的. 通常,在终端输入的命令执行的前台进程模式.如果一个命令要执行好久,就会阻塞住终端好久,不能进行其他工作,所以,我们可以把执行花费时间很长的 ...
- Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins问题的解决
今天换了个maven仓库,结果新建maven工程的时候,忽然报错: Cannot read lifecycle mapping metadata for artifact org.apache.mav ...
- 阿里云ECS提示RHSA-2017:3263: curl security update
服务器配置: 原因阿里云安装的CentOS 7.3的curl和libcurl的源不是最新的,会导致安全漏洞出现 方法首先还是要在实例列表远程连接打开终端. 1.更新ca-bundle1.备份: cp ...