mybatis的CURD

注意:

  • 增删改三步骤需要事务的提交
    • sqlsession.commit();
  • Mapper.xml主要参数
    • resultType 返回类型 (一般只又查询会用到)
    • parameterType 参数类型
    • 传参用#   例:#{s_id}
  • sql对应的标签不要对应错 —— 例:查询不能用insert

 

1、首先在Mapper接口中完善方法

package com.dao;

import com.pojo.Student;

import java.util.List;

public interface StudentMapper {
    List<Student> SelectStudent();
    Student SelectStudentByid(String s_id);
    int InsertStudent(Student student);
    int UpdateStudent(Student student);
    int DeleteByid(String s_id);

}

2、在mapper.xml中完善sql 和相应的参数

<?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="com.dao.StudentMapper">
    <select id="SelectStudent" resultType="com.pojo.Student">
        select * from school.student
    </select>

    <select id="SelectStudentByid" parameterType="String" resultType="com.pojo.Student">
        select * from school.student where s_id = #{s_id}
    </select>

    <insert id="InsertStudent" parameterType="com.pojo.Student" >
        insert into school.student (s_id,s_name,s_sex,s_birth) values
(#{s_id},#{s_name},#{s_sex},#{s_birth})
    </insert>
    
    <delete id="DeleteByid" parameterType="String">
        delete from school.student where s_id = #{s_id}
    </delete>
    
    <update id="UpdateStudent" parameterType="com.pojo.Student">
        update school.student set s_name = #{s_name},s_sex = #{s_sex}
where s_id = #{s_id}
    </update>
</mapper>

3、测试

import com.dao.StudentMapper;
import com.pojo.Student;
import com.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class Mytest {
    @Test
    public void SelectStudent() {
//       通过MybatisUtils 获取sqlsession,并通过sqlSession获取mapper
//    再调用mapper中的方法查询
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.SelectStudent();
        for (Student student : students) {
            System.out.println(student);
        }
        sqlSession.close();
    }

    @Test
    public void SelectStudentByid() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.SelectStudentByid("03");
        System.out.println(student);
        sqlSession.close();
    }

    @Test
    public void InsertStudent() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student student = new Student("09", "小番茄", "男", "1995-09-14");
        int i = mapper.InsertStudent(student);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void UpdateStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.SelectStudentByid("02");
        student.setS_name("yyf");
        student.setS_sex("lv");
        int i = mapper.UpdateStudent(student);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void DeleteByid(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.DeleteByid("09");
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }
}

万能的MAP

如果,实体类或者数据库中表的参数或者字段过多,可以使用Map

例:

  • Mapper接口中添加对应的 方法
  • 再在Mapper.xml写相应的sql语句
  • 最后Test测试

注意:

  • 这里map的key 就是sql语句中的#{……}  名称需要一致(但是可以随意取)
    • map 传递参数,直接在sql中取出key
    • 对象传递参数,直接在sql中取出属性,但是值要和定义的一模一样
 //万能的map
    List<Student> SelectStudentByMap(Map<String,Object> map);
    <select id="SelectStudentByMap" parameterType="map" resultType="com.pojo.Student">
        select * from student where s_name = #{sname};
    </select>
    @Test
    public void SelectStudentByMap(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Map<String , Object> map = new HashMap<>();
        map.put("sname","yyf");
        List<Student> students = mapper.SelectStudentByMap(map);
        for(Student student :students){
            System.out.println(student);
        }
        sqlSession.close();

    }

模糊查询

  • 在java执行中添加通配符“%”
  • 在xml的sql语句中进行字符串拼接加入%
 //模糊查询
    List<Student> SelectStudentLike(String value);
    <!--使用模糊查询 -->
    <select id="SelectStudentLike" parameterType="String" resultType="com.pojo.Student">
        select * from student where s_name like "%"#{value}"%"
    </select>

或者
    <select id="SelectStudentLike" parameterType="String" resultType="com.pojo.Student">
        select * from student where s_name like #{value}
    </select>
    @Test
    public void SelectStudentLike() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.SelectStudentLike("王");
        for (Student student : students) {
            System.out.println(student);
        }

        sqlSession.close();
    }

    public void SelectStudentLike() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.SelectStudentLike("%王%");
        for (Student student : students) {
            System.out.println(student);
        }

        sqlSession.close();
    }

上一篇:Java学习--day05


下一篇:小伙伴都惊呆了!1年半经验,2本学历,Curd背景,竟给30K,我的美团Offer终于来了!