1.项目结构:
2.代码:
1.User.xml Article.java User.java Configuration.xml IUserOperation.java:参考mybatis快速使用
2.spring.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 数据库配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8"></property>
<!-- <property name="connection.url">
<value><![CDATA[jdbc:mysql:///jdbc?useUnicode=true&characterEncoding=UTF-8]]></value>
</property> -->
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="1"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<!--sqlSessionFactory工厂配置 使用mybatis继承spring的类 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 导入mybatis配置文件 -->
<property name="configLocation" value="config/Configuration.xml"></property>
</bean>
<!-- 重点 -->
<!-- 将权限交给spring来初始化管理,使用mybatis继承spring的类 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="com.mybatis.service.IUserOperation" />
</bean>
</beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 数据库配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8"></property>
<!-- <property name="connection.url">
<value><![CDATA[jdbc:mysql:///jdbc?useUnicode=true&characterEncoding=UTF-8]]></value>
</property> -->
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="1"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<!--sqlSessionFactory工厂配置 使用mybatis继承spring的类 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 导入mybatis配置文件 -->
<property name="configLocation" value="config/Configuration.xml"></property>
</bean>
<!-- 重点 -->
<!-- 将权限交给spring来初始化管理,使用mybatis继承spring的类 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="com.mybatis.service.IUserOperation" />
</bean>
</beans>
3.测试:UserTestSpring.java
package com.junit.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mybatis.bean.Article;
import com.mybatis.bean.User;
import com.mybatis.service.IUserOperation;
public class UserTestSpring {
private static ApplicationContext ctx;
static{
ctx = new ClassPathXmlApplicationContext("config/spring.xml");
}
@Test
public void test() {
//通过spring容器获取 接口bean的实例
IUserOperation mapper = (IUserOperation)ctx.getBean("userMapper");
//测试id=1的用户查询,根据数据库中的情况,可以改成你自己的.
System.out.println("得到用户id=1的用户信息");
User user = mapper.selectUserById(1);
System.out.println(user.getUserAddress());
//得到文章列表测试
System.out.println("得到用户id为1的所有文章列表");
List<Article> articles = mapper.getUserArticles(1);
for(Article article:articles){
System.out.println(article.getContent()+"--"+article.getTitle());
}
}
package com.junit.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mybatis.bean.Article;
import com.mybatis.bean.User;
import com.mybatis.service.IUserOperation;
public class UserTestSpring {
private static ApplicationContext ctx;
static{
ctx = new ClassPathXmlApplicationContext("config/spring.xml");
}
@Test
public void test() {
//通过spring容器获取 接口bean的实例
IUserOperation mapper = (IUserOperation)ctx.getBean("userMapper");
//测试id=1的用户查询,根据数据库中的情况,可以改成你自己的.
System.out.println("得到用户id=1的用户信息");
User user = mapper.selectUserById(1);
System.out.println(user.getUserAddress());
//得到文章列表测试
System.out.println("得到用户id为1的所有文章列表");
List<Article> articles = mapper.getUserArticles(1);
for(Article article:articles){
System.out.println(article.getContent()+"--"+article.getTitle());
}
}