day44 6-9 spring框架IOC与AOP(视频) & 6-10 集成mybatis框架 & 6-11 mybatis映射文件标签 & 6-12 条件查询

image

MyBatis

什么是持久化

将数据(如内存中对象)保存到可以永久保存的存储设备中(如磁盘),持久化的主要应用是将内存的数据存储在关系型数据库中,也可存储在磁盘文件、xml数据文件中等

什么是持久层

系统中专注于实现数据持久化的一个逻辑层面,将数据使用者和数据实体相关联。

什么是ORM

Object-Relational Mapping 在关系型数据库和类对象之间作一个映射(吧数据库表和实体类的属性对应起来,让开发者操作实体类就能操作数据库表)

什么是MyBatis

是一个用Java编写的持久层框架,它使用ORM实现了结果集的封装。它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需专注注册驱动,创建连接等。

集成Mybatis

pom.xml配置依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- mysql驱动包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>

    <!-- mybatis核心包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!-- mybatis集成spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    <!-- 导入dbcp数据库连接池的依赖,用来在spring-mybatis.xml中配置数据库 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>

spring.xml配置mybatis

<!--********************MyBatis配置********************-->
   <!-- 1 引入jdbc配置文件 -->
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="classpath:jdbc.properties" />
   </bean>
   <!-- 2.配置数据源-dbcp数据库连接池 -->
   <bean id="dbSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="${driver}" />
      <property name="url" value="${url}" />
      <property name="username" value="${username}" />
      <property name="password" value="${password}" />
      <!-- 最大空闲连接数 -->
      <property name="maxIdle" value="${maxIdle}"/>
      <!-- 初始化连接数 -->
      <property name="initialSize" value="${initialSize}"/>
      <!--最大连接等待时间,连接超时时间 单位:ms-->
      <property name="maxWait" value="${maxWait}"></property>
   </bean>
   <!-- 3 spring和MyBatis整合,通过spring来管理MyBatis的SqlSessionFactory会话工厂 -->
   <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- 指定数据库连接池引用 -->
      <property name="dataSource" ref="dbSource" />
      <!-- 引入mybatis配置文件 -->
      <property name="configLocation" value="classpath:mybatis.xml"/>
      <!-- 自动扫描mapping.xml文件 -->
      <!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
   </bean>
   <!--sqlSession模板对象,sqlSession可以实现数据库连接以及包含所有执行SQL操作的方法-->
   <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg index="0" ref="ssf" />
   </bean>

jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8&allowMultiQueries=true
username=root
password=123456

initialSize=0
maxIdle=20
minIdle=1
maxWait=30000

配置mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
        <!-- 开启sql日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!-- 开启下划线转驼峰的配置 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!--类型别名-->
    <typeAliases>
        <!-- 扫描该包下所有类,每个类的别名是其类名首字母小写,如 User类的别名为 user -->
        <package name="com.st.model"/>
    </typeAliases>

    <!-- mapping文件(ORM对象关系映射文件)路径配置 -->
    <mappers>
        <mapper resource="mapper/StudentMapper.xml" />
    </mappers>

</configuration>

mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">
<mapper namespace="com.st.dao.IStudentDao">
  <!--namespace属性关联dao层的接口-->
  <!-- 结果集映射关系 -->
  <resultMap type="student" id="baseMap">
    <result jdbcType="INTEGER" property="id" column="id"/>
    <result jdbcType="VARCHAR" property="sName" column="s_name"/>
    <result jdbcType="INTEGER" property="sSex" column="s_sex"/>
  </resultMap>

  <!--id属性对应接口的签名方法 parameterType指定传入的数据类型 resultType 指定接收结果集的数据类型 resultMap指定结果集映射关系-->
  <select id="query" resultMap="baseMap" parameterType="student">
    select * from student 
    <where>
      <if test="id != null"> and id = #{id}</if>
      <if test="sName != null and sName != ''"> and s_name = #{sName}</if>
      <if test="sSex != null"> and s_sex = #{sSex}</if>
    </where>
    <if test="pageNo != null">limit #{pageNo},#{rowCount} </if>
  </select>

  <select id="queryAll" resultType="student">select * from student </select>

  <insert id="add" parameterType="student">
    insert into student 
    <!-- prefix前缀 suffix后缀 suffixOverrides去除拼接语句的最后一个逗号-->
    <trim suffixOverrides="," suffix=")" prefix="(">
      <if test="sName != null and sName !='' ">s_name,</if>
      <if test="sSex != null">s_sex,</if>
    </trim>
    <trim suffixOverrides="," suffix=")" prefix="VALUES(">
      <if test="sName != null and sName !='' ">#{sName},</if>
      <if test="sSex != null">#{sSex},</if>
    </trim>
  </insert>

  <update id="update" parameterType="student">
    update student 
    <set>
      <if test="sName != null and sName !='' ">s_name = #{sName},</if>
      <if test="sSex != null">s_sex = #{sSex},</if>
    </set>
    <where>
      <if test="id != null">and id = #{id}</if>
    </where>
  </update>

  <delete id="del" parameterType="java.lang.Integer">delete from student where id = #{id} </delete>
</mapper>

dao层实现类

package com.st.dao.impl;

import com.st.dao.IStudentDao;
import com.st.model.Student;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class StudentDao implements IStudentDao {
    @Resource
    private SqlSession sqlSession;

    @Override
    public List<Student> query(Student student) {
        return sqlSession.selectList("com.st.dao.IStudentDao.query", student);
    }

    @Override
    public List<Student> queryAll() {
        return sqlSession.selectList("com.st.dao.IStudentDao.queryAll");
    }

    @Override
    public Integer add(Student student) {
        return sqlSession.insert("com.st.dao.IStudentDao.add", student);
    }

    @Override
    public Integer update(Student student) {
        return sqlSession.update("com.st.dao.IStudentDao.update", student);
    }

    @Override
    public Integer del(Integer id) {
        return sqlSession.delete("com.st.dao.IStudentDao.del", id);
    }
}

mapper内查询和分页方法

   <!-- id属性对应接口的签名方法 resultMap指定结果集映射关系-->
    <select id="query" parameterType="student" resultMap="baseMap">
        select * from student
        <where>
            <if test="id != null"> and id = #{id}</if>
            <if test="sName != null and sName != ''"> and s_name = #{sName}</if>
            <if test="sSex != null"> and s_sex = #{sSex}</if>
        </where>
        <if test="pageNo != null">
            limit #{pageNo},#{rowCount}
        </if>
    </select>

    <select id="queryCount" parameterType="student" resultType="java.lang.Integer">
        select count(*) from student
        <where>
            <if test="id != null"> and id = #{id}</if>
            <if test="sName != null and sName != ''"> and s_name = #{sName}</if>
            <if test="sSex != null"> and s_sex = #{sSex}</if>
        </where>
    </select>
posted @ 2022-12-05 21:14  小彤在努力  阅读(23)  评论(0)    收藏  举报