学习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">来导入其他配置文件,作用类似于复制黏贴,最终效果相当于只有一个文件。



原文地址:https://www.cnblogs.com/woyujiezhen/p/11734917.html