1.实现增、删、改

zhanglei 2022年05月30日 275次浏览

StudentDao接口

package Dao;

import org.example.domain.Student;

import java.util.List;

public interface StudentDao {
    List<Student> getStudentList();
    //查询一个学生
    Student getStudent();

    //添加一个学生,返回值int表示影响的行数
    int insertStudent();

    //删除一个学生,返回值int表示影响的行数
    int deleteStudent();

    //修改一个学生的姓名,返回值int表示影响的行数
    int updateStudent();
}

StudengMapper.xml

<?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 ="Dao.StudentDao">

    <!--
        mapper是根标签
        mapper里面可以写<insert>,<update>,<select>等标签
            标签的id属性:正常是需要重写的方法名
            标签的resultType属性:将数据赋值给哪个类型的Java对象
        标签内部都有同名的sql语句
    -->

    <select id="getStudent" resultType="org.example.domain.Student">
        select id name,email,age  from student where name='张磊'
    </select>

    <select id="getStudentList" resultType="org.example.domain.Student">
        select id name,email,age  from student
    </select>

    <insert id="insertStudent" >
        insert into student values(003,'开文浩','kwh@qq.com',19)
    </insert>

    <delete id="deleteStudent">
        delete from student where id=#{ID};
    </delete>

    <update id="updateStudent">
        update student set name='陆启赛' where id='003'
    </update>
</mapper>

实体类Student

package org.example.domain;

public class Student {
    //属性名和列名一样
    private  Integer id;
    private  String name;
    private String email;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "学生实体的信息{" +
               // "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }
}

工具类MybatisUtil

package 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;

//sqlSessionFactory--->sqlSession
public class MybatisUtil {
    //提升作用域
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            //使用Mybatis第一步:获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);

            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //既然有了sqlSessionFactory,顾名思义,我们就可以从中获得sqlSession实例了
    //sqlSession完全包含了面向数据库执行SQL命令的所有方法
    public static SqlSession getSqlSession(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}

Test类

public class MyTest {
    @Test
    public void testSelect(){
            //在mybatis依赖中获取SqlSession对象
            SqlSession sqlSession = MybatisUtil.getSqlSession();
            //执行SQL语句select 方式一getMapper(Dao代理:创建实现类,获取StudentDao实现类对象)
            StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
            List<Student> studentList = studentDao.getStudentList();

      /*  //执行SQL语句select 方式二selectOne,只能执行一条
            String sqlId="Dao.StudentDao.getStudent";
            Student student = sqlSession.selectOne(sqlId);*/


        for (Student student : studentList) {
                System.out.println(student);
            }

        /*System.out.println(student);*/

            //关闭sqlSession
            sqlSession.close();
    }

    @Test
    public void testInsert(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        //执行SQL语句insert 方法insert
        String sqlId="Dao.StudentDao.insertStudent";
        int rows=sqlSession.insert(sqlId);
        //执行DML语句要手工提交事务
        sqlSession.commit();
        System.out.println("添加了一个学生:rows="+rows);
        //关闭sqlSession
        sqlSession.close();
    }

    @Test
    public void testDelete(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        //执行SQL语句delete 方法delete
        String sqlId="Dao.StudentDao.deleteStudent";
        int rows=sqlSession.delete(sqlId,003);
        //执行DML语句要手工提交事务
        sqlSession.commit();
        System.out.println("删除了一个学生: row="+rows);
        //关闭sqlSession
        sqlSession.close();

    }

    @Test
    public void testUpdate(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        //执行SQL语句update 方法update
        String sqlId="Dao.StudentDao.updateStudent";
        int rows=sqlSession.update(sqlId);
        //执行DML语句要手工提交事务
        sqlSession.commit();
        System.out.println("更新了一个学生: row="+rows);
        //关闭sqlSession
        sqlSession.close();
    }
}