MyBatis深入学习

1.sql映射器 Mapper的测试

先写一个类 在写一个接口 写配置文件

教师类
public class Teacher {

    private Long id;
    private String name;
    private Integer age;
    get set tostring省略了
}
接口
public interface TeacherMapper {

    List<Teacher> queryAll();
}
配置
<mapper namespace="cn.hx._02_mapper.TeacherMapper">
    <select id="queryAll" resultType="Teacher">
        select * from t_teacher
    </select>
</mapper>
测试
public class TeacherMapperTest {
    @Test
    public void testQueryAll() throws Exception{
        SqlSession sqlSession = MyBatisUtil.INSTANCE.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        mapper.queryAll().forEach(e-> System.out.println(e));
    }
}

2.高级查询

这里直接丢配置 自己进行测试

<select id="queryList" parameterType="teacherQuery" resultType="Teacher">
        select * from t_teacher
        <where>
            <if test="name!=null">
                and name like concat("%",#{name},"%")
            </if>
            <!--&gt; 大于   &lt;  小于-->
            <if test="minAge!=null">
                and age &gt; #{minAge}
            </if>
            <if test="maxAge!=null">
                and age &lt; #{maxAge}
            </if>
        </where>
    </select>

3.ResultMap结果集映射

多对一  -----嵌套结果
<resultMap id="studentMap" type="student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></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.*,t.name tname,t.age tage from t_student s join t_teacher t on s.tid = t.id
    </select>
多对一  -----嵌套查询
<resultMap id="studentMap" type="student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <association property="teacher" column="tid" javaType="Teacher" select="queryTeacherByTid"></association>
    </resultMap>
    <select id="queryAll" resultMap="studentMap">
        select * from t_student
    </select>
    <select id="queryTeacherByTid" parameterType="long" resultType="Teacher">
        select * from t_teacher where id=#{id}
    </select>
    一对多   -----嵌套结果
    <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="Student">
            <id property="id" column="sid"></id>
            <result property="name" column="sname"></result>
            <result property="age" column="sage"></result>
        </collection>
    </resultMap>
    <select id="queryAll" resultMap="teacherMap">
        select t.*,s.id sid,s.name sname,s.age sage from t_teacher t join t_student s on t.id = s.tid
    </select>
      一对多   -----嵌套查询
    <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="queryStudentByTid"></collection>
    </resultMap>
    <select id="queryAll" resultMap="teacherMap">
        select * from t_teacher
    </select>
    <select id="queryStudentByTid" parameterType="long" resultType="Student">
        select * from t_student where tid=#{tid}
    </select>

4.缓存问题

jpa的缓存:

​ 一级缓存(自带) 属于entityManager缓存 – 命中条件 同一个EntityManagerFactory 同一个EntityManager 同OID
​ 二级缓存(需要配置实现) 属于entitytManagerFactory – 命中条件 同一个EntityManagerFactory 不同EntityManager 同OID
缓存的作用用: 用空间换取时间概念

Mybatis的缓存:

​ 一级缓存: 属于sqlSession级别 同一个SqlSessionFactory 同一个SqlSession 同一个id
​ 二级缓存:属于sqlSessionFactory级别 同一个SqlSessionFactory 不同一个SqlSession 同一个id

序列化: 把对象转换成二进制形式 一个对象为什么需要序列化 --方便传输(特别需要保存到磁盘或者硬盘,需要你要去进行序列化)

反序列化: 把二进制的数据转换对象

5.SSM框架整合

ssm–>springmvc+spring+mybatis
整合步骤
1.导包
2.写配置文件
applicationContext.xml

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.itsources.ssm.service"></context:component-scan>
    <!--jdbc.properties -->
    <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/itsources/ssm/mapper/*Mapper.xml"></property>
        <!--别名配置-->
        <property name="typeAliasesPackage">
            <value>
                cn.itsources.ssm.domain
                cn.itsources.ssm.query
            </value>
        </property>
    </bean>
    <!-- 处理mapper接口 spring会扫描包产生很多子类 注入到service层-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.itsources.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.itsources.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-mcv.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.itsources.ssm.mapper.StudentMapper">
    <select id="queryAll" resultType="student">
        select * from t_student
    </select>
</mapper>

3.包与类要有层次结构,这样你的代码才不至于乱
在这里插入图片描述

一些细节问题需要注意,路径不要错,我就是因为不小心把路径写错了,找了一个多小时才看到 O(∩_∩)O哈哈~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值