JavaEE框架——mybatis和spring整合
Posted wangjian_an
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaEE框架——mybatis和spring整合相关的知识,希望对你有一定的参考价值。
mybatis和spring的联合使用
当前主流的框架已经不是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)
其他相关的核心包请自行添加
连接步骤
- 在项目里写上spring的配置文件application.xml(src目录下)
<?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: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><!-- 设置数据源正式项目用DataSourceTransactionManager 测试用SimpleTransactionManager-->
<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="txManager" pointcut-ref="pointcut"/> 可以-->
<aop:advisor advice-ref="txAdive"/>
</aop:config>
<!-- 配置mybatis -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" >
<value>classpath:mybatis-config.xml</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
</bean>
<!-- 配置业务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"> <!-- daoJdbc利用spring自动配置 -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property> -->
<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目录下)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<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同目录)
<?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="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<User> 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<User> 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<User> 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<User> 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<User> 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<User> 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<User> 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<User> 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);
以上是关于JavaEE框架——mybatis和spring整合的主要内容,如果未能解决你的问题,请参考以下文章
清华毕业大佬用了一个项目就把SpringMVC+Spring+MyBatis给讲完了
Java EE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系
企业快速开发平台Spring Cloud+Spring Boot+Mybatis+ElementUI 实现前后端分离