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();
    }
}
上一篇:MyBatis-Spring


下一篇:Spring-声明式事务