天天看點

Mybatis使用注解實作簡單增删改查

1、UserMapper.java

import com.wjj.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface UserMapper {
    // 擷取全部使用者
    @Select("select * from user")
    List<User> getUserList();

    // 根據id查詢使用者
    @Select("select * from mybatis.user where id = #{id}")
    User getUserById(int id);

    // 分頁查詢
    @Select("select * from mybatis.user limit #{startIndex}, #{pageSize}")
    List<User> getUserByLimit(Map<String, Integer> map);

    // 添加使用者
    @Insert("insert into mybatis.user(id, name, password) values (#{id}, #{name}, #{password})")
    int addUser(User user);

    // 更新使用者
    @Update("update user set name = #{name}, password = #{password} where id = #{id}")
    int updateUser(User user);

    // 删除一個使用者
    @Delete("delete from user where id = #{id}")
    int deleteUser(int id);
}

           

2、在配置檔案中綁定接口

<mappers>
        <!-- 綁定接口 -->
        <mapper class="com.wjj.dao.UserMapper"/>
    </mappers>
           

3、測試

import com.wjj.pojo.User;
import com.wjj.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

public class UserMapperTest {

    // 查詢所有使用者
    @Test
    public void testSelect() {
        // 擷取SqlSession對象
        SqlSession sqlSession = MybatisUtil.sqlSession();
        // 執行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserList();

        for (User user : userList) {
            System.out.println(user.toString());
        }
        // 關閉SqlSession
        sqlSession.close();
    }

    // 根據查詢使用者
    @Test
    public void testSelectById() {
        // 擷取SqlSession對象
        SqlSession sqlSession = MybatisUtil.sqlSession();
        // 執行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserById(4);
        System.out.println(user);
        // 關閉SqlSession
        sqlSession.close();
    }

    @Test
    public void testGetUserByLimit() {
        SqlSession sqlSession = MybatisUtil.sqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("startIndex", 1);
        map.put("pageSize", 5);
        List<User> userList = userMapper.getUserByLimit(map);

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

        sqlSession.close();
    }

    // 添加使用者
    @Test
    public void addUser() {
        // 擷取SqlSession對象
        SqlSession sqlSession = MybatisUtil.sqlSession();
        // 執行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int i = userMapper.addUser(new User(4, "遊安康", "123"));
        System.out.println(i);

        // 送出事務
        sqlSession.commit();
        // 關閉SqlSession
        sqlSession.close();
    }

    // 更新使用者
    @Test
    public void updateUser() {
        // 擷取SqlSession對象
        SqlSession sqlSession = MybatisUtil.sqlSession();
        // 執行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int i = userMapper.updateUser(new User(2, "kuangshen", "123"));
        System.out.println(i);

        // 送出事務
        sqlSession.commit();
        // 關閉SqlSession
        sqlSession.close();
    }

    // 删除使用者
    @Test
    public void deleteUser() {
        // 擷取SqlSession對象
        SqlSession sqlSession = MybatisUtil.sqlSession();
        // 執行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int i = userMapper.deleteUser(4);
        System.out.println(i);

        // 送出事務
        sqlSession.commit();
        // 關閉SqlSession
        sqlSession.close();
    }
}