1.mybatis的回顾
mybatis它是java持久层的框架,也是ORM实现框架,可以完成sql的映射的处理,通过程序调用sql映射里面的方法,可以完成自动封装参数;
SqlSessionFactory–>SqlSession–>调用 selectList/selectOne/insert/update/delete
2. mybatis使用
3 sql映射器 Mapper
(1) 写一个接口 TeacherMapper
写方法
(2)配置文件 TeacherMapper.xml
<mapper namespace="cn.itsource._02_mapper.TeacherMapper">
<!-- 查询 queryAll()
resultType 返回类型
-->
<select id="queryAll" resultType="Teacher">
select * from t_teacher
</select>
</mapper>
(3)写一个测试类进行测试
@Test
public void queryAll() {
SqlSession sqlSession = MyBatisUtils.INSTANCE.getSqlSession();
//底层动态代理 userDaoImpl
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
for (Teacher teacher : mapper.queryAll()) {
System.out.println(teacher);
}
}
4 高级查询
<select id="queryList" parameterType="teacherQuery" resultType="Teacher">
select * from t_teacher
<where>
<if test="name!=null">
and name like concat("%",#{name},"%")
</if>
<if test="minAge !=null">
and age > #{minAge}
</if>
<!--特殊符号需要转义
<if test="maxAge != null">
and age < #{maxAge}
</if>
-->
<!-- 放入cdata区域里面被xml忽略解析
<if test="maxAge != null">
<![CDATA[
and age < #{maxAge}
]]>
</if>-->
</where>
</select>
5 ResultMap结果集映射
表和表之前有关系:
一对一 一对多 多对一 多对多
多对一:
多个员工对应一个部门
多个产品 对应一个产品分类
多对学生 对应一个老师
5.1 多对一(一对一)–嵌套结果
<resultMap id="studentMap" type="student">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="hobby" column="hobby"></result>
<association property="teacher" javaType="Teacher">
<id property="id" column="tid"></id>
<result property="name" column="tname"></result>
<result property="age" column="tage"></result>
</association>
</resultMap>
<select id="queryAll" resultMap="studentMap">
select s.id ,s.name,s.hobby,t.id tid,t.name tname,t.age tage
from t_student s
join t_teacher t on s.tid = t.id
</select>
5.2 多对一(一对一) --嵌套查询
<resultMap id="studentMap" type="student">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="hobby" column="hobby"></result>
<association property="teacher" column="tid" javaType="Teacher" select="queryTeacherByTid" >
</association>
</resultMap>
<select id="queryAll" resultMap="studentMap">
<!-- id name hobby tid -->
select s.* from t_student s
</select>
<select id="queryTeacherByTid" parameterType="long" resultType="Teacher">
select * from t_teacher where id=#{id}
</select>
5.3 一对多(多对多) – 嵌套结果
<resultMap id="teacherMap" type="Teacher">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<collection property="students" javaType="arraylist" ofType="student">
<id property="id" column="sid"></id>
<result property="name" column="sname"></result>
<result property="hobby" column="shobby"></result>
</collection>
</resultMap>
<select id="loadAll" resultMap="teacherMap">
SELECT
t.id,
t. NAME,
t.age,
s.id sid,
s. NAME sname,
s.hobby shobby
FROM
t_teacher t
JOIN t_student s ON t.id = s.tid
</select>
5.4 一对多(多对多) 嵌套查询
<!-- 嵌套查询-->
<resultMap id="teacherMap" type="Teacher">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<collection property="students" column="id" javaType="arraylist"
ofType="student" select="findStudentsByTid">
</collection>
</resultMap>
<select id="loadAll" resultMap="teacherMap">
SELECT t.id,t. NAME,t.age FROM t_teacher t
</select>
<select id="findStudentsByTid" parameterType="long" resultType="student">
select * from t_student where tid=#{tid}
</select>
6 延迟加载
lazy load: 需要使用的才去加载-- jpa(FetchType.LAZY)
mybatis可以配置lazyload–(了解)
前后端分离项目 – lazy用不大的
7 缓存
jpa:
一级缓存(自带) 属于entityManager缓存 – 命中条件 同一个EntityManagerFactory 同一个EntityManager 同OID
二级缓存(需要配置实现) 属于entitytManagerFactory – 命中条件 同一个EntityManagerFactory 不同EntityManager 同OID
缓存有什么用: 用空间换取时间概念
Mybatis它也有缓存:
一级缓存: 属于sqlSession级别 同一个SqlSessionFactory 同一个SqlSession 同一个id
二级缓存:属于sqlSessionFactory级别 同一个SqlSessionFactory 不同一个SqlSession 同一个id
序列化: 把对象转换成二进制形式 一个对象为什么需要序列化 --方便传输(特别需要保存到磁盘或者硬盘,需要你要去进行序列化)
反序列化: 把二进制的数据转换对象
8 SSM集成
开发项目,都需要把框架整合起来, SSM 目前而且 企业使用最多架构模式
ssm–>springmvc+spring+mybatis
步骤:
(1) 导入jar包
拷贝包, maven(工作里面)
(2) 配置文件
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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
">
<!--扫描service层-->
<context:component-scan base-package="cn.itsource.ssm.service"></context:component-scan>
<!-- (1)jdbc.properties (2)dataSource (3)sqlSessionFactory (4)Tranactions-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--(2)dataSource连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<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>
<!-- mapper.xml位置-->
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"></property>
<!--别名配置-->
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.domain
cn.itsource.ssm.query
</value>
</property>
</bean>
<!-- 处理mapper接口 spring会扫描包产生很多子类 注入到service层-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper"></property>
</bean>
<!--事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
applicationContext-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描controller-->
<context:component-scan base-package="cn.itsource.ssm.controller" />
<!--静态资源处理-->
<mvc:default-servlet-handler />
<!--识别@requestMappering等注解支持-->
<mvc:annotation-driven />
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 读取spring配置文件-->
<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>
<!-- 核心控制器-->
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 编码过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
*Mapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- orm框架 sql的映射
namespace:命名空间 namespace路径+ id值
namespace怎么配置 包名.接口名 + queryAll
-->
<mapper namespace="cn.itsource.ssm.mapper.StudentMapper">
<select id="queryAll" resultType="student">
select s.* from t_student s
</select>
</mapper>
(3) 创建包和类