• Mybatis与Spring集成、Aop整合pagehelper插件


    所有创建对象的动作都交给Spring来完成

    将上篇的分页放到通知里来完成

    目录

     一、SSM整合

    导入pom依赖

    添加spring相关依赖(5.0.2.RELEASE)

    添加mybatis相关依赖

    spring整合mybatis(1.3.1)

    添加dbcp2连接池

    添加日志配置(2.9.1)

    其他

    解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题

    2、Aop整合分页插件


     一、SSM整合

    导入pom依赖

    1. <properties>
    2. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    3. <maven.compiler.source>1.8maven.compiler.source>
    4. <maven.compiler.target>1.8maven.compiler.target>
    5. <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
    6. <spring.version>5.0.2.RELEASEspring.version>
    7. <mybatis.version>3.4.5mybatis.version>
    8. <mysql.version>5.1.44mysql.version>
    9. <pagehelper.version>5.1.2pagehelper.version>
    10. <mybatis.spring.version>1.3.1mybatis.spring.version>
    11. <commons.dbcp2.version>2.1.1commons.dbcp2.version>
    12. <commons.pool2.version>2.4.3commons.pool2.version>
    13. <log4j2.version>2.9.1log4j2.version>
    14. <junit.version>4.12junit.version>
    15. <servlet.version>4.0.0servlet.version>
    16. <lombok.version>1.18.2lombok.version>
    17. properties>

    添加spring相关依赖(5.0.2.RELEASE)

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-contextartifactId>
    4. <version>${spring.version}version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframeworkgroupId>
    8. <artifactId>spring-ormartifactId>
    9. <version>${spring.version}version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.springframeworkgroupId>
    13. <artifactId>spring-txartifactId>
    14. <version>${spring.version}version>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframeworkgroupId>
    18. <artifactId>spring-aspectsartifactId>
    19. <version>${spring.version}version>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframeworkgroupId>
    23. <artifactId>spring-webartifactId>
    24. <version>${spring.version}version>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframeworkgroupId>
    28. <artifactId>spring-testartifactId>
    29. <version>${spring.version}version>
    30. dependency>

    添加mybatis相关依赖

    1. <dependency>
    2. <groupId>org.mybatisgroupId>
    3. <artifactId>mybatisartifactId>
    4. <version>${mybatis.version}version>
    5. dependency>
    6. <dependency>
    7. <groupId>mysqlgroupId>
    8. <artifactId>mysql-connector-javaartifactId>
    9. <version>${mysql.version}version>
    10. dependency>
    11. <dependency>
    12. <groupId>com.github.pagehelpergroupId>
    13. <artifactId>pagehelperartifactId>
    14. <version>${pagehelper.version}version>
    15. dependency>

    spring整合mybatis(1.3.1)

    1. <dependency>
    2. <groupId>org.mybatisgroupId>
    3. <artifactId>mybatis-springartifactId>
    4. <version>${mybatis.spring.version}version>
    5. dependency>

    添加dbcp2连接池

    1. <dependency>
    2. <groupId>org.apache.commonsgroupId>
    3. <artifactId>commons-dbcp2artifactId>
    4. <version>${commons.dbcp2.version}version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.apache.commonsgroupId>
    8. <artifactId>commons-pool2artifactId>
    9. <version>${commons.pool2.version}version>
    10. dependency>

    添加日志配置(2.9.1)

    1. <dependency>
    2. <groupId>org.apache.logging.log4jgroupId>
    3. <artifactId>log4j-coreartifactId>
    4. <version>${log4j2.version}version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.apache.logging.log4jgroupId>
    8. <artifactId>log4j-apiartifactId>
    9. <version>${log4j2.version}version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.apache.logging.log4jgroupId>
    13. <artifactId>log4j-webartifactId>
    14. <version>${log4j2.version}version>
    15. dependency>

    其他

    1. <dependency>
    2. <groupId>junitgroupId>
    3. <artifactId>junitartifactId>
    4. <version>${junit.version}version>
    5. <scope>testscope>
    6. dependency>
    7. <dependency>
    8. <groupId>javax.servletgroupId>
    9. <artifactId>javax.servlet-apiartifactId>
    10. <version>${servlet.version}version>
    11. <scope>providedscope>
    12. dependency>
    13. <dependency>
    14. <groupId>org.projectlombokgroupId>
    15. <artifactId>lombokartifactId>
    16. <version>${lombok.version}version>
    17. <scope>providedscope>
    18. dependency>

    解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题

    1. <resources>
    2. <resource>
    3. <directory>src/main/javadirectory>
    4. <includes>
    5. <include>**/*.xmlinclude>
    6. includes>
    7. resource>
    8. <resource>
    9. <directory>src/main/resourcesdirectory>
    10. <includes>
    11. <include>jdbc.propertiesinclude>
    12. <include>*.xmlinclude>
    13. includes>
    14. resource>
    15. resources>
    1. <plugin>
    2. <groupId>org.apache.maven.pluginsgroupId>
    3. <artifactId>maven-compiler-pluginartifactId>
    4. <version>${maven.compiler.plugin.version}version>
    5. <configuration>
    6. <source>${maven.compiler.source}source>
    7. <target>${maven.compiler.target}target>
    8. <encoding>${project.build.sourceEncoding}encoding>
    9. configuration>
    10. plugin>
    11. <plugin>
    12. <groupId>org.mybatis.generatorgroupId>
    13. <artifactId>mybatis-generator-maven-pluginartifactId>
    14. <version>1.3.2version>
    15. <dependencies>
    16. <dependency>
    17. <groupId>mysqlgroupId>
    18. <artifactId>mysql-connector-javaartifactId>
    19. <version>${mysql.version}version>
    20. dependency>
    21. dependencies>
    22. <configuration>
    23. <overwrite>trueoverwrite>
    24. configuration>
    25. plugin>

     SSM整合

    1、添加所有的pom依赖

    2、添加并配置整合的配置文件

            Spring:spring-context.xml

            Mybatis:mybatis.cfg.xml

            spring-mybatis.xml

                    1、扫描注解驱动

                    2、添加并管理数据源->管理sqlsession

                    3、扫描所有的mapper接口

                    4、添加事务管理器

    添加applicationContext-mybatis.xml也就是  spring-mybatis.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    6. <context:annotation-config/>
    7. <context:component-scan base-package="com.zwc.ssm"/>
    8. <context:property-placeholder location="classpath:jdbc.properties"/>
    9. <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    10. destroy-method="close">
    11. <property name="driverClassName" value="${jdbc.driver}"/>
    12. <property name="url" value="${jdbc.url}"/>
    13. <property name="username" value="${jdbc.username}"/>
    14. <property name="password" value="${jdbc.password}"/>
    15. <property name="initialSize" value="10"/>
    16. <property name="maxTotal" value="100"/>
    17. <property name="maxIdle" value="50"/>
    18. <property name="minIdle" value="10"/>
    19. <property name="maxWaitMillis" value="-1"/>
    20. bean>
    21. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    22. <property name="dataSource" ref="dataSource"/>
    23. <property name="mapperLocations" value="classpath*:com/zwc/ssm/**/mapper/*.xml"/>
    24. <property name="typeAliasesPackage" value="com/javaxl/ssm/**/model"/>
    25. <property name="plugins">
    26. <array>
    27. <bean class="com.github.pagehelper.PageInterceptor">
    28. <property name="properties">
    29. <value>
    30. helperDialect=mysql
    31. value>
    32. property>
    33. bean>
    34. array>
    35. property>
    36. bean>
    37. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    38. <property name="basePackage" value="com/javaxl/ssm/**/mapper"/>
    39. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    40. bean>
    41. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    42. <property name="dataSource" ref="dataSource" />
    43. bean>
    44. <tx:annotation-driven transaction-manager="transactionManager" />
    45. <aop:aspectj-autoproxy/>
    46. beans>
    javaBean交给Spring进行管理?
    
    在BookBizimpl上方提啊你啊@service注解
    
    相当于
    
    在属性上方添加@autowised
    
    相当于:
    
    
    
    
    
    
    

     

     


           
       

       
       

    事务管理器,管理事务的提交、回滚

    在test类加入注解:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})

     在属性前面加入注解:@Autowired
     

     

    测试 

    2、Aop整合分页插件

    利用pageHelper分页,

    PageHelper.start与后续结果处理即PageInfo处理代码是完全重复的

    上述图中重复的即非核心的业务代码

    根据Spring Aop所学

    共性的非核心业务代码用通知解决

    核心业务代码,前面后面都需要添加 共性的非核心的业务代码,利用环绕通知解决
     

    分析:

    1.找到目标对象——>*Biz.*Pager以biz结尾类或者接口,pager方法

    2.写通知

            2.1 目标对象 该方法一定携带PageBean对象

            2.2 if(pageBean!=null&&pageBean.isPagination()){ // 拿到页数和行数 PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); }

            2.3执行目标对象

            2.4 if(pageBean!=null&& pageBean.isPagination()){ // 处理结果的前提,是需要分页的 // maps经过第三方插件进行处理 分页 PageInfo info = new PageInfo(maps); pageBean.setTotal(info.getTotal()+""); }

    @aspect——>代表了通知/切面

    @component——>交个Spring进行管理
     

    创建切面包和类 

     

    1. @Component
    2. @Aspect
    3. public class PagerAspect {
    4. /**
    5. * 第一个*代表返回值
    6. * 第二个*:代表无限包
    7. * *Service:以service结尾接口名
    8. * *Page:以Page结尾的方法
    9. * 只要同事匹配上述四个条件,就会被列为目标对象
    10. * 上述配置要生效,代理注解不能少
    11. * @param args
    12. * @return
    13. * @throws Throwable
    14. */
    15. @Around("execution(* *..*Service.*Pager(..))")
    16. public Object invoke(ProceedingJoinPoint args) throws Throwable {
    17. Object[] params = args.getArgs();
    18. PageBean pageBean = null;
    19. for (Object param : params) {
    20. if(param instanceof PageBean){
    21. pageBean = (PageBean)param;
    22. break;
    23. }
    24. }
    25. if(pageBean != null && pageBean.isPagination())
    26. PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
    27. //执行目标方法
    28. Object list = args.proceed(params);
    29. if(null != pageBean && pageBean.isPagination()){
    30. PageInfo pageInfo = new PageInfo((List) list);
    31. pageBean.setTotal(pageInfo.getTotal()+"");
    32. }
    33. return list;
    34. }

    test:

     

  • 相关阅读:
    指纹浏览器开发指南-EasyBR
    Michael.W基于Foundry精读Openzeppelin第35期——Ownable.sol
    服了呀,被现在的00后卷麻了....
    java 基于 SpringMVC+Mybaties+ easyUI 快递公司管理系统 的 设计与实现
    实现和CSS一样的easing动画?直接看Mozilla、Chromium源码!
    极致CMS翻译插件自动批量多语种翻译
    【微服务】Hystrix的概念、作用以及使用方法
    HTTP1.0,HTTP2.0,HTTP3.0
    Dart 异常详解
    【Visual Leak Detector】QT 中 VLD 输出解析(一)
  • 原文地址:https://blog.csdn.net/weixin_65211978/article/details/126342465