Mybatis第五篇【Mybatis与Spring整合】
Mybatis与Spring整合
既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了!
以下使用的是Oracle数据库来进行测试
导入jar包
- aopalliance.jar
- asm-3.3.1.jar
- aspectjweaver.jar
- c3p0-0.9.1.2.jar
- cglib-2.2.2.jar
- commons-logging.jar
- log4j-1.2.16.jar
- mybatis-3.1.1.jar
- mybatis-spring-1.1.1.jar
- mysql-connector-java-5.1.7-bin.jar
- ojdbc5.jar
- org.springframework.aop-3.0.5.RELEASE.jar
- org.springframework.asm-3.0.5.RELEASE.jar
- org.springframework.beans-3.0.5.RELEASE.jar
- org.springframework.context-3.0.5.RELEASE.jar
- org.springframework.core-3.0.5.RELEASE.jar
- org.springframework.expression-3.0.5.RELEASE.jar
- org.springframework.jdbc-3.0.5.RELEASE.jar
- org.springframework.orm-3.0.5.RELEASE.jar
- org.springframework.transaction-3.0.5.RELEASE.jar
- org.springframework.web.servlet-3.0.5.RELEASE.jar
- org.springframework.web-3.0.5.RELEASE.jar
创建表
create table emps(
eid number(5) primary key,
ename varchar2(20),
esal number(8,2),
esex varchar2(2)
);
创建实体
package entity;
/**
* 员工
* @author AdminTC
*/
public class Emp {
private Integer id;
private String name;
private Double sal;
private String sex;
public Emp(){}
public Emp(Integer id, String name, Double sal, String sex) {
this.id = id;
this.name = name;
this.sal = sal;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
创建实体与表的映射文件
<?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="empNamespace">
<resultMap type="entity.Emp" id="empMap">
<id property="id" column="eid"/>
<result property="name" column="ename"/>
<result property="sal" column="esal"/>
<result property="sex" column="esex"/>
</resultMap>
<!-- 增加员工 -->
<insert id="add" parameterType="entity.Emp">
insert into emps(eid,ename,esal,esex) values(#{id},#{name},#{sal},#{sex})
</insert>
</mapper>
创建Mybatis映射文件配置环境
数据库的信息交由Spring管理!Mybatis配置文件负责加载对应映射文件即可
<mappers>
<mapper resource="zhongfucheng/entity/EmpMapper.xml"/>
</mappers>
</configuration>
配置Spring核心过滤器【也是加载总配置文件】
<!-- 核心springmvc核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
配置数据库信息、事务
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置C3P0连接池,目的:管理数据库连接 -->
<bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:ZHONGFUCHENG"/>
<property name="user" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->
<bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="comboPooledDataSourceID"/>
</bean>
<!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->
<bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSourceID"/>
</bean>
<!-- 配置事务通知,即让哪些方法需要事务支持 -->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切面,即让哪些包下的类需要事务 -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* zhongfucheng.service.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
</aop:config>
<!--扫描注解-->
<context:component-scan base-package="zhongfucheng"/>
</beans>
创建Dao、Service、Action
@Repository
public class EmpDao {
@Autowired
private SqlSessionFactory sqlSessionFactory;
/**
* 增加员工
*/
public void add(Emp emp) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("empNamespace.add", emp);
sqlSession.close();
}
}
@Service
public class EmpService {
@Autowired
private zhongfucheng.dao.EmpDao empDao;
public void addEmp(Emp emp) throws Exception {
empDao.add(emp);
}
}
@Controller
@RequestMapping("/emp")
public class EmpAction {
@Autowired
private EmpService empService;
@RequestMapping("/register")
public void register(Emp emp) throws Exception {
empService.addEmp(emp);
System.out.println("注册成功");
}
}
JSP页面测试
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>员工注册</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/emp/register.action" method="POST">
<table border="2" align="center">
<tr>
<th>编号</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>姓名</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>薪水</th>
<td><input type="text" name="sal"></td>
</tr>
<tr>
<th>性别</th>
<td>
<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女" checked/>女
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="注册"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Mybatis第五篇【Mybatis与Spring整合】的更多相关文章
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- mybatis第二天_拓展——与spring整合以及逆向工程
一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...
- 【mybatis源码学习】与spring整合Mapper接口执行原理
一.重要的接口 org.mybatis.spring.mapper.MapperFactoryBean MapperScannerConfigurer会向spring中注册该bean,一个mapper ...
- Mybatis系列(五):mybatis逆向工程
一.背景 在实际开发中我们会自己去写mapper映射文件,接口,数据库表对应的实体类,如果需求任务比较少,咱们还可以慢慢的一个一个去写,但是这是不现实的,因为在工作中我们的任务是很多的,这时mybat ...
- Mybatis笔记五:Mybatis的全局配置文件Configuration.xml讲解
从 XML 中构建 SqlSessionFactory 每个基于Mybatis应用都是以一个SqlSessionFactory实例为中心.SqlSessionFactory实例可以由SqlSessio ...
- 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源
目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...
- Spring整合MyBatis(二)Spring整合MyBatis
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 了解了MyBatis的独立使用过程后,我们再看看它与Spring整合的 ...
- MyBatis 源码篇-MyBatis-Spring 剖析
本章通过分析 mybatis-spring-x.x.x.jar Jar 包中的源码,了解 MyBatis 是如何与 Spring 进行集成的. Spring 配置文件 MyBatis 与 Spring ...
- MyBatis 源码篇-Transaction
本章简单介绍一下 MyBatis 的事务模块,这块内容比较简单,主要为后面介绍 mybatis-spring-1.**.jar(MyBatis 与 Spring 集成)中的事务模块做准备. 类图结构 ...
随机推荐
- Orleans—一些概念
Orleans-一些概念 这是Orleans系列文章中的一篇.首篇文章在此 这个文章聊一聊Orleans的概念.以下文章大部分翻译自官方教程,还有一些结合实际的应用经验,并对以前文章留下的坑进行填平. ...
- Linux环境下安装禅道
1.下载禅道包 http://dl.cnezsoft.com/zentao/7.3/ZenTaoPMS.7.3.stable.zbox_64.tar.gz http://dl.cnezsoft.c ...
- css知识点
css知识点 一.盒模型知识 border: 边框 border-width:边框的宽度 border-color:边框的颜色 border-style:边框的线型 border-top:上边框 bo ...
- js中的访问器属性中的getter和setter函数实现数据双向绑定
嗯,之前在读js红宝书的时候,在对象那一章有介绍属性类型.第一种数据类型指的是数据属性,第二种是访问器属性.在初识vue的时候,其双向数据绑定也是基于访问器属性中的getter和setter函数原理来 ...
- 获取windows所有用户名
#include <LM.h> #pragma comment(lib, "netapi32.lib") // See more: http://msdn.micros ...
- PHP 字符串替换
这是2017上半年的第一篇学习笔记,也是最后一篇,捂脸- 在前几天的工作中,关于"银行卡"页面原型如下,其中,不同银行卡的卡号只保留了最后四位数字可以显示,其他数字均用*字符隐藏了 ...
- 为shell布置陷阱:trap捕捉信号方法论
本文目录: 1.1 信号说明 1.2 trap布置陷阱 1.3 布置完美陷阱必备知识 家里有老鼠,快消灭它!哎,又给跑了.老鼠这小东西跑那么快,想直接直接消灭它还真不那么容易.于是,老鼠药.老鼠夹子或 ...
- 微信原图泄露的只能是 Exif ,你的隐私不在这!!!
版权声明: 本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有. 每周会统一更新到这里,如果喜欢,可关注公众号获取最新文章. 未经允许,不得转载. 序 最近很多公众号都推送了关 ...
- Java常见算法整理
兔子问题(斐波那契数列规律) 台阶问题 (兔子问题变种,递归规律) 素数问题(判断素数.质数方式) 水仙花数问题(数字分解) 查找算法(二分查找) 排序算法(选择排序,冒泡排序,快速排序) 兔子问题, ...
- ORA-06575:程序包或函数处于无效状态
今天一个朋友问我下面这段sql语句的问题,我发现了他竟然把程序员的编程思想带入了oracle,虽然是错误的,但也是很经典的错误啊. create or replace package p_view_p ...