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

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

结构

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>day01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


    </dependencies>
</project>

核心配置文件

<?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>
    <!-- 配置 mybatis的环境 -->
    <environments default="development">
        <!-- 配置环境 -->
        <environment id="development">
            <!-- 配置事务的类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置连接数据库的信息:用的是数据源【连接池】-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/db_school"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 注册StudentDao接口映射文件位置 -->
    <mappers>
        <mapper resource="mapper/StudentMapper.xml"/>
    </mappers>
</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">
<!--namespace的值,就是该映射文件对应的接口-->
<mapper namespace="com.etime.dao.StudentDao">
    <!--id必须是接口中需要实现的方法的名字-->
    <!--resultType查询到的数据要封装成的类型-->
    <select id="getAllStudent" resultType="com.etime.pojo.Student">
        select * from tb_student;
    </select>

    <select id="getStudentBySid" parameterType="int" resultType="com.etime.pojo.Student">
        select * from tb_student where sid = #{sid};
    </select>

    <insert id="addStudent" parameterType="com.etime.pojo.Student">
        insert into tb_student(sname,sage,ssex,semail,sphoto) values (#{sname},#{sage},#{ssex},#{semail},#{sphoto})
    </insert>

    <delete id="delStudent" parameterType="com.etime.pojo.Student">
        delete from tb_student where sid = #{sid};
    </delete>

    <update id="editStudent" parameterType="com.etime.pojo.Student">
        update tb_student set sname=#{sname},sage=#{sage},ssex=#{ssex} where sid = #{sid}
    </update>

    <select id="getStudentCount" resultType="int">
        select count(*) from tb_student;
    </select>

    <select id="getStudentBySname" parameterType="String" resultType="com.etime.pojo.Student">
        select * from tb_student where sname like #{sname};
    </select>

    <select id="getCountBySname" parameterType="String" resultType="int">
        select count(*) from tb_student where sname like #{sname};
    </select>
</mapper>

工具类

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<Student> getAllStudent();

    Student getStudentBySid(int sid);

    int addStudent(Student student);

    int delStudent(int sid);

    int editStudent(Student student);

    int getStudentCount();

    List<Student> 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<Student> 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, "女", "mary@qq.com", "");
        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<Student> 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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaGgIeOo0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值