MyBatis入门级(增删改查)

1.接口 StudentMapper

package com.tty.mybatis.dao;

import com.tty.mybatis.pojo.Student;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

public interface StudentMapper {

    // 还可以这样写节省时间
    @Select("select `id`,`name`,`student_sex` from student where `id` = #{id}")
    //查询 id 查询学生
    public Student retrieveStudentById(Integer id);
    //增加
    public void addStudent(Student student);
    //更新
    public void updateStudentName(Map<String,Object> map);
    // 普通写法 :public void updateStudentName(@Param("id") Integer id, @Param("newName") String newName);
    // 删除
    public void deleteStudent(Integer id);
}


2.学生映射配置文件 StudentMapper.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">
<!-- student SQL 映射文件 -->

<mapper namespace="com.tty.mybatis.dao.StudentMapper">  <!-- 命名空间,写 StudentMapper的路径 -->

    <!-- 查询 -->
    <select id="retrieveStudentById" resultType="student">  <!-- 这里的id写之前的命名的 , resultType是封装结果类型写 import 后面的 -->
        select `id`,`name`,`student_sex` from student where `id` = #{id}
    </select>

    <!-- 插入 -->
    <insert id="addStudent" >
        INSERT  INTO  `student` VALUES (#{id},#{name},#{studentSex})
    </insert>

    <!-- 更新 -->
    <update id="updateStudentName">
            UPDATE `student` SET `name`=#{newName} WHERE `id`=#{id}
    </update>
    
    <!-- 删除 -->
    <delete id="deleteStudent">
        DELETE  FROM  `student` WHERE  `id`=#{id}
    </delete>
</mapper>


3.测试类 TestStudent

import com.tty.mybatis.dao.StudentMapper;
import com.tty.mybatis.pojo.Student;
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 org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class TestStudent {
    @Test
    public void test() {
 SqlSession sqlSession=getSqlSession();
    try {
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //查询
        Student student = mapper.retrieveStudentById(1);
        System.out.println(student);
    //增加
        mapper.addStudent(new Student(null,"怀怀","女"));
    //更新
        //可使用的参数 are [0, 1, param1, param2]
        //原理:会封装成一个hashmap,所以 map.put("parm1",8)  map.put("parm2","钟佳文说我来啦~~")
        /* 这样写会显得比较不爽,我们可以在StudentMapper里面写注解即可 @Param
        * 或者用另外一种方法 那就是 创建 HashMap
        * */
//        mapper.updateStudentName(8,"钟佳文说我是大胖胖~~");
        // Map方法
        Map<String,Object> params = new HashMap<>();
        params.put("id",8);
        params.put("newName","我getlog4j.xml~");
        mapper.updateStudentName(params);
        sqlSession.commit();
    //删除
        mapper.deleteStudent(8);
        sqlSession.commit();
    }finally {
        sqlSession.close();
    }

    }
    public SqlSession getSqlSession(){
        String mybatisConfigFile = "mybatis-config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            return sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_49174867/article/details/124828898
今日推荐