参考CSDN-------http://blog.csdn.net/kutejava/article/details/9164353#t3
1. 创建spring的配置文件 applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- 貌似DBCP连接时必须使用该name -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123,456a" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <!-- 注入dataSource 和 Mybatis的主配置文件 -->
<property name="configLocation" value="conf.xml"/>
</bean>
<bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/> <!-- 注入sqlSessionFactory 和 接口编程的接口-->
<property name="mapperInterface" value="com.imut.inf.IUser"/>
</bean>
</beans>
2. 测试类
public class MyBatisSpringTest {
private static ApplicationContext ac;
static{
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public static void main(String[] args) {
IUser iuser = (IUser) ac.getBean("UserMapper");
User user = iuser.getUserById(2);
System.out.println(user.getName());
}
}