这个项目就是一个例子,只有添加图书的功能:

项目架构:

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的更多相关文章

  1. mybatis学习笔记 spring与mybatis整合

    转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...

  2. (摘录笔记)JAVA学习笔记SSH整合搭建项目

    1:当然是导jar包啦: struts2: spring: hibernate: 至于这些jar包是什么作用,我想就不必我解释了,大家都懂得,ssh2基本的jar包: 还有一些其他jar包:strut ...

  3. Spring Boot学习笔记(五)整合mybatis

    pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...

  4. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  5. Java开发学习(十四)----Spring整合Mybatis及Junit

    一.Spring整合Mybatis思路分析 1.1 环境准备 步骤1:准备数据库表 Mybatis是来操作数据库表,所以先创建一个数据库及表 create database spring_db cha ...

  6. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  7. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

  8. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  9. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

随机推荐

  1. chaos —— 混沌

    混沌,一个被严重滥用的物理数学概念. 混沌(chaos)是一个动力学系统(Dynamic System)概念,指的是确定性动力学系统因对初值敏感而表现出的不可预测的.类似随机性的运动. 1. 洛伦兹吸 ...

  2. python 教程 第二十一章、 扩展Python

    第二十一章. 扩展Python /* D:\Python27\Lib\Extest-1.0\Extest2.c */ #include <stdio.h> #include <std ...

  3. Linux栈搜索算法优化随想

    Linux网络协议栈可以准确但仍进行说明,不用说,Netfilter.简单地说,TC够了,但有几个硬伤,本文不构成一个完整的记录,如果是随笔,不可当真. 0.发现物种 Linux堆栈作为一个纯软件实现 ...

  4. handler looper和messageQueue

    一.用法. Looper为了应付新闻周期,在创建过程中初始化MessageQueue. Handler在一个消息到当前线程的其他线程 MessageQueue用于存储所述消息 Looper其中线程创建 ...

  5. ThreadingTest(线程测试)领先的白框进入这个行业

    测试一直是黑色的,白点.在一般情况下,因为白盒测试需要逻辑思维能力是比较高的技术要求比一般开发商的项目经验和谨慎甚至更高,和较长的测试时间,用于单元测试,昂贵的工具,因此,国内企业普遍忽视白盒测试.这 ...

  6. Java之nio MappedByteBuffer的资源释放问题

    使用nio的MappedByteBuffer映射内存, 在最后执行File.delete()方法的时候, 返回false,  即文件没有被删除. 原因是MappedByteBuffer在内存中也会创建 ...

  7. WPF学习目录

    基本概念 数据源Source-目标Target WPF生命周期 App.xaml 依赖属性 WPF路由 线程操纵UI问题 利用属性中设置.查看DataContext/Command等 分析布局 写数据 ...

  8. 怎样开始GO编程?

    如果你想开始学习GO语法前,请先背熟下述4点: 1. 环境变量: 使用go env查看环境变量 GOARCH/GOHOSTARCH: 体系架构, amd64或386 GOOS/GOHOSTOS: 操作 ...

  9. 【转载】MySQL Replication 环境安装与配置

    安装[root@msr01 ~]# yum install mysql-serverInstalled:mysql-server.x86_64 0:5.1.73-3.el6_5 Dependency ...

  10. WPF中的文字修饰——上划线,中划线,基线与下划线

    原文:WPF中的文字修饰——上划线,中划线,基线与下划线 我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线 ...