[MyBatis]增删改查

CRUD

接上篇博客:https://www.cnblogs.com/zlc364624/p/14593990.html

注意:namespace与类名一致

UserMapper.xml中

<select id="getUserList" resultType="com.zlc.pojo.User">

id代表namespace中类的方法名

resultType为返回值

 

编写接口:

package com.zlc.dao;

import com.zlc.pojo.User;

import java.util.List;

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

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

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

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

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

修改参数:

<?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.zlc.dao.UserMapper">
    <!--select查询语句-->
    <select id="getUserList" resultType="com.zlc.pojo.User">
        select * from mybatis.user
    </select>
    
    <select id="getUserById" parameterType="int" resultType="com.zlc.pojo.User">
        select * from mybatis.user where id =#{id}
    </select>

    <insert id="addUser" parameterType="com.zlc.pojo.User">
        insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd})
    </insert>

    <update id="updateUser" parameterType="com.zlc.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
    </update>
    
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id};
    </delete>
</mapper>

测试

package com.zlc.dao;

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

import java.util.List;

public class UserMapperTest {
    @Test
    public void test(){
        //获取SqlSession
        SqlSession sqlSession= MybatisUtils.getSqlSession();
        //执行SQL
        UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
        List<User> userList= userMapper.getUserList();

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

        //关闭
        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();
    }

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

        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        int res=mapper.addUser(new User(4,"流流","445682"));
        if(res>0){
            System.out.println("插入成功!");
        }
        //提交事务
        sqlSession.commit();

        sqlSession.close();
    }

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

        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        int res=mapper.updateUser(new User(4,"呜呜","111222"));
        if(res>0){
            System.out.println("修改成功!");
        }
        //提交事务
        sqlSession.commit();
        //关闭
        sqlSession.close();
    }

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

        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        //设置返回
        int res=mapper.deleteUser(4);
        if(res>0){
            System.out.println("删除成功!");
        }
        //提交事务
        sqlSession.commit();
        //关闭
        sqlSession.close();
    }
}

 

posted @ 2021-03-06 03:31  雾霾王者  阅读(43)  评论(0编辑  收藏  举报