spring整合mybatis(代理的方式)【我】
创建项目等同上一篇非代理方式随笔,只说不一样的部分:
项目结构主要是多了下面红框部分:
配置文件:
主要是dao配置文件中多了Mapper代理java类的扫描包路径:
applicationContext-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties" /> <!-- 引入其他xml -->
<import resource="classpath:spring/applicationContext-service.xml"/> <!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean> <!-- 配置sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property> <!-- sql映射文件路径(是否用代理方式都可以在这配置xml文件路径) -->
<!-- <property name="mapperLocations" value="classpath*:com/zhangguo/bookstore/mapper/*Mapper.xml"></property> -->
<property name="mapperLocations" value="classpath*:mybatis/mapper/*.xml"></property>
</bean> <!-- 配置扫描包,加载mapper代理对象(使用代理方式才需要配置这个xxMapper.java的扫描包) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- <property name="basePackage" value="com.taotao.mapper"></property> -->
<property name="basePackage" value="mybatis.mapper"></property>
</bean>
</beans>
其实就是多了下面部分:
<!-- 配置扫描包,加载mapper代理对象(使用代理方式才需要配置这个xxMapper.java的扫描包) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mybatis.mapper"></property>
</bean>
在这里其实就是对 BookDAO.java 类的扫描包配置,指定其为对应的代理类。
然后是java代码:
首先,这个就需要针对每个代理类配置一个 xml文件:BookMapper.xml
<?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="mybatis.mapper.BookDAO">
<!--id应该是接口中的方法,结果类型如没有配置别名则应该使用全名称 -->
<!--获得所有图书 -->
<select id="getAllBooks" resultType="mybatis.pojo.Book">
select id,title,price,publishDate from books
</select>
<!--获得图书对象通过编号 -->
<select id="getBookById" resultType="mybatis.pojo.Book">
select id,title,price,publishDate from books where id=#{id}
</select>
<!-- 增加 -->
<insert id="add">
insert into books(title,price,publishDate)
values(#{title},#{price},#{publishDate})
</insert>
<!-- 删除 -->
<delete id="delete">
delete from books where id=#{id}
</delete>
<!-- 更新 -->
<update id="update">
update books set title=#{title},price=#{price},publishDate=#{publishDate}
where id=#{id}
</update>
</mapper>
注意,开头部分的 命名空间要指定其对应的代理类,这里 是 namespace="mybatis.mapper.BookDAO"
然后是其代理类的java代码:BookDAO.java
package mybatis.mapper;
import java.util.List; import org.apache.ibatis.annotations.Param; import mybatis.pojo.Book; /**
* 图书数据访问接口
*/
public interface BookDAO {
/**
* 获得所有图书
*/
public List<Book> getAllBooks();
/**
* 根据图书编号获得图书对象
*/
public Book getBookById(@Param("id") int id);
/**
* 添加图书
*/
public int add(Book entity);
/**
* 根据图书编号删除图书
*/
public int delete(int id);
/**
* 更新图书
*/
public int update(Book entity);
}
最后是测试类:Test
package test; import java.util.List; import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import mybatis.pojo.Book;
import mybatis.service.BookService;
import mybatis.service.T1Service; public class T1 { static BookService bookservice; @BeforeClass
public static void before(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
bookservice=ctx.getBean(BookService.class);
} @Test
public void testGetAllBooks() {
List<Book> books=bookservice.getAllBooks();
System.out.println(111);
System.out.println(books);
}
//只需看上面部分,当然也可以将其改写在main方法中
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml"); //非代理的方式
T1Service t1Service =ctx.getBean(T1Service.class);
t1Service.getBookById();
}
}
spring整合mybatis(代理的方式)【我】的更多相关文章
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- SPring整合Mybatis方式一
Spring整合Mybatis 需要maven包: mysql-connector-java 5.1.47, mybatis 3.5.2, spring-webmvc 5.2.2.RELEASE, s ...
- Mybatis学习(六)————— Spring整合mybatis
一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...
- Mybatis(六) Spring整合mybatis
心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...
- 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题
https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...
- Spring整合Mybatis案例,献给初学的朋友
今天我们来学习Spring整合Mybatis. 开发环境:Ide:MyEclipse 2017 CI JDK:1.8 首先我们简单的认识下这两个框架 1.Mybatis MyBatis是一个支持普通S ...
- spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现
知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...
- 深入源码理解Spring整合MyBatis原理
写在前面 聊一聊MyBatis的核心概念.Spring相关的核心内容,主要结合源码理解Spring是如何整合MyBatis的.(结合右侧目录了解吧) MyBatis相关核心概念粗略回顾 SqlSess ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
随机推荐
- weakref:对象的弱引用
介绍 weakref支持对象的弱引用,正常的引用会增加对象的引用计数,并避免它被垃圾回收.但结果并不是总和期望的那样,比如有时候可能会出现一个循环引用,或者有时候需要内存时可能要删除对象的缓存.而弱引 ...
- XML基础综合案例【三】
实现简单的学生管理系统 使用xml当做数据,存储学生信息 ** 创建一个xml文件,写一些学生信息 ** 增加操作1.创建解析器2.得到document 3.获取到根节点4.在根节点上面创建stu标签 ...
- Hive的视图和索引(九)
Hive的视图和索引 1.Hive Lateral View 1.基本介绍 Lateral View用于和UDTF函数(explode.split)结合来使用. 首先通过UDTF函数拆分成多行 ...
- java-集合学习-底层实现
集合分为两大类: Collection集合: 单个存储 Map集合: 按<键,值>对的形式存储, <员工姓名,工资> Collection类关系图 Collection常见方 ...
- “美登杯”上海市高校大学生程序设计邀请赛 **D. 小花梨的取石子游戏**
"美登杯"上海市高校大学生程序设计邀请赛 (华东理工大学) D. 小花梨的取石子游戏 Description 小花梨有
- 数据驱动——ddt
1: pip3 install ddt 2: @ddt 装饰 @data((2,3),(4,5)) 支持列表,元祖,字典 @unpack 解压数据 1 import unittest 2 from ...
- KMP算法详解&&P3375 【模板】KMP字符串匹配题解
KMP算法详解: KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt(雾)提出的. 对于字符串匹配问题(such as 问你在abababb中有多少个 ...
- C#工具:ASP.net 调用MySQL 帮助类(包括存储过程调用)
1.创建DbHelperMySQL类 2.复制代码到类中 using System; using System.Collections; using System.Collections.Specia ...
- .NET Core 3来了!如何使用DevExpress WPF创建.NET Core 3应用
DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...
- 小程序swiper组件的bindchange方法重复执行问题
这是官方文档的说法给出了swiper组件一直来回滑动的bug原因 以下是修正方法 <swiper autoplay="{{autoplay}}" interval=" ...