Java学习笔记-spring整合mybatis
这个项目就是一个例子,只有添加图书的功能:
项目架构:
resource:
整合流程:
1.pom文件节点,这两个是整合用的,其他节点不再赘述:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.</version>
</dependency>
如果mybatis用的是3.4.1,那么mybatis-spring版本要用1.3.0
2.applicationContextDay06.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:p="http://www.springframework.org/schema/p"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--.识别jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--.数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--<property name="maxActive" value=""/>-->
</bean>
<!--.工厂配置 datasource+mybatis-config-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml"/>
<!--.Dao层Mapper注入-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="cn.happy.day06zhenghe.dao"/>
<!--.service-->
<bean id="bookService" class="cn.happy.day06zhenghe.service.impl.BookServiceImpl" p:bookDAO-ref="IBookDAO"/>
<!--.transactionManager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
<!--.AspectJ AOP 配置事务-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addBook" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--切点-->
<aop:pointcut id="pointcut" expression="execution(* *..day06zhenghe.service.*.*(..))"/>
<!--顾问-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
3.jdbc.properties,这里把四个串配上就行了。
4.mybatis,不用配置environments和mappers了,可以setting和alias,也可以直接干掉这个文件。
5.测试类:
public class Test20171021 {
@Test
public void test01(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextDay06.xml");
IBookService bookService = (IBookService)ctx.getBean("bookService");
Book book = new Book();
book.setBookname("放学后");
book.setBookprice((double) );
bookService.addBook(book);
}
}
Java学习笔记-spring整合mybatis的更多相关文章
- mybatis学习笔记 spring与mybatis整合
转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...
- (摘录笔记)JAVA学习笔记SSH整合搭建项目
1:当然是导jar包啦: struts2: spring: hibernate: 至于这些jar包是什么作用,我想就不必我解释了,大家都懂得,ssh2基本的jar包: 还有一些其他jar包:strut ...
- Spring Boot学习笔记(五)整合mybatis
pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Java开发学习(十四)----Spring整合Mybatis及Junit
一.Spring整合Mybatis思路分析 1.1 环境准备 步骤1:准备数据库表 Mybatis是来操作数据库表,所以先创建一个数据库及表 create database spring_db cha ...
- Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)
http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
随机推荐
- 使用 matlab 数据集的生成(generate datasets)
一般手工生成的数据集(artificial datasets),通常用于实验部分最开始的演示和示意,用于对结果的一种精确计算和量化分析. 1. Swiss/Helix/Twinpeaks/Broken ...
- matlab 二元函数的画法
plot:画线(curve,二维空间以及三维空间) surf:画面(surface,一般在三维空间) 1. surf 绘图函数 surf 是 surface 的缩写,表示表面(显然至少三维图像才会有表 ...
- 分析MySQL各项指标
MySQL各项指标(因为这不是大多数搜索引擎的区别故意) INDEX(总指数):主要指标,不管是什么限制 ALTER TABLE `table_name` ADD INDEX index_name ( ...
- [ 转]Node.js模块 require和 exports
什么是模块? node.js通过实现CommonJS的Modules/1.0标准引入了模块(module)概念,模块是Node.js的基本组成部分.一个node.js文件就是一个模块,也就是说文件和模 ...
- WPF 过滤集合
<Window x:Class="ViewExam.MainWindow" xmlns="http://schemas.microsoft.com/w ...
- WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂
原文:WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂 先上效果图 正常样式 拖动时样式 好下面 开始吧 ==================================== ...
- Selenium 获取动态js的网页
Selenium基于webkit实现爬虫功能 http://www.cnblogs.com/luxiaojun/p/6144748.html https://www.cnblogs.com/chenq ...
- 微信小程序把玩(六)模块化
原文:微信小程序把玩(六)模块化 模块化也就是将一些通用的东西抽出来放到一个文件中,通过module.exports去暴露接口.我们在最初新建项目时就有个util.js文件就是被模块化处理时间的 /* ...
- 搭建svn服务器&服务器客户端使用笔记
参考借鉴文章:http://www.cnblogs.com/vijayfly/p/5711962.html 之前尝试着用git管理公司代码,但被一个问题困惑了很久无法解决,那就是git该如何只pull ...
- oracle利用透明网关访问mssql
遇到一个客户,有个需求,想将mssql中的数据抽取到oracle中.经过上网查找,感觉gateway这个工具可以实现,因此就搭建实验环境进行测试.首先在oracle delivery上面下载对应的安装 ...