所有创建对象的动作都交给Spring来完成
将上篇的分页放到通知里来完成
目录
解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
-
-
-
- <spring.version>5.0.2.RELEASEspring.version>
-
- <mybatis.version>3.4.5mybatis.version>
-
- <mysql.version>5.1.44mysql.version>
-
- <pagehelper.version>5.1.2pagehelper.version>
-
- <mybatis.spring.version>1.3.1mybatis.spring.version>
-
- <commons.dbcp2.version>2.1.1commons.dbcp2.version>
- <commons.pool2.version>2.4.3commons.pool2.version>
-
- <log4j2.version>2.9.1log4j2.version>
-
- <junit.version>4.12junit.version>
- <servlet.version>4.0.0servlet.version>
- <lombok.version>1.18.2lombok.version>
- properties>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis.spring.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-dbcp2artifactId>
- <version>${commons.dbcp2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- <version>${commons.pool2.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet.version}version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>${lombok.version}version>
- <scope>providedscope>
- dependency>
- <resources>
-
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- includes>
- resource>
-
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>jdbc.propertiesinclude>
- <include>*.xmlinclude>
- includes>
- resource>
- resources>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>${maven.compiler.plugin.version}version>
- <configuration>
- <source>${maven.compiler.source}source>
- <target>${maven.compiler.target}target>
- <encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.2version>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
- dependencies>
- <configuration>
- <overwrite>trueoverwrite>
- configuration>
- 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
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- 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">
-
-
- <context:annotation-config/>
-
- <context:component-scan base-package="com.zwc.ssm"/>
-
- <context:property-placeholder location="classpath:jdbc.properties"/>
-
- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
-
- <property name="initialSize" value="10"/>
-
- <property name="maxTotal" value="100"/>
-
- <property name="maxIdle" value="50"/>
-
- <property name="minIdle" value="10"/>
-
-
- <property name="maxWaitMillis" value="-1"/>
- bean>
-
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="dataSource" ref="dataSource"/>
-
- <property name="mapperLocations" value="classpath*:com/zwc/ssm/**/mapper/*.xml"/>
-
- <property name="typeAliasesPackage" value="com/javaxl/ssm/**/model"/>
-
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <value>
- helperDialect=mysql
- value>
- property>
- bean>
- array>
- property>
- bean>
-
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
-
- <property name="basePackage" value="com/javaxl/ssm/**/mapper"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- bean>
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- <aop:aspectj-autoproxy/>
- beans>
-
javaBean交给Spring进行管理? 在BookBizimpl上方提啊你啊@service注解 相当于bean> 在属性上方添加@autowised 相当于:
事务管理器,管理事务的提交、回滚
在test类加入注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
在属性前面加入注解:@Autowired
测试
利用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进行管理
创建切面包和类
- @Component
- @Aspect
- public class PagerAspect {
- /**
- * 第一个*代表返回值
- * 第二个*:代表无限包
- * *Service:以service结尾接口名
- * *Page:以Page结尾的方法
- * 只要同事匹配上述四个条件,就会被列为目标对象
- * 上述配置要生效,代理注解
不能少 - * @param args
- * @return
- * @throws Throwable
- */
- @Around("execution(* *..*Service.*Pager(..))")
- public Object invoke(ProceedingJoinPoint args) throws Throwable {
- Object[] params = args.getArgs();
- PageBean pageBean = null;
- for (Object param : params) {
- if(param instanceof PageBean){
- pageBean = (PageBean)param;
- break;
- }
- }
-
- if(pageBean != null && pageBean.isPagination())
- PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
- //执行目标方法
- Object list = args.proceed(params);
-
- if(null != pageBean && pageBean.isPagination()){
- PageInfo pageInfo = new PageInfo((List) list);
- pageBean.setTotal(pageInfo.getTotal()+"");
- }
- return list;
- }
-
test: