学习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” ...
随机推荐
- Android之Context和Activity互相转换
1.context转换为activity Activity activity = (Activity) context; 2.从activity得到context 在activity的方法中用cont ...
- Java中Compareable和Comparator两种比较器的区别
Java中Compareable和Comparator两种比较器的区别 参考原文链接:https://www.cnblogs.com/ldy-blogs/p/8488138.html 1.引言 在ja ...
- 字符串替换 (replace)
将文本文件中指定的字符串替换成新字符串. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束.end后面有两个字符串,要求用第二个字符串替 ...
- c++中比较好用的“黑科技”
切入正题,上黑科技 一.黑科技函数(常用的我就不写了,例如sort函数) 1.next_permutation(a+1,a+1+n) a[1-n]全排列 2.reverse(a+1,a+1+n) 将a ...
- [7b2美化]柒比贰 魔改系列|7B2-分类封面添加波浪效果&每日诗词
本文转载自:钻芒博客 https://www.zmki.cn/5105.html 效果如图: 代码: 首先在style.css样式表里添加波浪样式 /*浪来了*/ .lang { overflow: ...
- 【QSBOJ】字符串编辑
题目链接:https://bbs.csdn.net/topics/390289884?page=1 AC代码: #include<bits/stdc++.h> using namespac ...
- 08 SSM整合案例(企业权限管理系统):07.订单操作
04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 07.订单操作 SSM订单操作 ...
- POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 35642 Accep ...
- 解决使用还原卡的PC在2个月后要重新加入域的问题
客户端正确操作: 1. 启动注册表编辑器. 要这样做, 请依次单击 开始 . 运行 , 类型 regedit 在 打开, 框, 然后单击 确定 . 2. 找到并单击以下注册表子项: HKEY_LOCA ...
- springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)
总想对微服务架构做一个小小的总结,不知如何下手,最近觉得还是从搭建微服务的过程来入手,对于springboot的maven项目从构建多模块架构进而衍化为常用的微服务架构来做个记录吧. 首先,创建多个s ...