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整合,布布扣,bubuko.com

Spring和MyBatis整合

上一篇:ClassPathXMLApplicationContext上下文加载过程


下一篇:pandas apply 草稿,等待合集