java之mybatis整合spring
这篇讲解spring+mybatis的整合。
目录结构:

一. 整合spring的第一种方法
1. 新建 java 项目 : spring_mybatis
2.导入jar 包-----spring和mybatis的整合包,然后build
aopalliance.jar
aspectjweaver.jar
commons-logging.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.20-bin.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
3.编写 vo 类
在cn.vincent.vo下 User.java
package cn.vincent.vo;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private int age;
private int rileId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getRileId() {
return rileId;
}
public void setRileId(int rileId) {
this.rileId = rileId;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age
+ ", rileId=" + rileId + "]";
}
}
4.编写 映射文件
在cn.vincent.mapper下 UserMapper.xml
<?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="cn.vincent.mapper.UserMapper">
<select id="findAll" resultType="User">
select * from t_user
</select>
</mapper>
5.编写 dao
在cn.vincent.mapper下 UserMapper.java
package cn.vincent.mapper;
import java.util.List;
import cn.vincent.vo.User;
public interface UserMapper {
public List<User> findAll();
}
在cn.vincent.mapper.impl下 UserMapperImpl.java
package cn.vincent.mapper.impl; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import cn.vincent.mapper.UserMapper;
import cn.vincent.vo.User; @Repository("userMapper")
public class UserMapperImpl implements UserMapper { @Autowired
private SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession){
this.sqlSession=sqlSession;
} @Override
public List<User> findAll() {
return sqlSession.selectList("cn.vincent.mapper.UserMapper.findAll");
} }
6.编写 service
在 cn.vincent.service下 UserService.java
package cn.vincent.service;
import java.util.List;
import cn.vincent.vo.User;
public interface UserService {
public List<User> findAll();
}
在 cn.vincent.service下 UserServiceImpl.java
package cn.vincent.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import cn.vincent.mapper.UserMapper;
import cn.vincent.service.UserService;
import cn.vincent.vo.User; @Service("userService")
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
} @Override
public List<User> findAll() {
return userMapper.findAll();
} }
7. 编写mybatis的配置文件
mybatis.cfg.xml
<?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>
<package name="cn.vincent.vo"/>
</typeAliases>
<mappers>
<mapper resource="cn/vincent/mapper/UserMapper.xml"/>
</mappers>
</configuration>
8. 编写spring的配置文件
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--读取外部配置-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 根据mybatis的配置文件 来创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 表示以save开头的方法都需要事务
propagation 表示事务的传播特性
REQUIRED 查看当前是否有事务,如果有,使用当前事务,如果没有开启新事务
-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--expression 指明事务在哪里起作用
第一个* 表示所有返回值
第二个* 表示所有类
第三个* 表示类中的所有方法
.. 表示所有参数
-->
<aop:pointcut expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<context:component-scan base-package="cn.vincent"></context:component-scan>
</beans>
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
9.添加测试
在 test下的 cn.vincent.service下的 UserServiceTest.java
package cn.vincent.service; import java.util.List; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.vincent.vo.User; public class UserServiceTest { @Test
public void testFindAll(){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=ac.getBean(UserService.class);
List<User> list=userService.findAll();
for(User u:list){
System.out.println(u);
}
}
}
10. 运行测试
效果如下:

