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

项目架构:

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. DLL动态库的创建,隐式加载和显式加载

    动态库的创建 打开VS,创建如下控制台工程,工程命名为DllTest: 在弹出的对话框中选择"DLL"后单击"完成"按钮: 在工程中新建DllTest.h和Dl ...

  2. Rust 2017 调查报告:学习曲线是最大痛点(最大的问题是这门语言太偏底层了,现在做底层的少了。还有C这个绕不过去的存在)

    Rust 官方在社区上做了一次调查,以了解用户如何看待 Rust 的发展.调查共收到 5368 份回复,其中有 大约 2/3 的是 Rust 用户,剩下的 1/3 是非 Rust 用户,调查结果如下. ...

  3. DOS符号转义(转 http://www.robvanderwoude.com/escapechars.php)

    Escape Characters Character to be escaped Escape Sequence Remark % %% May not always be required in ...

  4. typescript Json Convert

    关键代码 this.data={}; // json string this.dataStr=JSON.stringify(this.data); // json object this.conver ...

  5. WPF特效-粒子动画

    原文:WPF特效-粒子动画 WPF实现泡泡龙小游戏效果.     /// -Ball to Ball Collision - Detection and Handling    /// http:// ...

  6. style原则问题

    就近原则 1.“行内”覆盖“嵌入”,“嵌入”覆盖“外部”Style.xml <Window.Resources> <Grid.Resources> ….中间层 <Butt ...

  7. QT Linux Demo程序编译

    我手上的qt源码包为:qt-everywhere-opensource-src-4.7.0.tar.gz 在Linux下编译比较容易,解压后直接 ./configure,一般会报缺少什么库这些.自己遇 ...

  8. .NET VS 自定义新建代码文件模板

    参考:http://www.cnblogs.com/fightingtong/p/3765914.html 在VS中新建文件时,可使用模板在文件中生成指定内容.只需要把IDE安装目录下的模板进行修改保 ...

  9. 【Git】整合分支那些事儿

    对于scm这个岗位来说,基线升级应该是这个岗位需要的必备技能了,现在来说说我司进行高通代码基线升级时选择的方式方法,供大家参考,也供自己学习积累. git这个工具大家都并不陌生,但是对于不经常提交代码 ...

  10. win10中使用sqlserver2008r2 SQL Server 配置管理器

    原文:win10中使用sqlserver2008r2 SQL Server 配置管理器 使用 Windows10 访问 SQL Server 配置管理器 因为 SQL Server 配置管理器是 Mi ...