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 [sun.misc.Launcher$AppClassLoader@66d3c617]
一、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>
<!-- mybatis配置 -->
<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/*.xml"/>
</bean>
<!-- 使用Mapper映射器,使用MapperScannerConfigurer扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 事务 需要三件事才可以 一具体的事务实现着 二事务管理器 三 aop 事务控制 不起作用-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource" />
</bean>
<!-- 容器自动扫描IOC组件 -->
<context:component-scan base-package="com"/>
</beans>
二、Mapper映射文件
<?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"><!-- namespace必须和Mapper接口类路径一致 -->
<mapper namespace="com.dao.TmCustomerDao">
<select id="findByPK" parameterType="String" resultType="TmCustomerPO">
select * from tm_customer where pk_customer=#{PK_CUSTOMER}
</select>
</mapper>
三、映射器接口
package com.dao;
import org.springframework.stereotype.Repository;
import com.pojo.TmCustomerPO;
@Repository
public interface TmCustomerDao {
public TmCustomerPO findByPK(String PK_CUSTOMER);
}
四、实体类
/*
Copyright (c) 2005 KWT, Inc. All Rights Reserved.
*/
package com.pojo;
public class TmCustomerPO {
private String vname;
private String pkCustomer;
private String creationtime;
private String modifier;
private String vmobile;
private String pkOrg;
private String pkGroup;
private String pkFinanceOrg;
public void setVname(String vname){
this.vname=vname;
}
public String getVname(){
return this.vname;
}
public void setPkCustomer(String pkCustomer){
this.pkCustomer=pkCustomer;
}
public String getPkCustomer(){
return this.pkCustomer;
}
public void setCreationtime(String creationtime){
this.creationtime=creationtime;
}
public String getCreationtime(){
return this.creationtime;
}
public void setModifier(String modifier){
this.modifier=modifier;
}
public String getModifier(){
return this.modifier;
}
public void setVmobile(String vmobile){
this.vmobile=vmobile;
}
public String getVmobile(){
return this.vmobile;
}
public void setPkOrg(String pkOrg){
this.pkOrg=pkOrg;
}
public String getPkOrg(){
return this.pkOrg;
}
public void setPkGroup(String pkGroup){
this.pkGroup=pkGroup;
}
public String getPkGroup(){
return this.pkGroup;
}
public void setPkFinanceOrg(String pkFinanceOrg){
this.pkFinanceOrg=pkFinanceOrg;
}
public String getPkFinanceOrg(){
return this.pkFinanceOrg;
}
}
五、测试代码
static TmCustomerDao tmCustomer;
@BeforeClass
public static void before(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextByMapper.xml");
tmCustomer=ctx.getBean(TmCustomerDao.class);
}
@Test
public void testSpringMyBatisByMapper() {
TmCustomerPO tmcustomer=tmCustomer.findByPK("CO201511010000010090");
System.out.println(tmcustomer.getVname());
}
引入外部数据源注意事项: uername会报错,所以加上system-properties-mode="FALLBACK"
<context:property-placeholder location="classpath:dbconnection.properties" system-properties-mode="FALLBACK"/>
<!-- 数据源 -->
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>