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. 1.切面(aspect):日志方法的类
    2.切点(pointcut):它也称为切点表达式,目的是描述符合条件的方法
    3.目标(target):被注入的类
    4.连接点(join point):就是目标对象中的被注入日志的方法
    5.通知(advice):也就是日志方法
    6.aop代理(aopProxy):spring aop的实现就是靠代理来做到的,默认利用jdk代理和cglib代理
    来实现
    7.织入(weaving):是个动词,表示把切面类的通知与目标对象连接点糅合在一起的过程就叫织入
    通知:有五种
  1. before:前置通知,在连接点方法之前执行,而且不能控制连接点是否执行
    after:后置通知也叫最终通知,意思就是连接点方法只要执行(不管是否有错误),它
    都会得到执行
    after-return:返回通知,连接点正常执行(不报错)时才会执行这个通知.
    throwing:异常通知:连接点方法抛出异常时才会得到执行.这个通知不能处理异常
    只能得到异常信息.异常通知如果想把目标方法抛出的异常传递给通知方法
    只需要在异常通知的throwing属性设置的值等于通知方法的参数名就可以.
    当异常通知方法没有异常类型作为参数时,潜台词就是目标方法抛出任何异常,通知都会得到执行
    当异常通知方法"有"异常类型作为参数是,潜台词是只有目标方法抛出的异常是参数指定类型
    的异常或是子类型时,此通知方法才会得到执行
    around通知:环绕通知,环绕通知是最强的通知类型,它可以完全取代上面的4
    也可以进行异常的捕获处理,也可以组织目标方法执行
    顺序:before最前,after最后。但是若有多个beforeafter那么按顺序配置的顺序,会出现before...afterbefore...after
  2.  
  3. 通知方法参数中,第一个参数可以为JoinPoint类,直接使用这个对象,joinPont.class.getType()是目标类型。但是around方法中传的是这个JoinPoint的实现类ProceedingJoinPoint
  1. <context:property-placeholder local-override="true" location="classpath:db.properties"></context:property-placeholder>
  2. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  3. <property name="username" value="${jdbc.username}"/>
  4.  
  5. <property name="password" value="${jdbc.password}"/>
  6. <property name="url" value="${jdbc.url}"/>
  7. </bean>
  8. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  9. <property name="dataSource" ref="dataSource"></property>
  10. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  11. <property name="mapperLocations" value="classpath*:*Mapper.xml"></property>
  12. <!--<property name="configuration" -->
  13. </bean>
  14.  
  15. <mybatis:scan base-package="com.dao"></mybatis:scan>
  16. <bean id="deptService" class="com.service.impl.DeptServiceImpl" autowire="byType"></bean>
  17.  
  18. <bean id="logTest" class="com.log.LogTest"></bean>
  19. <aop:config>
  20. <aop:aspect id="logTest" ref="logTest">
  21. <aop:pointcut id="myPointcut" expression="execution(* com.service.impl.*.*(..))"/>
  22. <aop:before method="say" pointcut-ref="myPointcut"></aop:before>
  23. <!--<aop:after method="see" pointcut-ref="myPointcut"/>-->
  24. <aop:after-returning method="say" pointcut-ref="myPointcut"/>
  25. <!--<aop:after-throwing method="see" pointcut-ref="myPointcut"/>-->
  26. <!--<aop:around method="see" pointcut-ref="myPointcut"/>-->
  27. <aop:before method="say" pointcut-ref="myPointcut"></aop:before>
  28. </aop:aspect>
  29. </aop:config>

  关于sqlSessionFactory的<bean>还有另外一种配置,那就是不用mybaits的mybatis-config.xml文件,直接将该文件中的配置写在这个<bean>中

  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource"/>
  3. <!--指定mapper文件-->
  4. <property name="mapperLocations" value="classpath*:com/dao/**/*Mapper.xml"/>
  5. <!--mybatis-config文件解析之后mybatis是用Configuration类型来代表(入口对象)-->
  6. <property name="configuration">
  7. <bean class="org.apache.ibatis.session.Configuration">
  8. <!--配置显示sql的日志-->
  9. <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
  10. </bean>
  11. </property>
  12. <property name="plugins">
  13. <list>
  14. <bean class="com.github.pagehelper.PageInterceptor">
  15. <property name="properties">
  16. <!-- <props>
  17. <prop key="supportMethodsArguments">true</prop>
  18. <prop key="reasonable">true</prop>
  19. </props>
  20. -->
  21. <value>
  22. supportMethodsArguments=true
  23. resonable=true
  24. </value>
  25. </property>
  26. </bean>
  27. </list>
  28. </property>
  29. </bean>

  最后还有一个关键,如果这是这么配置完applicationContext.xml之后会发现一件事,那就是怎么用到它?

  在java代码中可以通过spring-web依赖中有一个工具类WebApplicationContextUtils.getWebApplicationContext(...)方法获取该xml文件,但是如果没有额外配置从哪里找这个xml文件,默认是在WEB-INF目录下找,配置的代码为<context-param>,其他的还是使用这个依赖完成过滤器的功能

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath*:applicationContext.xml</param-value>
  4. </context-param>
  5.  
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>
  9.  
  10. <filter>
  11. <filter-name>filter</filter-name>
  12. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13.  
  14. <init-param>
  15. <param-name>forceEncoding</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18.  
  19. <init-param>
  20. <param-name>encoding</param-name>
  21. <param-value>UTF-8</param-value>
  22. </init-param>
  23. </filter>
  24.  
  25. <filter-mapping>
  26. <filter-name>filter</filter-name>
  27. <url-pattern>/*</url-pattern>
  28. </filter-mapping>

  关于applicationContext.xml文件可以使用<import resource="classpath*:xxx.xml">来导入其他配置文件,作用类似于复制黏贴,最终效果相当于只有一个文件。

  1.  

学习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. Android之Context和Activity互相转换

    1.context转换为activity Activity activity = (Activity) context; 2.从activity得到context 在activity的方法中用cont ...

  2. Java中Compareable和Comparator两种比较器的区别

    Java中Compareable和Comparator两种比较器的区别 参考原文链接:https://www.cnblogs.com/ldy-blogs/p/8488138.html 1.引言 在ja ...

  3. 字符串替换 (replace)

    将文本文件中指定的字符串替换成新字符串. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束.end后面有两个字符串,要求用第二个字符串替 ...

  4. c++中比较好用的“黑科技”

    切入正题,上黑科技 一.黑科技函数(常用的我就不写了,例如sort函数) 1.next_permutation(a+1,a+1+n) a[1-n]全排列 2.reverse(a+1,a+1+n) 将a ...

  5. [7b2美化]柒比贰 魔改系列|7B2-分类封面添加波浪效果&每日诗词

    本文转载自:钻芒博客 https://www.zmki.cn/5105.html 效果如图: 代码: 首先在style.css样式表里添加波浪样式 /*浪来了*/ .lang { overflow: ...

  6. 【QSBOJ】字符串编辑

    题目链接:https://bbs.csdn.net/topics/390289884?page=1 AC代码: #include<bits/stdc++.h> using namespac ...

  7. 08 SSM整合案例(企业权限管理系统):07.订单操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 07.订单操作 SSM订单操作 ...

  8. POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 35642   Accep ...

  9. 解决使用还原卡的PC在2个月后要重新加入域的问题

    客户端正确操作: 1. 启动注册表编辑器. 要这样做, 请依次单击 开始 . 运行 , 类型 regedit 在 打开, 框, 然后单击 确定 . 2. 找到并单击以下注册表子项: HKEY_LOCA ...

  10. springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)

    总想对微服务架构做一个小小的总结,不知如何下手,最近觉得还是从搭建微服务的过程来入手,对于springboot的maven项目从构建多模块架构进而衍化为常用的微服务架构来做个记录吧. 首先,创建多个s ...