当前主流的框架已经不是ssh了,前台偏向于springMvc代替struts ,后台的mybatis/ibatis的流行程度则超过了hibernate了,只有spring框架一直是流行框架里的中流砥柱。mybatis如何与spring交互呢?我为大家演示一下简单的交互过程。
整合版本:spring4.3.2和mybatis3.4.1
需要的核心整合jar包:mybatis-spring-1.3.0.jar(如果是其他版本,在网上找最新的连接包
https://github.com/mybatis)
其他相关的核心包请自行添加
<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql:///mybatis?characterEncoding=UTF-8">property>
<property name="username" value="root">property>
<property name="password" value="1234">property>
bean>
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:advice id="txAdive" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<tx:annotation-driven proxy-target-class="false" transaction-manager="txManager"/>
<aop:config>
<aop:pointcut expression="excution( * cn.hncu..*.(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdive"/>
aop:config>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" >
<value>classpath:mybatis-config.xmlvalue>
property>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean">constructor-arg>
bean>
<bean id="userDao" class="cn.hncu.dao.UserDaoJdbc">
<property name="sqlSession" ref="sqlSession">property>
bean>
<bean id="userService" class="cn.hncu.service.UserService">
<property name="dao" ref="userDao">property>
bean>
<bean id="userDao2" class="cn.hncu.dao.UserDaoJdbc2">
<property name="sqlSessionTemplate" ref="sqlSession">property>
bean>
<bean id="userService2" class="cn.hncu.service.UserService">
<property name="dao" ref="userDao2">property>
bean>
beans>
2.写javaBean User.java
package cn.hncu.domain;
public class User {
private String id;
private String name;
private String pwd;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
3.配置mybatis-config.xml和User.xml
mybatis-config.xml(项目src目录下)
<configuration>
<typeAliases>
<typeAlias type="cn.hncu.domain.User" alias="User"/>
typeAliases>
<mappers>
<mapper resource="cn/hncu/domain/User.xml" />
mappers>
configuration>
User.xml(User.java同目录)
<mapper namespace="user">
<select id="all" resultType="User" >
select *from users
select>
<insert id="insert" parameterType="User">
insert into users(id,name,pwd) values(#{id},#{name},#{pwd})
insert>
mapper>
3.设置dao层:
UserDao.java
package cn.hncu.dao;
import java.util.List;
import cn.hncu.domain.User;
public interface UserDAO {
public List query();
public void insert(User user);
}
UserDaoJdbc.java(纯底层模式)
package cn.hncu.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import cn.hncu.domain.User;
public class UserDaoJdbc implements UserDAO {
private SqlSession sqlSession;
@Override
public List query() {
return sqlSession.selectList("all");
}
public SqlSession getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public void insert(User user) {
getSqlSession().insert("insert", user);
}
}
UserDaoJdbc2.java(继承SqlSessionDaoSupport模式)
package cn.hncu.dao;
import java.util.List;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.hncu.domain.User;
public class UserDaoJdbc2 extends SqlSessionDaoSupport implements UserDAO {
@Override
public List query() {
return getSqlSession().selectList("all");
}
@Override
public void insert(User user) {
getSqlSession().insert("insert", user);
}
}
4.service层
IStudService.java
package cn.hncu.service;
import java.util.List;
import cn.hncu.domain.User;
public interface IUserService {
public List query();
public void insert(User user,User user2);
}
StudServiceImpl.java
package cn.hncu.service;
import java.util.List;
import cn.hncu.dao.UserDAO;
import cn.hncu.domain.User;
public class UserService implements IUserService {
private UserDAO dao;
@Override
public List query() {
return dao.query();
}
public UserDAO getDao() {
return dao;
}
public void setDao(UserDAO dao) {
this.dao = dao;
}
@Override
public void insert(User user,User user2) {
dao.insert(user);
dao.insert(user2);
}
}
5.测试代码:
测试代码一:
Demo.java
package cn.hncu;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import cn.hncu.domain.User;
public class Demo {
@Test//测试dataSource
public void demo(){
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContent.xml");
DataSource ds=act.getBean("dataSource", DataSource.class);
System.out.println(ds);
}
@Test//测试SqlSessionFactory
public void demo2() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContent.xml");
SqlSessionFactory ssf = (SqlSessionFactory) ctx.getBean("sqlSessionFactoryBean");
SqlSession s = ssf.openSession();
System.out.println( s );
}
//坑:版本匹配----mybatis-3.4.1 配 mybatis-spring-1.3.0
@Test//测试SqlSessionTemplate
public void demo3(){
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContent.xml");
SqlSession s=act.getBean("sqlSession", SqlSession.class);
System.out.println(s);
List list=s.selectList("all");
System.out.println(list);
}
}
测试代码二:
Demo2.java
package cn.hncu;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.hncu.domain.User;
import cn.hncu.service.IUserService;
public class Demo2 {
@Test//测试spring+mybatis---service+dao --dao完全是我们自己写
public void demo(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContent.xml");
IUserService service = ctx.getBean("userService", IUserService.class);
List list=service.query();
System.out.println(list);
}
@Test//测试spring+mybatis---service+dao ----mybatis推荐的方式: dao继承SqlSessionDaoSupport
public void demo2(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContent.xml");
IUserService service = ctx.getBean("userService2", IUserService.class);
List list=service.query();
System.out.println(list);
}
@Test//测试事务
public void demo3(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContent.xml");
IUserService service = ctx.getBean("userService2", IUserService.class);
User user=new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setName("1111");
user.setPwd("1111");
User user2=new User();
user2.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user2.setName("222222222222222222222222222222222222222222222" +
"222222222222222222222222222222222222222222222222222222222" +
"222222222222222222222222222222222222222222222222222222222222" +
"2222222222222222222222222222222222222222222222222222222222222222222222" +
"22222222222222222222222222222222222222222222222222222222");
user2.setPwd("2222");
service.insert(user, user2);
}
}