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 list = sqlSession.selectList( "userMapper.queryUsers");
  10. User user = sqlSession.selectOne( "userMapper.queryUserById", 2);

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


   
   
   
   
  1. "1.0" encoding= "UTF-8"?>
  2. "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. class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  15. "ignoreUnresolvablePlaceholders" value= "true"/>
  16. "order" value= "1"/>
  17. "locations">
  18. classpath:oracle.properties
  19. package= "cn.richinfo.**" />
  20. "dataSource" class= "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close">
  21. "driverClassName" value= "oracle.jdbc.OracleDriver"/>
  22. "url" value= "${oracle.datasource.url}" />
  23. "username" value= "${oracle.datasource.username}" />
  24. "password" value= "${oracle.datasource.password}" />
  25. "maxActive" value= "${oracle.datasource.maxActive}"/>
  26. "maxIdle" value= "${oracle.datasource.maxIdle}"/>
  27. "maxWait" value= "${oracle.datasource.maxWait}"/>
  28. "logAbandoned" value= "${oracle.datasource.logAbandoned}"/>
  29. "removeAbandoned" value= "${oracle.datasource.removeAbandoned}"/>
  30. "removeAbandonedTimeout" value= "${oracle.datasource.removeAbandonedTimeout}"/>
  31. "validationQuery" value= "select 1 from dual" />
  32. "transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">
  33. "dataSource" ref= "dataSource"/>
  34. "transactionManager" />
  35. "sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean">
  36. "dataSource" ref= "dataSource"/>
  37. "mapperLocations" value= "classpath:cn/richinfo/mapper/*.xml"/>
  38. "sqlSessionTemplate" class= "org.mybatis.spring.SqlSessionTemplate" scope= "prototype">
  39. "0" ref= "sqlSessionFactory"/>

并且在applicationContent.xml中导入

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

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


   
   
   
   
  1. namespace= "userMapper">
  2. "insertUser" parameterType= "cn.data.User">

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

你可能感兴趣的:(spring)