【配置】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. ...
随机推荐
- c#版flappybird 未完全实现
这些天开始在深圳找工作,想着把从前有些淡忘的技术再温故下.看到尊敬的<传智播客>有一期公开课,讲的是用c#编写flappybird小游戏,也就自己搜了下游戏资源,也来试试看. 其实用到的技 ...
- Daily Scrum - 12/0809
Meeting Minutes (08的Scrum报告放在word里,publish没有成功,所以这是08-09的报告,抱歉…): 卡片翻转的效果确认完成: 按钮蓄力的效果确认完成: 按钮上移的效果确 ...
- React组件继承的由来
没有显式继承的时候我们这么写: import * as React from "react"; export interface HelloProps { compiler: st ...
- PAT 1064 朋友数
https://pintia.cn/problem-sets/994805260223102976/problems/994805267416334336 如果两个整数各位数字的和是一样的,则被称为是 ...
- 如何运行spring项目,并打成jar包进行发布
一.创建spring项目 1.创建项目 2.创建moudule,选择java类型即可. 3.创建lib文件,引入spring的4个核心包spring-beans.spring-context.spri ...
- Ubutnu linux 下升级python版本,以2.x升级到3.x为例
Linux操作系统一般 都会自带python,但是python版本会比主流低,故升级python, 主要思路:自带的python的链接link文件是在/usr/bin 下,采用sudo apt-get ...
- ES6之Promise用法详解
一 前言 本文主要对ES6的Promise进行一些入门级的介绍.要想学习一个知识点,肯定是从三个方面出发,what.why.how.下面就跟着我一步步学习吧~ 二 什么是Promise 首先是what ...
- 【设计模式】—— 创建者模式Builder
前言:[模式总览]——————————by xingoo 模式意图 一个对象的创建十分复杂,为了区分构建过程和使用过程,因此分开.使用一个Director类进行对象的创建,Builder规定了这个创建 ...
- 02 使用Mybatis的逆向工程自动生成代码
1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...
- MT【82】凸函数
评:对于(3)几何上来看要满足性质$P$图像来看必须下凸.这样区间中点$x=2$处不可能为最大.(4)的形式让我想起在证明算术几何平均不等式时历史上著名的柯西反向归纳证明: