1. 程式人生 > 實用技巧 >Mybatis 完成增刪改查

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

}