Mybatis【CRUD】

3.CRUD

1.namespace

The package name in the namespace should be consistent with the package name of the Dao/mapper interface

2.select

select, query statement;

id: is the method name in the corresponding namespace

resultType: the return value of the Sql statement execution

1. Write the interface

  //查询全部用户
    List<User> getUserList();
    //根据ID查询用户
    User getUserById(int id);

2. Write the sql statement in the corresponding Mapper

<select id="getUserList" resultType="com.jiang.pojo.User">
       select * from mybatis.user
   </select>

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

3. Test

 @Test
    public void test(){
        //第一步:获得SqlSession对象
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();
        //执行SQL
        //方式一:getMapper
        UserMapper userDao = sqlSesssion.getMapper(UserMapper.class);
        List<User> userList = userDao.getUserList();

    /*
      //方式二:
        List<User> userList = sqlSesssion.selectList("com.jiang.dao.UserDao.getUserList");
*/
        for (User user:userList) {
            System.out.println(user);
        }

        //关闭SqlSession
        sqlSesssion.close();
    }

 @Test
    public void getUserById(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();

        UserMapper mapper = sqlSesssion.getMapper(UserMapper.class);

        User user = mapper.getUserById(1);
        System.out.println(user);
        sqlSesssion.close();
    }

3.Insert

1. Write the interface

 //insert一个用户
    int addUser(User user);

2. Write SQL statements

parameterType: input attribute ----- basic type int can be ignored

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

3. Test

//增删改需要加事物
    @Test
    public void addUser(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();

        UserMapper mapper = sqlSesssion.getMapper(UserMapper.class);

       int res= mapper.addUser(new User(4,"王五","123456"));
       if(res>0){
           System.out.println("插入成功");
       }

       //提交事务
        sqlSesssion.commit();
        sqlSesssion.close();
        
    }

4.update

1. Write the interface

  //修改用户
    int updateUser(User user);

2. Write SQL statements

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

3. Test

 @Test
    public void updateUser(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();
        UserMapper mapper = sqlSesssion.getMapper(UserMapper.class);

        int res=mapper.updateUser(new User(4,"田七","123"));
        if(res>0){
            System.out.println("更新数据成功");
        }
        sqlSesssion.commit();
        sqlSesssion.close();
    }

5.delete

1. Write the interface

 //删除一个用户
    int deleteUser(int id);

2. Write SQL statements

  <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>

3. Test

@Test
    public void deleteUser(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();
        UserMapper mapper = sqlSesssion.getMapper(UserMapper.class);
        int res=mapper.deleteUser(4);
        if(res>0){
            System.out.println("删除用户成功");
        }
        sqlSesssion.commit();
        sqlSesssion.close();
    }

Notice:

Addition, deletion and modification operations need to commit transactions

6. Analyzing errors

Do not match tags

resource binding mapper, you need to use the path

The program configuration file must conform to the specification

NullPointException, not registered to the resource

There are Chinese garbled characters in the output XML file

Maven resource not exported issue

6. Universal Map

1. UserMapper interface

//用万能Map插入用户
public void addUser2(Map<String,Object> map);

2.UserMapper.xml

<!--对象中的属性可以直接取出来 传递map的key-->
<insert id="addUser2" parameterType="map">
    insert into user (id,name,password) values (#{userid},#{username},#{userpassword})
</insert>

3. Test

    @Test
    public void test3(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userid",4);
        map.put("username","酱油11");
        map.put("userpassword",123456);
        mapper.addUser2(map);
        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
    }

Map passes parameters, just take out the key directly in sql! 【parameter="map"】

The object passes parameters, just take out the properties of the object in sql! 【parameter="Object"】

When there is only one basic type parameter, it can be obtained directly in sql

Multiple parameters use Map,  or annotation!

8. Thinking questions

 How to write fuzzy query?

1. When the Java code is executed, pass the wildcard %%

List<User> userList = mapper.getUserLike("%李%");

2. Use wildcards in sql splicing (pay attention to prevent SQL injection)

select * from user where name like "%"#{value}"%"

https://www.bilibili.com/video/BV1NE411Q7Nx?p=5

Guess you like

Origin blog.csdn.net/qq_48108092/article/details/124127289