Use mybatis to add, delete, modify and check

0, pay attention

  • All additions, deletions and changes must be submitted to the transaction, otherwise the sql statement written will not take effect
  • Additions, deletions, and modifications have corresponding tags in the xml, don't use them as select tags (but I accidentally use the wrong one and it seems that the same can be executed? It is better to use the corresponding tags)
  • Use #{} to get the value in the sql statement
  • Note the note

1, insert (insert)

First define a method in the previously defined userDao interface, insert a user object as a parameter

    //插入一个用户
    void insertUser(User user);

Then "implement" this method in the bound userMapper.xml file, write SQL statements, and note that the parameter type here is User

    <insert id="insertUser" parameterType="com.wt.pojo.User">
        <!--若传入的参数是个对象,获取对象中的属性值直接通过#{属性名}就能得到-->
        insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd});
    </insert>

Finally to test

	@Test
    public void insertTest00(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        mapper.insertUser(new User(4,"Jeny","222222"));

        //注意,在进行增删改操作时必须要用到事务,如果这里不提交,那么代码运行没有问题但表并没有被增删改,需要提交事务后才能实现增删改
        sqlSession.commit();
        sqlSession.close();

    }

2. Update

Define methods in the interface

	//修改一个用户
    void updateUser(User user);

"Realize" this method through the bound mapper configuration file

    <update id="updateUser" parameterType="com.wt.pojo.User">
        <!--一样,对象中的属性值用#{}取-->
        update mybatis.user set pwd=#{pwd} where id=#{id};
    </update>

Retest

    @Test
    public void updateTest00(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        mapper.updateUser(new User(3,"Joe","123123"));

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

3. Delete (delete)

Define methods in the interface

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

"Realize" this method through the bound mapper configuration file

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

Retest

    @Test
    public void deleteTest00(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        mapper.deleteUser(3);

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

4. Query (select)

Query a user based on id, still
define methods in the interface

    //根据id查询用户
    User getUserById(int id);

"Realize" this method through the bound mapper configuration file

    <!--传入参数,要设置parameterType-->
    <select id="getUserById" parameterType="int" resultType="com.wt.pojo.User">
        <!--获取传入的参数用取值符号#{形参名}-->
        select * from mybatis.user where id=#{id};
    </select>

Retest

    @Test
    public void selectTest01(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        //传入参数
        User userById = mapper.getUserById(2);

        System.out.println(userById.getId());
        System.out.println(userById.getName());

        sqlSession.close();

    }

Fuzzy query:

    //模糊查询
    List<User> getUsers(String value);

Note the following string splicing

    <select id="getUsers" parameterType="String" resultType="com.wt.pojo.User">
    	<!--注意这里的字符串拼接-->
        select * from mybatis.user where name like "%"#{value}"%";
    </select>
    @Test
    public void selectTest02(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        List<User> users = mapper.getUsers("T");

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

        sqlSession.close();
    }

5. Afterword

Regarding incoming parameters, there is a map that can be used (a universal map), which can freely pass in parameters without passing in a whole object. This will be written later. Adding, deleting, modifying and checking can also be implemented with annotations. Although it can make the code more concise, it is not good for complex SQL statements. Personally, I prefer to use configuration files.

Guess you like

Origin blog.csdn.net/kitahiragawa/article/details/113035741