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

新建项目

Mybatis学习笔记5 面向接口CRUD练习_第1张图片

大致目录结构

核心配置文件

Mybatis学习笔记5 面向接口CRUD练习_第2张图片

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



    
    
    
    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    
    
    
        
            
            ${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log
            
            30
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logge
                r{50} - %msg%n
        
        
        
            100MB
        
    
    
    
    
    
    
    
    
        
        
    

映射文件

 

    
        insert into t_car value(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
    
    
        delete from t_car where id=#{id}
    
    
        update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id=#{id}
    
    
    

pojo实体类

Mybatis学习笔记5 面向接口CRUD练习_第3张图片

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 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 cars = mapper.selectAll();
        System.out.println(cars);
        sqlSession.commit();
    }

}

Mybatis学习笔记5 面向接口CRUD练习_第4张图片

你可能感兴趣的:(学习,笔记)