JAVA框架 Spring 和Mybatis整合(动态代理)
一、使用传统方式的dao的书写方式,不建议。目前采用的是动态代理的方式交给mybatis进行处理。
首先回顾下动态代理要求:
1)子配置文件的中,namespace需要是接口的全路径,id是接口的方法名称 这两项唯一确定我们的调用的接口。
2)子mapper文件的名称要和接口的名称保持一致。
3)参数和返回值要和方法的保持一致。
二、整合
1)dao代码:
dao的代码,只需要保留接口即可。
2)service的代码:
实现类发生变化。
动态代理dao的id的名字是: 类名首字母小写
package jd.com.service; import jd.com.dao.trDao;
import jd.com.dao.user;
import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service(value = "serv")
public class serFindByIdImpl implements trService { @Resource(name="trDao")
private trDao trDao; @Override
public user serFindById(Integer id) {
user us=this.trDao.findUserByName();
return us;
}
}
测试类:
package jd.com.testDemo; import jd.com.dao.user;
import jd.com.service.serFindByIdImpl; import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.annotation.Resource; public class testDemo { @Test
public void test1(){
ApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext.xml");
serFindByIdImpl serv= (serFindByIdImpl) ap.getBean("serv");
user us=serv.serFindById();
System.out.println(us); }
}
三 、配置文件
applicationContext.xml 加入mapper的包的动态扫描
<!--开启mapper包扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="jd.com.dao" />
</bean>
需要使用类:MapperScannerConfigurer在spring和mybaits整合的包里。
需要配置属性basepack 需要扫描mapper.xml配置文件的包的目录,如果有多个mapper文件的话,可以在value出以逗号隔开写多个包。
配置了上面配置 不需要在SqlMapConfig.xml写入引入的文件。否则会加载2次。
<?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">
<!--加载配置文件,定义的properites文件
引入外部文件使用classpath关键字。
--> <context:component-scan base-package="jd.com" />
<aop:aspectj-autoproxy /> <context:property-placeholder location="classpath:/db.properties" />
<!--定义数据库连接池-->
<bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
<!--支持el表达式-->
<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="maxTotal" value="" />
<property name="maxIdle" value="" />
</bean>
<!--配置mapper
其中:org.mybatis.spring.SqlSessionFactoryBean 是SqlSessionFactory的实现类,该类在mybatis-spring.1.2..jar里。
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--依赖DataSource-->
<property name="dataSource" ref="basicDataSource"/>
<!--加载mybaits的配置文件-->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>
<!--开启mapper包扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="jd.com.dao" />
</bean> </beans>
子配置文件:需要namespace、id、parameterType、resultType要和接口的保持一致。
<?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="jd.com.dao.trDao">
<select id="findUserByName" parameterType="int" resultType="jd.com.dao.user" >
SELECT * FROM username WHERE id=#{id};
</select>
</mapper>
四:探讨
1、这个时候SqlMapConfig 文件是空的。可以去掉 在applicationContext.xml文件中添加mapper文件改为实际的子mapper配置文件路径 这个是错误的,因为如果有多个mapper文件咋办????
2、mybaits的使用的动态代理 被代理的接口的,的动态代理对象的id是接口的名称的首字母小写。
JAVA框架 Spring 和Mybatis整合(动态代理)的更多相关文章
- JAVA框架 Spring 和Mybatis整合(传统dao)
一:我们使用spring处理service,mybaits处理dao层. 二:导入jar包 pom.xml文件内容: <?xml version="1.0" encoding ...
- 我的第九个java程序--spring和mybatis整合(java project)
思路:入口程序读spring的配置文件-配置文件注入给程序bean--程序拿到bean以操作对象的手法查出程序 入口程序HelloWorld.java package HelloWorld; impo ...
- Mybatis学习--spring和Mybatis整合
简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...
- 九 spring和mybatis整合
1 spring和mybatis整合 1.1 整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...
- Mybatis学习(7)spring和mybatis整合
整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- Java基础-SSM之Spring和Mybatis整合案例
Java基础-SSM之Spring和Mybatis整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在之前我分享过mybatis和Spring的配置案例,想必大家对它们的 ...
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
随机推荐
- django admin登陆添加修改内容
model文件中 __all__ = ["Book", "Publisher", "Author"] from django.db impo ...
- Java 并发:Future FutureTask
Future 当向一个ExecutorService提交任务后可以获得一个Future对象,在该对象上可以调用get,cancel等命令来获取任务运行值或者是取消任务.下面是一个简单的计数任务: pu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- 自定义多选框(checkbox)和单选框(radio)css样式
直接上代码: input[type="radio"],input[type="checkbox"]{ -webkit-appearance: none; out ...
- 学习MVC之租房网站(十二)-缓存和静态页面
在上一篇<学习MVC之租房网站(十一)-定时任务和云存储>学习了Quartz的使用.发邮件,并将通过UEditor上传的图片保存到云存储.在项目的最后,再学习优化网站性能的一些技术:缓存和 ...
- 与HttpSession相关的监听器
概述 与HttpSession相关的监听器有四个:分别是HttpSessionListener.HttpSessionAttributeListener.HttpSessionBindingListe ...
- url override and HttpSession implements session for form
url 重写结合HttpSession实现会话管理之 form 提交 package com.test; import javax.servlet.ServletException; import j ...
- 用 React 整合 LogEntries JavaScript 库
[编者按]本文作者为 David Posin,主要介绍 React 与 LogEntries 间的相互操作.本文系国内 ITOM 管理平台 OneAPM 编译呈现. 众所周知,React.js已经被证 ...
- MySQL索引与Index Condition Pushdown(employees示例)
实验 先从一个简单的实验开始直观认识ICP的作用. 安装数据库 首先需要安装一个支持ICP的MariaDB或MySQL数据库.我使用的是MariaDB 5.5.34,如果是使用MySQL则需要5.6版 ...
- 转:ASP.NET前台代码绑定后台变量方法总结
经常会碰到在前台代码中要使用(或绑定)后台代码中变量值的问题.一般有<%= str%>和<%# str %>两种方式,这里简单总结一下.如有错误或异议之处,敬请各位指教. 一方 ...