回顾
代理
代理的目的: 让目标方法增强.
静态代理: 一个类有一个代理,一个代理只能代理一个类
动态代理: 可以为每个类动态的产生代理对象.
实现方式: JDK,CGLIB
JDK动态代理: 只能代理接口
CGLIB动态代理: 可以代理接口,也可以类
AOP:
织入(Weaving):把增强的方法织入到目标方法的过程
连接点(Joinpoint): 目标类中待切入(增强)的方法
切入点(Pointcut): 目标方法
切面(Aspect): 定义增强方法的类
目标类(Target): 被代理的java类
增强/通知(Advice): 作用于目标方法的增强的方法
各种增强的方法:
前置增强,后置增强,环绕通知,后置返回通知,异常通知
今天
AOP注解版
Spring整合Mybatis
Mybatis使用数据源,分页插件
AOP注解[重点]
搭建环境
创建java-maven项目
导入依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies>写配置文件
注解开发AOP
bean id=“” class=“目标类路径” --> @Service
bean id=“切面对象” class=“切面类路径” --> @Component
aop:confing
aop:aspect ref=“切面对象” --> @Aspect
aop:before -->@Before
aop:after -->@After
aop:pointcut -->@Pointcut
配置注解生效+配置开启AOP自动代理
public interface UserService {
void findUserById();
}
@Service // 让spring创建对象,将该类交给Spring托管
public class UserServiceImpl implements UserService {
@Override
public void findUserById() {
System.out.println("com.qf.service.UserServiceImpl.findUserById()");
}
}
@Component // 让spring创建其对象,将该类交给Spring托管
@Aspect // 声明该类是一个切面类
public class MyAspect {
/**
* 将切入点表达式抽取,以便复用
*/
@Pointcut("execution(* com.qf.service.*.*(..))")
public void pc(){}
/**
* 前置通知注解,其中写的是切入点表达式的方法名,注意不要忘了括号()
*/
@Before("pc()")
public void myBefore(){
System.out.println("This is before advice..." );
}
@After("pc()")
public void myAfter(JoinPoint joinPoint){
System.out.println("This is after advice..."+joinPoint.getSignature().getName() );
}
@Around("pc()")
public void myAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("This is around advice...qian");
joinPoint.proceed();
System.out.println("This is around advice...hou");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描注解使其生效 -->
<context:component-scan base-package="com.qf"/>
<!-- 开启AOP的自动代理配置 -->
<aop:aspectj-autoproxy/>
</beans>
public static void main(String[] args) {
// 获得容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 从容器获得对象,对象名一定是类名首字母小写
UserService userService = context.getBean("userServiceImpl", UserService.class);
// 执行目标方法,目标方法前后都会执行增强方法
userService.findUserById();
}
Spring整合Mybatis[重点]
Spring的特点: IOC和AOP
IOC: 控制反转,即创建对象
- 数据库的连接对象
- 创建SqlSessionFactory
- 创建SqlSession对象
- 获得Mapper的代理对象
AOP: 面向切面
- 控制事务
思路:
- 准备数据表
- 创建java-maven项目
- 导入依赖
- Spring
- spring-beans
- spring-context[只需导入这个,其他beans,core,exp,aop都会导入]
- spring-core
- spring-expression
- spring-aop
- spring-aspects
- spring-jdbc,对Dao层的支持
- mybatis
- 数据库驱动
- spring整合mybatis的单独依赖
- Spring
- 配置文件
- applicationContext.xml [重点]
- 加载数据配置文件
- 配置数据源
- 创建SqlSessionFactory
- 配置Mapper扫描器
- mybatis-config.xml
- db.properties
- log4j.properties
- applicationContext.xml [重点]
- 类文件
- UserService
- UserServiceImpl
- UserMapper.java
- UserMapper.xml
- 测试
依赖
<dependencies>
<!-- spring的核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- spring的切面 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- 对数据访问层的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- spring整合mybatis单独的jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>*.xml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
<!-- 注解扫描 -->
<context:component-scan base-package="com.qf"/>
<!-- 1加载db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2创建数据源 -->
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 设置别名 -->
<property name="typeAliasesPackage" value="com.qf.model"/>
</bean>
<!-- 4创建Mapper扫描器 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 根标签 -->
<configuration>
<!-- 此处大部分的配置,都可以交给Spring处理 -->
</configuration>
其实可以不用该文件也行
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java2212?useSSL=false
jdbc.username=root
jdbc.password=123456
log4j.properties
log4j.rootLogger=error
log4j.logger.com.qf.mapper=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
UserService&UserServiceImpl
public interface UserService {
User findUserById(int id);
}
@Service
public class UserServiceImpl implements UserService {
// 设置Dao属性
@Autowired // 自动注入Mapper
private UserMapper userMapper;
@Override
public User findUserById(int id) {
User user = userMapper.findUserById(id);
return user;
}
}
UserMaper.java&UserMapper.xml
public interface UserMapper {
User findUserById(int id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
测试
public class TestSpringMybatis {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean("userServiceImpl", UserService.class);
User user = service.findUserById(51);
System.out.println(user);
}
}
使用mybatis-config配置
在此之前的配置没有使用到mybatis-config配置文件,现在可以使用mybatis-config配置.
mybatis-config.xml中配置类型别名
<!-- 根标签 -->
<configuration>
<!-- 此处大部分的配置,都可以交给Spring处理 -->
<typeAliases>
<package name="com.qf.model"/>
</typeAliases>
</configuration>
applicationContext.xml中加载该配置文件
<!-- 3创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 设置别名 -->
<!-- <property name="typeAliasesPackage" value="com.qf.model"/>-->
</bean>
Spring整合Mybatis后,Log4j不生效的问题
Spring整合Mybatis后,Log4j不生效,原因在于整合mybatis后,mybatis自己无法确定到底使用哪一个日志框架.
所以,需要在mybatis的全局配置文件中,指定日志的实现方案为log4j
<configuration>
<!-- 此处大部分的配置,都可以交给Spring处理 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>
测试即可
整合后使用Druid数据源[重点]
目前mybatis自己使用的内部自带数据库连接池技术.但是,在开发中一般会使用Druid数据库连接池.
之前使用JDBC的问题:
频繁的创建连接和关闭连接.销毁资源
数据库连接池:
在项目运行之初,在"池子"中创建若干条数据连接对象,供连接使用.有请求需要查数据库,就从中获取连接,用完后放回池子中.这样就可以减少资源销毁,提供运行效率.
加入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
修改db配置文件
# 配置初始化大小、最小、最大
jdbc.initialSize=5
jdbc.minIdle=3
jdbc.maxActive=20
#配置获取连接等待超时的时间
jdbc.maxWait=0
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
jdbc.timeBetweenEvictionRunsMillis=600000
#配置一个连接在池中最小生存的时间,单位是毫秒
jdbc.minEvictableIdleTimeMillis=300000
修改applicationContext.xml中关于数据源部分
<!-- 2创建数据源: class换成阿里巴巴的 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 注意属性名,要换成driverClassName -->
<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="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${jdbc.maxWait}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
</bean>
测试
整合后使用分页插件[重点]
mybatis框架支持各种插件.分页插件就是由国内某程序员开发,开源在GitHub上的优秀插件.
分页插件作用: 可以自动完成分页功能.
以前需要自己在SQL中拼接limit语句,而且需要自己完成count()计算.
现在使用了分页插件,只需要正常查询全部数据,不用考虑关于分页的limit拼接问题,和count的数据条数等问题,插件全部搞定.
准备好查询全部的环境
UserService&UserServiceImpl
public interface UserService {
List<User> findAll();
}
@Service
public class UserServiceImpl implements UserService {
// 设置Dao属性
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
UsreMapper.java
public interface UserMapper {
List<User> findAll();
}
UsreMapper.xml
<select id="findAll" resultType="User">
select * from user
</select>
测试
现在开始分页插件的使用
加入依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.3.0</version> </dependency>mybatis中配置使用插件
要将分页插件的配置放在applicationContext.xml的SqlSessionFactory中
<!-- 设置分页插件 --> <property name="plugins"> <set> <!-- 创建分页插件对象,其实是一个拦截器 --> <bean class="com.github.pagehelper.PageInterceptor"> <!-- 给对象指定属性 --> <property name="properties"> <props> <!-- 指定数据库类型 --> <prop key="helperDialect">mysql</prop> </props> </property> </bean> </set> </property>在代码中使用分页功能
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean("userServiceImpl", UserService.class);
// 设置分页
PageHelper.startPage(2,2);
// 查询全部
List<User> all = service.findAll( );
for (User user : all) {
System.out.println(user );
}
// 将查询返回的集合,交给分页插件,返回给所有分页数据
PageInfo<User> pageInfo = new PageInfo<>(all);
System.out.println(pageInfo );
}
底层原理,拦截Mybatis发出SQL语句,自动拼接Limit 语句,完成分页语句.另外还会发出SELECT count(0) FROM user 计算数据条数.