Mybatis学习笔记5 面向接口CRUD练习

Mybatis学习笔记4 用javassist动态实现DAO接口基于接口的CRUD_biubiubiu0706的博客-CSDN博客

新建项目

大致目录结构

核心配置文件

文章来源地址https://uudwc.com/A/Nx6kd

日志配置文件,只需要引入logback依赖  配置文件名为logback.xml即可

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!--定义⽇志⽂件的存储地址-->
    <property name="LOG_HOME" value="D:/home"/>
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示⽇期,%thread表示线程名,%-5level:级别从左显示5
           个字符宽度%msg:⽇志消息,%n是换⾏符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- 按照每天⽣成⽇志⽂件 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--⽇志⽂件输出的⽂件名-->
            <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--⽇志⽂件保留天数-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示⽇期,%thread表示线程名,%-5level:级别从左显示5
           个字符宽度%msg:⽇志消息,%n是换⾏符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logge
                r{50} - %msg%n</pattern>
        </encoder>
        <!--⽇志⽂件最⼤的⼤⼩-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <!--mybatis log configure-->
    <logger name="com.apache.ibatis" level="TRACE"/>
    <logger name="java.sql.Connection" level="DEBUG"/>
    <logger name="java.sql.Statement" level="DEBUG"/>
    <logger name="java.sql.PreparedStatement" level="DEBUG"/>
    <!-- ⽇志输出级别,logback⽇志级别包括五个:TRACE < DEBUG < INFO < WARN < ERROR -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

映射文件

<?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">
<mapper namespace="com.example.mapper.XXXMapper">
    <insert id="insert">
        insert into t_car value(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
    </insert>
    <delete id="deleteById">
        delete from t_car where id=#{id}
    </delete>
    <update id="update">
        update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id=#{id}
    </update>
    <select id="selectById" resultType="car">
        /*
        这里可以使用* 原因是配置里配置了自动转驼峰,
        也可以通过resultMp方式或者sql里写别名的方式

          */
        select * from t_car where id=#{id}
    </select>
    <select id="selectAll" resultType="car">/*因配置了实体类包 因此这里可以写类名大写或者小写都行*/
        select * from t_car
    </select>
</mapper>

pojo实体类

XXXMapper

package com.example.mapper;

import com.example.pojo.Car;

import java.util.List;

/**
 * @author hrui
 * @date 2023/9/18 2:17
 */
public interface XXXMapper {

    int insert(Car car);

    int deleteById(Long id);

    int update(Car car);

    Car selectById(Long id);

    List<Car> selectAll();


}

单元测试

关于selectOne   selectList等等方法在sqlSession.getMapper(xxx)这个底层用javassist动态生成的实现类中封装了   也是因为这个实现类的原因:要求在映射文件中--->namespace必须是dao接口的全限定名称。id必须是dao接口中方法名

import com.example.mapper.XXXMapper;
import com.example.pojo.Car;
import com.example.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.zip.CheckedOutputStream;

/**
 * @author hrui
 * @date 2023/9/18 2:42
 */
public class Test {

    @org.junit.Test
    public void testInsert(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        XXXMapper mapper=sqlSession.getMapper(XXXMapper.class);
        int count = mapper.insert(new Car(null, "2001", "奇美拉", 20.0, "2020-01-01", "新能源"));
        System.out.println(count);
        sqlSession.commit();

    }
    @org.junit.Test
    public void testDeleteById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        XXXMapper mapper=sqlSession.getMapper(XXXMapper.class);
        int count = mapper.deleteById(3L);
        System.out.println(count);
        sqlSession.commit();
    }
    @org.junit.Test
    public void testUpdate(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        XXXMapper mapper=sqlSession.getMapper(XXXMapper.class);
        Car car = new Car(4L, "2002", "奇美拉2", 202.0, "2020-01-01", "新能源");
        int count = mapper.update(car);
        System.out.println(count);
        sqlSession.commit();

    }@org.junit.Test
    public void testSelectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        XXXMapper mapper=sqlSession.getMapper(XXXMapper.class);
        Car car = mapper.selectById(4L);
        System.out.println(car);
        sqlSession.commit();
    }
    @org.junit.Test
    public void testSelectAll(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        XXXMapper mapper=sqlSession.getMapper(XXXMapper.class);
        List<Car> cars = mapper.selectAll();
        System.out.println(cars);
        sqlSession.commit();
    }

}

原文地址:https://blog.csdn.net/tiantiantbtb/article/details/132962047

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇 2023年09月25日 12:02
下一篇 2023年09月25日 12:05