MyBatis增删改查

CRUD

增删改必须提交事务才会生效。

sqlSession.commit();

1、namespace

namespace中的包名要和Dao/Mapper接口的包名一致。

2、select、update、insert、delete

查询语句:

  • id:就是对应的namespace中的方法名
  • resultType:SQL语句执行的返回值。
  • parameterType:参数类型。

编写Mapper方法

package com.study.dao;

import com.study.pojo.User;

import java.util.List;

public interface UserMapper {
    public List<User> getUserList();

    public User selectUserById(int id);

    public void insertUser(User user);

    public void updateUser(User user);

    public void deleteUser(int id);
    
}

编写mapper.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">

<!--绑定UserMapper-->
<mapper namespace="com.study.dao.UserMapper">
    <!--id和方法名一致-->
    <select id="getUserList" resultType="com.study.pojo.User">
        select * from user;
    </select>

    <select id="selectUserById" parameterType="int" resultType="com.study.pojo.User">
        select * from user where `id`=#{id};
    </select>

    <insert id="insertUser" parameterType="com.study.pojo.User">
        insert into user(`id`,`name`,`pwd`) values(#{id},#{name},#{pwd});
    </insert>

    <update id="updateUser" parameterType="com.study.pojo.User">
        update user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>
</mapper>

编写Junit测试

package com.study.dao;

import com.study.pojo.User;
import com.study.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();

        //方式一:
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserList();

        for(User user : userList){
            System.out.println(user);
        }

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

    @Test
    public void selectUserById(){
        //1、获取sqlSession
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.selectUserById(4);
        System.out.println(user);

        //2、关闭sqlSession,防止内存泄漏
        sqlSession.close();
    }

    @Test
    public void inserTest(){
        //1、获取sqlSession
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User(4,"小红","654321");
        mapper.insertUser(user);

        //2、提交事务
        sqlSession.commit();

        //3、关闭sqlSession,防止内存泄漏
        sqlSession.close();
    }

    @Test
    public void updateTest(){
        //1、获取sqlSession
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User(4,"小蓝","54321");
        mapper.updateUser(user);

        //2、提交事务
        sqlSession.commit();

        //3、关闭sqlSession,防止内存泄漏
        sqlSession.close();
    }

    @Test
    public void deleteTest(){
        //1、获取sqlSession
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.deleteUser(4);

        //2、提交事务
        sqlSession.commit();

        //3、关闭sqlSession,防止内存泄漏
        sqlSession.close();
    }
}

错误分析

  • 标签不要匹配错误
  • resource绑定mapper,需要使用路径!
  • 程序配置文件必须符合规范!
  • NullPointException,没有注册到资源!
  • 输出的xml文件中文存在中文乱码问题!
  • maven资源没有导出问题!

你可能感兴趣的:(#,MyBatis)