Mybatis 完成增删改查

Mybatis 完成增删改查

1.首先要完成 Mybatis的基础配置:

博客:https://www.cnblogs.com/love2000/p/14188244.html

2.完成增删改查:

2.1到UserMapper编写接口

 UserMapper代码:

package com.xiaofu.dao;

import com.xiaofu.pojo.User;

import java.util.List;

public interface UserMapper {

    //获取所有的用户
    List<User> getUserList();

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

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

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

    //删除一个用户
    int deleteUser(int id);

}

2.2.到UserMapper.xml中实现接口

 UserMapper.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.xiaofu.dao.UserMapper">

<!--    查询所有用户-->
<!--    select查询语句  id是对应接口的方法名 resultType是返回的类型-->
    <select id="getUserList" resultType="com.xiaofu.pojo.User">
      select * from test.user
    </select>

<!--    根据id查询用户-->
<!--    id是对应接口的方法名 parameterType参数类型 resultType是返回的类型  #{id}传过来的参数-->
    <select id="getUserById" parameterType="int" resultType="com.xiaofu.pojo.User">
        select * from test.user where id = #{id}
    </select>

<!--    插入一个用户-->
<!--    id是对应接口的方法名 parameterType参数类型 因为传过来的是一个User对象所以#{id},#{name},#{pwd} 可以直接拿到对象中的值-->
    <insert id="addUser" parameterType="com.xiaofu.pojo.User"  >
        insert into test.user (id ,name , pwd) values (#{id},#{name},#{pwd})
    </insert>

<!--    修改一个用户-->
<!--    id是对应接口的方法名 parameterType参数类型  #{id}User对象中的id-->
    <update id="updateUser" parameterType="com.xiaofu.pojo.User">
        update test.user set name=#{id},pwd=#{pwd} = where id = #{id}
    </update>


<!--    删除一个用户-->
<!--    id是对应接口的方法名 parameterType参数类型  #{id} 传过来的参数-->
    <delete id="deleteUser" parameterType="int">
        delete from test.user where id = #{id}
    </delete>

</mapper>

2.3.编写测试类

 UserMapperText代码:

package com.xiaofu.dao;
import com.xiaofu.pojo.User;
import com.xiaofu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserMapperText {
    @Test
    public void text(){
        //第一步:获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第二步:执行sql
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        //第三步:关闭sqlSession
        sqlSession.close();
    }

    @Test
    public void getUserByIdText(){
        //第一步:获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第二步:获取mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //第三步:调用方法
        User userById = mapper.getUserById(2);
        System.out.println(userById);
        //第四步:关闭sqlSession
        sqlSession.close();
    }

    @Test
    public void addUser(){
        //第一步:获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第二步:获取mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //第三步:调用方法
        int res = mapper.addUser(new User(5, "haha", "4567"));
        if (res>0){
            System.out.println("插入成功");
        }
        //第四步:提交事物(由于是增删改所以要提交)
        sqlSession.commit();
        //第五步:关闭sqlSession
        sqlSession.close();


    }

    @Test
    public void updateUser(){
        //第一步:获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第二步:获取mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //第三步:调用方法
        int res = mapper.updateUser(new User(4, "haha", "4567"));
        if (res>0){
            System.out.println("更新成功");
        }
        //第四步:提交事物(由于是增删改所以要提交)
        sqlSession.commit();
        //第五步:关闭sqlSession
        sqlSession.close();
    }

    @Test
    public void deleteUser(){
        //第一步:获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第二步:获取mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //第三步:调用方法
        int res = mapper.deleteUser(1);
        if (res>0){
            System.out.println("删除成功");
        }
        //第四步:提交事物(由于是增删改所以要提交)
        sqlSession.commit();
        //第五步:关闭sqlSession
        sqlSession.close();
    }

}

 

posted @ 2020-12-28 17:09  lovelife80  阅读(134)  评论(0编辑  收藏  举报