Day55
mybatis的缓存
缓存:将上次的查询结果进行记录;为下次的同构查询提供查询结果;降低与数据库的交互;
一级缓存:SqlSession
该缓存默认开启
mybatis的配置文件中:
<settings>
<setting name="cacheEnabled" value="false|true" /> 默认开启为true
</settings>
mapper.xml映射文件:
<cache /> 开启缓存
两次同构查询之间,进行一次提交操作,也会清空缓存;
重新创建SqlSession对象,也会清空缓存;
二级缓存:SqlSessionFactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
SqlSession sqlSession=factory.openSession();
....sqlSession.select();//连接数据库
sqlSession.commit();
sqlsession.close();
factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
sqlSession=factory.openSession();
....sqlSession.select();//连接数据库
sqlSession.commit();
sqlsession.close();
注:对于当前实体对象实现序列化;实现
Spring
解决的问题
解决现有web工程中硬编码的问题;比如
UserController类
UserService userService=new UserServiceImpl();//属性;
配置spring框架
添加依赖
在原来mybatis的基础上,添加
<!--spring的依赖:-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!--mybatis-spring依赖;:-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.
<!--添加spring-jdbc依赖;:-->
<!--spring对数据库访问层的支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
创建配置文件
spring.xml
创建配置文件:spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
SqlSessionFactoryBean--spring管理mybatis
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--mybatis的核心文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!--mapper映射文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
扫描dao的接口:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--mapper接口的包-->
<!--mybatis生成的代理对象的默认名称是:首字母小写的类名,例:接口UserMapper,默认生成的id名称:userMapper-->
<property name="basePackage" value="com.qf.mapper"/>
<!--关联mybatis-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
</beans>
依赖注入 DI
对象的属性需要赋值;
比如:
UserServlet类对象的创建
—由服务器tomcat创建
UserService接口 userService;//默认为null---在类内调用
从工厂中获得对象:
初始化工厂:ApplicationContext ac=new ClassPathXMLApplicationContext("spring.xml");
获得对象:UserService接口 userService=ac.getBean(参数);
使用动态代码块
{
初始化工厂:ApplicationContext ac=new ClassPathXMLApplicationContext("spring.xml");
获得对象:UserService接口 userService=ac.getBean(参数);
}
UserMapper类对象的创建
UserMapper接口
UserMapper接口--由mybatis生成代理实现类;spring管理mybatis的实例;
UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
spring工厂中:spring.xml
<bean id="" class="UserService接口的实现类全类名称"/>
<bean id="" class="">
<property name="属性名" value="属性值"/>
</bean>