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的更多相关文章

  1. Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

  2. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(三)——H2,MyBatis集成

    1.配置h2,连接池,MyBatis Maven依赖: <!-- spring与数据库访问集成(非Hibernate) --> <dependency> <groupId ...

  3. mybatis整合spring,使用org.mybatis.spring.mapper.MapperScannerConfigurer扫描出现问题

    <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" ...

  4. Spring(五):Spring&Struts2&Hibernate整合后,实现查询Employee信息

    背景: 基于之前两篇文章<Spring(三):Spring整合Hibernate>.<Spring(四):Spring整合Hibernate,之后整合Struts2>,了解了如 ...

  5. Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结

    一.Thymeleaf概述 一般来说,常用的模板引擎有JSP.Velocity.Freemarker.Thymeleaf . SpringBoot推荐的 Thymeleaf – 语法更简单,功能更强大 ...

  6. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(四)——Activiti集成

    1.添加Activiti Maven依赖: <!-- ==============================activiti=========================== --&g ...

  7. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(二)——Struts2集成

    1. pom.xml文件添struts2依赖jar包: <!-- 与Struts2集成必须使用 --> <dependency> <groupId>org.spri ...

  8. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(一)——Maven,Tomcat,Spring集成

    1.  创建Maven Web工程 (1)       磁盘上创建Maven工程所需要的文件夹结构如下: (2)       在与src同级目录中创建pom.xml文件: <project xm ...

  9. Spring第五弹—–配置Spring管理的bean的作用域和生命周期

    singleton (默认方式) 在每个Spring IoC容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init=“true” ...

随机推荐

  1. Intend之Date的几个功能

    封装为一个方法 1.跳转到拨号页面 //跳转到拨号页面的方法 protected void takeCall(String info){ Intent intent=new Intent(); int ...

  2. LeetCode 1025. Divisor Game

    题目链接:https://leetcode.com/problems/divisor-game/ 题意:Alice和Bob玩一个游戏,Alice先开始.最初,黑板上有一个数字N.每一轮,选手首先需要选 ...

  3. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

  4. 伪奢侈品iPhone大降价,肉搏国产手机胜算几何?

    据国外媒体报道,苹果在中国降低iPhone价格的策略已收到明显的效果,自从1月11日正式调整价格以来,iPhone在苏宁电器平台上的销量飙升83%,而天猫上的销量也增长了76%,其中最受欢迎的机型是i ...

  5. AOP五种执行时机

    动态代理四种增强方式 先创建一个service类 package com.zzj.calculatar.service; import org.springframework.stereotype.S ...

  6. 简单模拟IOC容器:为添加了@Autowired的属性赋值(初始值)

    创建@Autowired注解 package com.zzj.test; import java.lang.annotation.ElementType; import java.lang.annot ...

  7. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  8. Ubuntu系统配置Zabbix前端及中文乱码解决方案

    Ubuntu系统配置Zabbix前端及中文乱码解决方案  作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装zabbix 博主推荐阅读: https://www.cnblogs ...

  9. Python测试进阶——(6)Bash脚本启动Python监控程序并传递PID

    用HiBench执行Hadoop——Sort测试用例,进入 /HiBench-master/bin/workloads/micro/sort/hadoop 目录下,执行命令: [root@node1 ...

  10. '/'和‘/*’差异造成的No mapping found for HTTP request with URI [/springMVC/welcome.jsp] in DispatcherServlet with name 'springmvc'

    在采用springMVC框架的时候所遇到的一个小问题,其中web.xml中关于servlet的配置如下: <servlet> <servlet-name>springmvc&l ...