二. 第二种是去掉mybatis配置文件的配置方法
在beans.xml中修改 SqlSessionFactory的配置
<!-- 根据mybatis的配置文件 来创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="cn.vincent.vo"></property>
<property name="mapperLocations">
<list>
<value>classpath:cn/vincent/mapper/UserMapper.xml</value>
</list>
</property>
</bean>
三. 可以通过代理的方式来生成实现类的配置
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="1111"/>
</bean>
<!-- 根据mybatis的配置文件 来创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="cn.sxt.vo"></property>
<property name="mapperLocations">
<list>
<value>classpath:cn/sxt/mapper/UserMapper.xml</value>
</list>
</property>
</bean>
<!-- mapper的接口和mapper的映射文件在同一个包下,并且
mapper的接口名称和mapper的映射文件名相同
mapper接口中的方法名称和mapper映射文件中的id的名称一致
mapper映射文件中的namespace和mapper文件所在的包名+mapper文件名
该类将扫描指定的包 并且通过代理生成mapper接口的实现类 生成的类的id名称为
mapper接口首字母小写
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="basePackage" value="cn.vincent.mapper"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 表示以save开头的方法都需要事务
propagation 表示事务的传播特性
REQUIRED 查看当前是否有事务,如果有,使用当前事务,如果没有开启新事务
-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--expression 指明事务在哪里起作用
第一个* 表示所有返回值
第二个* 表示所有类
第三个* 表示类中的所有方法
.. 表示所有参数
-->
<aop:pointcut expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<context:component-scan base-package="cn.vincent"></context:component-scan>
</beans>
github地址:https://github.com/Vincent-yuan/spring_mybatis
java之mybatis整合spring的更多相关文章
- mybatis整合spring 之 基于接口映射的多对一关系
转载自:http://my.oschina.net/huangcongmin12/blog/83731 mybatis整合spring 之 基于接口映射的多对一关系. 项目用到俩个表,即studen ...
- JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程
1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...
- 160330、Mybatis整合Spring
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
- (转)MyBatis框架的学习(六)——MyBatis整合Spring
http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...
- Mybatis整合spring(适合小白)
目录 1.整合思路 2.整合需要的jar包 3.整合的步骤 4.Dao的开发的两种实现方式 6.Dao的开发的实现方式总结图 @ Mybatis整合spring其实就是SSM框架中SM的整合集成. 1 ...
- Mybatis整合spring详细教程(适合小白童鞋)
目录 1.整合思路 2.整合需要的jar包 3.整合的步骤 4.Dao的开发的两种实现方式 6.Dao的开发的实现方式总结图 @ Mybatis整合spring其实就是SSM框架中SM的整合集成. 1 ...
- JavaWeb_(Mybatis框架)MyBatis整合Spring框架
MyBatis + Spring整合开发 a)使用Spring容器用单例模式管理Mybatis的sqlSessionFactory:b)使用Spring管理连接池.数据源等:c)将Dao/Mapper ...
- mybatis整合spring获取配置文件信息出错
描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
随机推荐
- saltstack自动化运维工具搭建个人笔记
至于为什么选择saltstack,因为Puppet.Chef基于Ruby开发,而ansible.saltstack基于python开发,便于后期二次,良好的可移植性. 又,ansible基于SSH协议 ...
- Xshell远程登录
1.xshell由一台服务器a登录另一台服务器b sftp -oPort = root@ip 2.下载git /上传pull git b服务器想下载的目标文件目录 a服务器上的文件下载目录
- linux 下nc 命令的使用
linux 下nc 命令的使用 https://blog.51cto.com/samyubw/555247
- Video Architecture Search
Video Architecture Search 2019-10-20 06:48:26 This blog is from: https://ai.googleblog.com/2019/10/v ...
- Spring Boot-Error:(3, 32) java: 程序包org.springframework.boot不存在
问题分析 -由于加载的项目没有加载相应的依赖的包文件导致 解决方案 setting 选中图中的设置,点击apply,IDE就会自动下载所需要的包文件
- Android: NDK中的Android.mk和Application.mk
1. 简介 Android.mk 可用来描述要编译的某个具体模块的相关信息.比如:指定编译该模块时所需要的源文件.编译该模块时要链接的库文件.该模块编译完成后生成的库的名字等等.Applicatio ...
- Spring不能直接@autowired注入Static变量/ 关于SpringBoot的@Autowired 静态变量注入
昨天在编写JavaMail工具类的时候,静态方法调用静态变量,这是很正常的操作,当时也没多想,直接静态注入. @Component public class JavaMailUtil { @Autow ...
- 005_Philippines之行准备
一.去前必备的手续 (1)SSP SSP(Special Study Permit),又叫特殊学生许可,是由菲律宾移民局要求的,每一位前往菲律宾进行游学的学生都要办理的一种特殊许可,学校会统一进行办理 ...
- Linux下SQL Server安装及数据库迁移
有客户使用Linux服务器,SQL Server从2017开始已支持Linux,从未用过,今天测试了一下,比较简单,记录一下. Linux:CentOS 7.0 首先更新源: curl -o /etc ...
- scrapy中的Pipeline
当Item在Spider中被收集之后,它将会被传递到Item Pipeline,这些Item Pipeline组件按定义的顺序处理Item. 每个Item Pipeline都是实现了简单方法的Pyth ...