学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop
mybatis+spring的整合:
导入的依赖:1.数据库连接:mysql-connector-java 2.连接池:druid 3.servlet:javax.servlet-api 4.jstl:jstl(groupId:javax.servlet) 5.spring:spring-context 6.mybatis:mybatis
7.mybatis和spring整合:mybatis-spring(使用这个最好再加上spring-jdbc,这个内部有使用) 8.一些工具类的使用:spring-web 9.aop:aspectjweaver.
applicationContext.xml的配置:
1.<context:property-placeholder>有两两个属性:location配置文件名,例如db.properties可以引用其中的键,但是最好再加上local-override="true"防止引用键的名字重复,这个可能会读取到电脑上同名的键值.
2.DruidDataSource的<bean>,property的value就可以使用${xx}引用上方db.properties的键值
3.扫描dao,<mybatis:scan base-package="com.dao">
4.service的实现类的<bean>
5.配置aop:主要用来给某些方法注入日志,注入的类是指有日志方法的类;某些方法的类是目标,是被注入的
术语:
1.切面(aspect):日志方法的类
2.切点(pointcut):它也称为切点表达式,目的是描述符合条件的方法
3.目标(target):被注入的类
4.连接点(join point):就是目标对象中的被注入日志的方法
5.通知(advice):也就是日志方法
6.aop代理(aopProxy):spring aop的实现就是靠代理来做到的,默认利用jdk代理和cglib代理
来实现
7.织入(weaving):是个动词,表示把切面类的通知与目标对象连接点糅合在一起的过程就叫织入
通知:有五种
before:前置通知,在连接点方法之前执行,而且不能控制连接点是否执行
after:后置通知也叫最终通知,意思就是连接点方法只要执行(不管是否有错误),它
都会得到执行
after-return:返回通知,连接点正常执行(不报错)时才会执行这个通知.
throwing:异常通知:连接点方法抛出异常时才会得到执行.这个通知不能处理异常
只能得到异常信息.异常通知如果想把目标方法抛出的异常传递给通知方法
只需要在异常通知的throwing属性设置的值等于通知方法的参数名就可以.
当异常通知方法没有异常类型作为参数时,潜台词就是目标方法抛出任何异常,通知都会得到执行
当异常通知方法"有"异常类型作为参数是,潜台词是只有目标方法抛出的异常是参数指定类型
的异常或是子类型时,此通知方法才会得到执行
around通知:环绕通知,环绕通知是最强的通知类型,它可以完全取代上面的4种
也可以进行异常的捕获处理,也可以组织目标方法执行
顺序:before最前,after最后。但是若有多个before和after那么按顺序配置的顺序,会出现before...after后before...after 通知方法参数中,第一个参数可以为JoinPoint类,直接使用这个对象,joinPont.class.getType()是目标类型。但是around方法中传的是这个JoinPoint的实现类ProceedingJoinPoint
<context:property-placeholder local-override="true" location="classpath:db.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath*:*Mapper.xml"></property>
<!--<property name="configuration" -->
</bean> <mybatis:scan base-package="com.dao"></mybatis:scan>
<bean id="deptService" class="com.service.impl.DeptServiceImpl" autowire="byType"></bean> <bean id="logTest" class="com.log.LogTest"></bean>
<aop:config>
<aop:aspect id="logTest" ref="logTest">
<aop:pointcut id="myPointcut" expression="execution(* com.service.impl.*.*(..))"/>
<aop:before method="say" pointcut-ref="myPointcut"></aop:before>
<!--<aop:after method="see" pointcut-ref="myPointcut"/>-->
<aop:after-returning method="say" pointcut-ref="myPointcut"/>
<!--<aop:after-throwing method="see" pointcut-ref="myPointcut"/>-->
<!--<aop:around method="see" pointcut-ref="myPointcut"/>-->
<aop:before method="say" pointcut-ref="myPointcut"></aop:before>
</aop:aspect>
</aop:config>
关于sqlSessionFactory的<bean>还有另外一种配置,那就是不用mybaits的mybatis-config.xml文件,直接将该文件中的配置写在这个<bean>中
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--指定mapper文件-->
<property name="mapperLocations" value="classpath*:com/dao/**/*Mapper.xml"/>
<!--mybatis-config文件解析之后mybatis是用Configuration类型来代表(入口对象)-->
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!--配置显示sql的日志-->
<property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
</bean>
</property>
<property name="plugins">
<list>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!-- <props>
<prop key="supportMethodsArguments">true</prop>
<prop key="reasonable">true</prop>
</props>
-->
<value>
supportMethodsArguments=true
resonable=true
</value>
</property>
</bean>
</list>
</property>
</bean>
最后还有一个关键,如果这是这么配置完applicationContext.xml之后会发现一件事,那就是怎么用到它?
在java代码中可以通过spring-web依赖中有一个工具类WebApplicationContextUtils.getWebApplicationContext(...)方法获取该xml文件,但是如果没有额外配置从哪里找这个xml文件,默认是在WEB-INF目录下找,配置的代码为<context-param>,其他的还是使用这个依赖完成过滤器的功能
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <filter>
<filter-name>filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param> <init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
关于applicationContext.xml文件可以使用<import resource="classpath*:xxx.xml">来导入其他配置文件,作用类似于复制黏贴,最终效果相当于只有一个文件。
学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop的更多相关文章
- Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
- Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(三)——H2,MyBatis集成
1.配置h2,连接池,MyBatis Maven依赖: <!-- spring与数据库访问集成(非Hibernate) --> <dependency> <groupId ...
- mybatis整合spring,使用org.mybatis.spring.mapper.MapperScannerConfigurer扫描出现问题
<!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" ...
- Spring(五):Spring&Struts2&Hibernate整合后,实现查询Employee信息
背景: 基于之前两篇文章<Spring(三):Spring整合Hibernate>.<Spring(四):Spring整合Hibernate,之后整合Struts2>,了解了如 ...
- Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结
一.Thymeleaf概述 一般来说,常用的模板引擎有JSP.Velocity.Freemarker.Thymeleaf . SpringBoot推荐的 Thymeleaf – 语法更简单,功能更强大 ...
- Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(四)——Activiti集成
1.添加Activiti Maven依赖: <!-- ==============================activiti=========================== --&g ...
- Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(二)——Struts2集成
1. pom.xml文件添struts2依赖jar包: <!-- 与Struts2集成必须使用 --> <dependency> <groupId>org.spri ...
- Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(一)——Maven,Tomcat,Spring集成
1. 创建Maven Web工程 (1) 磁盘上创建Maven工程所需要的文件夹结构如下: (2) 在与src同级目录中创建pom.xml文件: <project xm ...
- Spring第五弹—–配置Spring管理的bean的作用域和生命周期
singleton (默认方式) 在每个Spring IoC容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init=“true” ...
随机推荐
- primecoin在ubuntu16.04上部署服务:
primecoin在ubuntu16.04上部署服务: 一.下载Tomcat,Jdk,primecoin(公司内部文件) 注意Tomcat版本需要高于Jdk的,不然会报错. 二.把它们都解压到你要的安 ...
- v-show和element中表单验证validate起到的化学反应
说起v-show和v-if,进行前端开发的大家一定不会陌生,他们都是用来控制标签元素的显示与隐藏的,他们的区别就是v-show会把标签渲染出来,只是会隐藏起来,相当于visibility:hidden ...
- R语言 subset()函数用法
subset() 函数: subset(dataset , subset , select ) dataset 是 要进行操作的数据集 subset 是对数据的某些字段进行操作 select 选取要显 ...
- Python对城市距离自动化爬取【必学小型项目】
本地创建数据库,将 excel 数据存储到 city 表中,再取 | 湖北省 | 的所有地级市和县.县级市.区数据作为样表数据记录在样表中.利用 python 的 xlrd 包,定义 proc ...
- GSON使用笔记(3) -- 如何反序列化出List
GSON使用笔记(3) -- 如何反序列化出List 时间 2014-06-26 17:57:06 CSDN博客原文 http://blog.csdn.net/zxhoo/article/deta ...
- JSONObject.fromObject() 转string时,实体里面的时间错乱的问题
在把要入库的数据实体转成JSON字符串发给kafka的时候,出现了问题,转换完以后,就变成这样子的了,真的是第一次见到这种,真的是长见识了 然后百度了一下:https://www.cnblogs.co ...
- P1091 N-自守数
1091 N-自守数 (15分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3,而 2 的末尾两位正好是 9,所以 9 是一个 3-自守 ...
- win7 & win10 安装AD管理工具
总所周知,AD域的作用对于一个公司有着无比重要的作用,但是在Win7/10系统下该如何去管理AD域呢. 对于AD域的服务器搭建,在这里我们不进行说明,感兴趣的同学可以去Google相关的资料,现在主要 ...
- iOS 安全地在主线程执行一个Block
//主线程同步队列 #define dispatch_main_sync_safe(block)\ if ([NSThread isMainThread]) {\ block();\ } else { ...
- [Java] Eclipse 设置相同变量背景色高亮显示
在Eclipse中,鼠标选中或者光标移动到java类的变量名时,相同变量会被标识显示(设置背景色高亮), 并且侧边滚动条会标出变量的位置, 查找变量十分方便. 1.相同变量标识高亮显示:Window ...