Mybatis知识小汇(2)——实现增删改查

增删改查

增删该由事务的提交

  1. namespace:命名空间中的包要和接口一致

  2. select:表示查询语句

    • id:表示实现的方法名
    • resultType:表示返回值的类型
    • parameterType:表示参数类型
  3. UserMapper

    ```java
    

    public interface UserMapper {
    //获取用户列表
    List getUserList();
    //更加id查询用户
    User getUserById(int id);
    //增加一个用户
    int addUser(User user);
    //修改一个用户
    int updateUser(User user);
    //删除一个用户
    int deleteUser(int id);
    }
    ```

  4. UserMapper.xml

<?xml version="1.0" encoding="utf8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定对应的接口-->
<mapper namespace="com.wjq.dao.UserMapper">
<!--    select查询语句-->
    <select id="getUserList" resultType="com.wjq.pojo.User">
        select * from user;
    </select>

    <select id="getUserById" parameterType="int" resultType="com.wjq.pojo.User">
        select * from user where id = #{id};
    </select>

    <insert id="addUser" parameterType="com.wjq.pojo.User">
        insert into user (id,name,pwd) values (#{id}, #{name}, #{pwd});
    </insert>

    <update id="updateUser" parameterType="com.wjq.pojo.User" >
        update user set name = #{name}, pwd = #{pwd} where id = #{id};
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from user where id = #{id};
    </delete>
</mapper>
  1. UserMapperTest
public class UserMapperTest {
    @Test
    public void testUserLIst(){
        //获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //获取UserMapper对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        //关闭
        sqlSession.close();
    }

    @Test
    public void testUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int num = mapper.addUser(new User(4, "田七", "444444"));
        if (num > 0){
            System.out.println("ok");
        }
        //提交事务
        sqlSession.commit();
        sqlSession.close();

    }
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(4, "aa", "555555"));
         //提交事务
        sqlSession.commit();
        sqlSession.close();

    }
    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(4);
         //提交事务
        sqlSession.commit();
        sqlSession.close();

    }
}

万能的Map

当数据库中的表、字段、参数过多时,可以考虑使用map

  1. UserMapper

        int addUser2(Map<String, Object> map);
    
  2. UserMapper.xml

    <insert id="addUser2" parameterType="map">
        insert into user (id,name) values (#{userId}, #{userName});
    </insert>
    
  3. UserMapperTest

    @Test
    public void addUserMap(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map map = new HashMap<String, Object>();
        //需要什么添加什么
        map.put("userId", 6);
        map.put("userName", "map");
        mapper.addUser2(map);
        sqlSession.commit();
        sqlSession.close();
    }
    
  • Map传递参数时,直接从key中取值【parameterType=“map”】
  • 对象传递时,直接获取对象的属性【parameterType=“User”】
  • 当为一个基本数据类型时,可以省略【parameterType=“int”】
  • 当为多个参数时,用map或者注解

模糊查询

  1. UserMapper

    //模糊查询获取用户
    List<User> getUserLike(String value);
    
    List<User> getUserLike2(String value);
    
  2. UserMapper.xml

    <!--模糊查询获取用户-->
    <select id="getUserLike" parameterType="String" resultType="com.wjq.pojo.User">
        select * from user where name like #{value};
    </select>
    <!--直接在sql中使用拼接-->
    <select id="getUserLike2" parameterType="String" resultType="com.wjq.pojo.User">
        select * from user where name like "%"#{value}"%";
    </select>
    
  3. UserMapperTest

    @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);
        }
        System.out.println("---------------------------------");
        List<User> userLike2 = mapper.getUserLike2("张");
        for (User user : userLike2) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    

上一节—>简介、第一个Mybatis

如有不对的地方欢迎指出,共同进步!

上一篇:Spring学习——声明式事物


下一篇:MyBatisPlus学习笔记