Mybatis的sqlSessionTemplate直接使用

本文的持久化操作使用的是mybatis的sqlSessionTemplate,可用
除了mapper.xml中的sql语句 还可以用这个提供好的类来操作数据库


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

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


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <beans xmlns= "http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. xmlns:tx= "http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation= "
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  13. ">
  14. <bean class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  15. <property name= "ignoreUnresolvablePlaceholders" value= "true"/>
  16. <property name= "order" value= "1"/>
  17. <property name= "locations">
  18. <list>
  19. <value>classpath:oracle.properties</value>
  20. </list>
  21. </property>
  22. </bean>
  23. <!-- 自动扫描 -->
  24. <context:component-scan base- package= "cn.richinfo.**" />
  25. <!-- 数据库配置 -->
  26. <bean id= "dataSource" class= "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close">
  27. <property name= "driverClassName" value= "oracle.jdbc.OracleDriver"/>
  28. <property name= "url" value= "${oracle.datasource.url}" />
  29. <property name= "username" value= "${oracle.datasource.username}" />
  30. <property name= "password" value= "${oracle.datasource.password}" />
  31. <property name= "maxActive" value= "${oracle.datasource.maxActive}"/>
  32. <property name= "maxIdle" value= "${oracle.datasource.maxIdle}"/>
  33. <property name= "maxWait" value= "${oracle.datasource.maxWait}"/>
  34. <property name= "logAbandoned" value= "${oracle.datasource.logAbandoned}"/>
  35. <property name= "removeAbandoned" value= "${oracle.datasource.removeAbandoned}"/>
  36. <property name= "removeAbandonedTimeout" value= "${oracle.datasource.removeAbandonedTimeout}"/>
  37. <property name= "validationQuery" value= "select 1 from dual" />
  38. </bean>
  39. <bean id= "transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">
  40. <property name= "dataSource" ref= "dataSource"/>
  41. </bean>
  42. <tx:annotation-driven transaction-manager= "transactionManager" />
  43. <!-- 扫描包的方式注入所有的Mapper -->
  44. <bean id= "sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean">
  45. <property name= "dataSource" ref= "dataSource"/>
  46. <property name= "mapperLocations" value= "classpath:cn/richinfo/mapper/*.xml"/>
  47. </bean>
  48. <!-- 配置扫描器 -->
  49. <!--<bean class= "org.mybatis.spring.mapper.MapperScannerConfigurer">-->
  50. <!--<property name= "basePackage" value= "com.richinfo.**.dao" />-->
  51. <!--<property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" />-->
  52. <!--</bean>-->
  53. <bean id= "sqlSessionTemplate" class= "org.mybatis.spring.SqlSessionTemplate" scope= "prototype">
  54. <constructor-arg index= "0" ref= "sqlSessionFactory"/>
  55. </bean>
  56. </beans>

并且在applicationContent.xml中导入

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

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


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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值