Spring+Mybatis操作資料庫(使用sqlSessionTemplate)
jsr250-api:
<dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
注意事項:mybatis-spring使用的是1.3.2的,此處mybatis版本使用的是3.4.1的,
如果用mybatis3.2及以下版本的會報:
java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z
如果使用mybatis3.4.6版本會報:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionTemplate': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.mybatis.spring.SqlSessionTemplate] from ClassLoader [
[email protected]]
一、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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"> <!-- <context:property-placeholder location="classpath:dbconnection.properties" /> --> <!-- 資料來源 --> <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@dx.abc.com:152:orcl"/> <property name="username" value="NC"/> <property name="password" value="2016"/> </bean> <!-- 而像這種使用介面實現的方式 是使用sqlsessionTemplate來進行操作的,他提供了一些方法 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="DataSource"/> <!-- mybatis配置檔案路徑 --> <!-- <property name="configLocation" value="classpath:mybatis-config.xml"/> --> <!-- 此處配置了com.pojo,在mapper對映檔案中就不用寫返回實體類的包名了 --> <property name="typeAliasesPackage" value="com.pojo"/> <!-- 實體類對映檔案路徑 --> <property name="mapperLocations" value="classpath:com/mapper/TmUserNoMapper.xml"/> </bean> <!-- 配置sqlSessionTemplate --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- <bean id="tmUserDaoImpl" class="com.dao.TmUserDaoImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> --> <!-- 事務 需要三件事才可以 一具體的事務實現著 二事務管理器 三 aop 事務控制 不起作用--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="DataSource" /> </bean> <!--6 容器自動掃描IOC元件 --> <context:component-scan base-package="com"/> </beans>
二、對映檔案
namespace不一定與對映器名稱一致,id也可不予對映器方法名一致,必須要寫resultMap否則返回結果一直為null,property為實體類的屬性值,column為資料庫欄位名
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
<resultMap id="tmuserMap" type="com.pojo.TmUserPO"> <id property="pkUser" column="pk_user"/> <result property="realName" column="real_name"/> <result property="userName" column="user_name"/> </resultMap> <select id="findAll" parameterType="String" resultType="TmUserPO" resultMap="tmuserMap"> select * from tm_user where real_name=#{real_name} </select> </mapper>
三、Dao
package com.dao;
import com.pojo.TmUserPO;
public interface TmUserDao {
public TmUserPO find(String real_name);
}
package com.dao;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.pojo.TmUserPO;
@Repository
public class TmUserDaoImpl implements TmUserDao {
@Autowired
SqlSessionTemplate sqlSessionTemplate;
public TmUserPO find(String real_name) {
return sqlSessionTemplate.selectOne("test.findAll",real_name);
}
}
四、實體類pojo
/*
Copyright (c) 2005 KWT, Inc. All Rights Reserved.
*/
package com.pojo;
import java.util.Date;
public class TmUserPO {
private String pkFinance;
private String pkOrg;
private String pkUser;
private String pkGroup;
private String userName;
private String realName;
private String userPhone;
private Date createDate;
private String password;
public void setPkFinance(String pkFinance){
this.pkFinance=pkFinance;
}
public String getPkFinance(){
return this.pkFinance;
}
public void setPkOrg(String pkOrg){
this.pkOrg=pkOrg;
}
public String getPkOrg(){
return this.pkOrg;
}
public void setPkUser(String pkUser){
this.pkUser=pkUser;
}
public String getPkUser(){
return this.pkUser;
}
public void setPkGroup(String pkGroup){
this.pkGroup=pkGroup;
}
public String getPkGroup(){
return this.pkGroup;
}
public void setUserName(String userName){
this.userName=userName;
}
public String getUserName(){
return this.userName;
}
public void setRealName(String realName){
this.realName=realName;
}
public String getRealName(){
return this.realName;
}
public void setUserPhone(String userPhone){
this.userPhone=userPhone;
}
public String getUserPhone(){
return this.userPhone;
}
public void setCreateDate(Date createDate){
this.createDate=createDate;
}
public Date getCreateDate(){
return this.createDate;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return this.password;
}
}
五、測試方法
static TmUserDaoImpl tmUser;
@BeforeClass
public static void before(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextNoMapper.xml");
tmUser=(TmUserDaoImpl)ctx.getBean("tmUserDaoImpl");
}
@Test
public void testSpringMyBatisNoMapper() {
TmUserPO tmuser=tmUser.find("姚景明");
System.out.println(tmuser.getUserName());
}