Java学习笔记-spring整合mybatis
2024-09-01 00:18:54
这个项目就是一个例子,只有添加图书的功能:
项目架构:
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);
}
}
最新文章
- [转]android访问网络:java.net.ConnectException: localhost/127.0.0.1:8888 - Connection refused
- Oracle执行语句跟踪(2)——使用10046事件实现语句追踪
- Linux 按行分割文件(转载)
- 有关java中static关键的重写问题
- Java RMI远程方法调用
- Realm for Android快速入门教程
- centos和Ubuntu区别
- gif动图快速制作方法(附工具)(转)
- LoadRunner性能测试指标分析
- Java开发笔记(五)数值变量的类型
- opencv学习之路(36)、运动物体检测(一)
- 解题报告 『机器翻译(vector)』
- DevExpress中GridControl的重新绑定数据后如何刷新 (转)
- 返回 字符串的 form和js组合让页面跳转
- PHP测试Mysql数据库连接
- 简单选择排序(Simple Selection Sort)
- 解题:THUWC 2017 在美妙的数学王国中畅游
- 20145324王嘉澜 《网络对抗》进阶实践之 shellcode注入和Return-to-libc攻击深入
- HDU 5887 Herbs Gathering(搜索求01背包)
- 学习mybatis-3 step by step 篇三
热门文章
- Leetcode 237 Delete Node in a Linked List 链表
- Android到您的计算机使用命令行屏幕捕获和出口
- python3实现万年历(包括公历、农历、节气、节日)
- POCO文档翻译:POCO C++库入门指南
- crossplatform----文本编辑器工具Atom安装
- IP地址的正则表达式
- 用游戏杆控制WPF中三维模型
- win10+vs2008编译比特币1.0版源码总结
- centos 6 yum源记录,离线下载rpm包的办法
- 没必要看源码。。把文档学通就已经牛逼了(我们大多还是在应用层,还达不到研究的程度。附class与examples大全链接)