LomBok的使用,MyBatis的使用(增删改查)

Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发。通过添加注解的方式,不需要 为类编写getter或equels方法,同时可以自动化日志变量。

结构

LomBok的使用,MyBatis的使用(增删改查)_第1张图片

pom



    4.0.0

    org.example
    day01
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            org.projectlombok
            lombok
            1.18.16
        
        
            org.mybatis
            mybatis
            3.5.1
        
        
            mysql
            mysql-connector-java
            8.0.25
            runtime
        
        
            junit
            junit
            4.12
            test
        


    

核心配置文件




    
    
        
        
            
            
            
            
                
                
                
                
            
        
    
    
    
        
    

映射文件






    
    
    

    

    
        insert into tb_student(sname,sage,ssex,semail,sphoto) values (#{sname},#{sage},#{ssex},#{semail},#{sphoto})
    

    
        delete from tb_student where sid = #{sid};
    

    
        update tb_student set sname=#{sname},sage=#{sage},ssex=#{ssex} where sid = #{sid}
    

    

    

    

工具类

package com.etime.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtils {
    private static SqlSession sqlSession =  null;

    static {
        try {
            InputStream in = Resources.getResourceAsStream("config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(in);
            sqlSession = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return sqlSession;
    }

    public static void closeSqlSession(SqlSession sqlSession){
        sqlSession.close();
    }
}

实体类

package com.etime.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private int sid;
    private String sname;
    private int sage;
    private String ssex;
    private String semail;
    private String sphoto;
}

dao

package com.etime.dao;

import com.etime.pojo.Student;

import java.util.List;

public interface StudentDao {
    List getAllStudent();

    Student getStudentBySid(int sid);

    int addStudent(Student student);

    int delStudent(int sid);

    int editStudent(Student student);

    int getStudentCount();

    List getStudentBySname(String sname);

    int getCountBySname(String sname);
}

单元测试

package com.etime.text;

import com.etime.dao.StudentDao;
import com.etime.pojo.Student;
import com.etime.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class Text {
    //查询所有学生
    @Test
    public void to1(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List list = studentDao.getAllStudent();
        list.forEach(System.out::println);
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //根据主键查询
    @Test
    public void to2(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student student = studentDao.getStudentBySid(21);
        System.out.println(student);
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //新增
    @Test
    public void to3(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student(0, "mary", 21, "女", "[email protected]", "");
        int row = studentDao.addStudent(student);
        System.out.println(row);
        sqlSession.commit();
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //根据主键删除
    @Test
    public void to4(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        int row = studentDao.delStudent(56);
        System.out.println(row);
        sqlSession.commit();
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //修改
    @Test
    public void to5(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        int row = studentDao.editStudent(new Student(49, "mybatis", 21, "男", "", ""));
        System.out.println(row);
        sqlSession.commit();
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //查询表中数据条数
    @Test
    public void to6(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        int count = mapper.getStudentCount();
        System.out.println(count);
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //根据表中某字符串列进行模糊查询
    @Test
    public void to7(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        String sname = "%建%";
        List list = studentDao.getStudentBySname(sname);
        list.forEach(System.out::println);
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
    //根据姓名模糊查询数据条数
    @Test
    public void to8(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        int count = studentDao.getCountBySname("%建%");
        System.out.println(count);
        SqlSessionUtils.closeSqlSession(sqlSession);
    }
}

你可能感兴趣的:(ssm,mybatis,java,开发语言)