Mybatis的sqlSessionTemplate直接使用

本文的持久化操作使用的是mybatis的sqlSessionTemplate,可用

SqlSession sqlSession= sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
//增
int result = sqlSession.insert("userMapper.insertUsers",user);
//删
int result = sqlSession.delete("userMapper.deleteUsers",49);
//改
int result = sqlSession.update("userMapper.updateUsers",user);
//查
List<User> list = sqlSession.selectList("userMapper.queryUsers");
User user = sqlSession.selectOne("userMapper.queryUserById",2);

把一个User对象写入数据库,相当于之前一直使用的parameterType="User",它不需要使用dao层,而直接用sqlSessionTemplate去使用mapper.xml里具体的持久化方法。它在spring-mybatis.xml中的配置为:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
                           http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                           ">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="order" value="1"/>
        <property name="locations">
            <list>
                <value>classpath:oracle.properties</value>
            </list>
        </property>
    </bean>

    <!-- 自动扫描 -->
    <context:component-scan base-package="cn.richinfo.**" />

    <!-- 数据库配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"  value="oracle.jdbc.OracleDriver"/>
        <property name="url" value="${oracle.datasource.url}" />
        <property name="username" value="${oracle.datasource.username}" />
        <property name="password" value="${oracle.datasource.password}" />
        <property name="maxActive" value="${oracle.datasource.maxActive}"/>
        <property name="maxIdle"   value="${oracle.datasource.maxIdle}"/>
        <property name="maxWait"   value="${oracle.datasource.maxWait}"/>
        <property name="logAbandoned" value="${oracle.datasource.logAbandoned}"/>
        <property name="removeAbandoned" value="${oracle.datasource.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="${oracle.datasource.removeAbandonedTimeout}"/>
        <property name="validationQuery" value="select 1 from dual" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 扫描包的方式注入所有的Mapper -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:cn/richinfo/mapper/*.xml"/>
    </bean>

    <!-- 配置扫描器 -->
    <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
        <!--<property name="basePackage" value="com.richinfo.**.dao" />-->
        <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />-->
    <!--</bean>-->

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

</beans>

并且在applicationContent.xml中导入

 <import resource="classpath*:spring-mybatis.xml" />

mqpper.xml中的写法与spring结合mybatis一直使用的那一套一致。这里要注意的是:

<mapper namespace="userMapper">
<insert id="insertUser" parameterType="cn.data.User">

如上例,在使用中要用"mapperName.methodName"

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值