Mybatis学习笔记:CRUD

文章目录


一、namespace

配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致!

二、select

需求:根据id查询用户

  1. 在UserMapper中添加对应方法
public interface UserMapper {
     //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User getUserById(int id);
}
  1. 在UserMapper.xml中添加Select语句
    <select id="getUserById" parameterType="int" resultType="com.pojo.User">
        select * from mybatis.user where id = #{id}
    </select>
  1. 测试类中测试
 @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();
    }

三、insert

需求:给数据库增加一个用户

  1. 在UserMapper中添加对应方法
public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User getUserById(int id);
    //insert 用户
    int addUser(User user);
    }
  1. 在UserMapper.xml中添加insert语句
 <insert id="addUser" parameterType="com.pojo.User">
        insert  into mybatis.user (id,name,pwd) value (#{id},#{name},#{pwd})
    </insert>
  1. 在测试类中测试
 //增删改需要提交事务
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.addUser(new User(6, "小李", "1233"));
        if(res>0){
            System.out.println("插入成功");
        }
        //提交事务
        sqlSession.commit();//很重要
        sqlSession.close();
    }

注意:增改删都必须要提交事务,即必须有:

 sqlSession.commit();

四、update

需求:修改用户的信息

  1. 在UserMapper中添加对应方法
public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User getUserById(int id);
    //insert 用户
    int addUser(User user);
    //修改用户
    int updateUser(User user);
    //删除用户
    int deleteUser(int id);
}
  1. 在UserMapper.xml中添加update语句
    <update id="updateUser" parameterType="com.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id =#{id}
    </update>
  1. 在测试类中测试
 @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(2,"小王","1115"));
        sqlSession.commit();
        sqlSession.close();
    }

五、提交事务

需求删除一个用户信息

  1. 在UserMapper中添加对应方法
public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User getUserById(int id);
    //insert 用户
    int addUser(User user);
    //修改用户
    int updateUser(User user);
    //删除用户
    int deleteUser(int id);
}
  1. 在UserMapper.xml中添加delete语句
<delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>
  1. 在测试类中测试
  @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(5);

        sqlSession.commit();
        sqlSession.close();
    }

六、Map的使用

假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应该考虑使用Map!

  1. UserMapper接口
    User addUser2(Map<String,Object> map);
  1. UserMapper.xml
 <insert id="addUser" parameterType="map">
        insert  into mybatis.user (id,name,pwd) value (#{userid},#{username},#{password})
    </insert>
  1. 测试
@Test
    public void getUserById2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("helloid",1);
        mapper.addUser2(map);
        sqlSession.close();
    }

七、如何实现模糊查询?

List<User> userList = mapper.getUserLike("%李%");
<!--模糊查询-->
    <select id="getUserLike" resultType="com.pojo.User">
        select * from mybatis.user where name like #{value}
    </select>
@Test
    public void getUserLike(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserLike("%李%");
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
上一篇:mybatis学习笔记:注解开发CRUD和Lombok


下一篇:Mybatis的CRUD