Mybatis之增删改查

CRUD

  1. namespace:namespace中的包名要和Dao/mapper接口的包名一致
  2. 重要参数:
    • id:对应的namespace中的方法名;
    • resultType:Sql语句执行的返回值;
    • parameterType:参数类型

接口(UserMapper.java)

package com.lb.dao;

import com.lb.pojo.User;

import java.util.List;

public interface UserMapper {
    //查询全部用户
    List<User> getUserList();

    //根据ID查询用户
    User getUserById(int id);

    //insert一个用户
    int addUser(User user);

    //update修改用户
    int updateUser(User user);

    //delete删除用户
    int deleteUser(int id);
}

sql语句(UserMapper.xml(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">
<!--namespace:绑定一个Dao/Mapper接口-->
<mapper namespace="com.lb.dao.UserMapper">

    <!--select查询语句-->
    <select id="getUserList" resultType="com.lb.pojo.User">
        select * from mybatis.user
    </select>

    <select id="getUserById" parameterType="int" resultType="com.lb.pojo.User">
        select * from mybatis.user where id=#{id}
    </select>

<!--insert插入语句    -->
    <insert id="addUser" parameterType="com.lb.pojo.User">
        insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
    </insert>
    
<!--update修改语句    -->
    <update id="updateUser" parameterType="com.lb.pojo.User">
        update mybatis.user set name =#{name}, pwd=#{pwd} where id=#{id};
    </update>

<!--delete删除语句    -->
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id};
    </delete>
</mapper>

测试类(UserDaoTest.java)

package com.lb.dao;

import com.lb.pojo.User;
import com.lb.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {

    @Test
    public void test() {

        //第一步:获得SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        try {
            //第二步:执行SQL获得接口  getMapper()
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.getUserList();

            for (User user : userList) {
                System.out.println(user);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭SqlSession
            sqlSession.close();
        }
    }

    @Test
    public void getUserById() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user);

        sqlSession.close();
    }

    //增删改查需要提交事务(commit)
    @Test
    public void addUser() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int add = mapper.addUser(new User(7, "哈哈", "123456"));

        if (add > 0) {
            System.out.println("插入成功");
        }
        //提交事务
        sqlSession.commit();

        sqlSession.close();
    }

    @Test
    public void updateUser() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(5, "王二", "123456"));

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void deleteUser() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(8);

        sqlSession.commit();
        sqlSession.close();
    }
}

注意:增删改需要提交事务

上一篇:MyBatisPlus学习笔记


下一篇:【MyBatis】Spring和Mybatis的整合