美文网首页
MyBatis 入坑(二)

MyBatis 入坑(二)

作者: jihe | 来源:发表于2018-01-21 13:40 被阅读0次

    概述

    在跑通前面的小程序之后,在前后梳理一遍,熟能生巧,下面继续学习其他起语句的执行。大家要是有接触过 Hibernate 的话,学了 MyBatis 后会觉得相对来说比较简单一点。也容易理解,记忆负担相对少点。当然最好的方式就是多练,不多说,就是干。

    删除

    用过用户 id 删除一个用户

    这里实现是比较简单的,在 Users.xml 文件中写删除语句:

    <!--用过 id 删除用户-->
    <select id="deleteById" parameterType="int">
        delete from users where id=#{id}
    </select>
    

    在 UserTest 中添加测试方法:

    //测试通过用户 id 删除用户
    @Test
    public void testDelteById(){
        SqlSession session = null;
         try {
             session = sqlSessionFactory.openSession();
             session.delete("com.jihe.domain.deleteById",35);
             session.commit();
         }catch (Exception e){
             e.printStackTrace();
         }finally {
             if(session != null){
                 session.close();
             }
        }
    }
    

    以上就是使用 Mybatis 实现简单的删除操作。

    修改

    通过用户 id 修改用户
    和删除一样,这里直接写更新语句:

    <!--通过 id 修改用户信息-->
    <update id="updateUser" parameterType="com.jihe.domain.Users">
        update users set username=#{username} where id=#{id}
    </update>
    

    传入一个要修改的用户,将修改信息设置在传入的对象中,在取值做修改,然后写测试程序:

    //测试通过 id 更新用户信息
     @Test
    public void testUpdateUser(){
            SqlSession session = null;
            try{
                session = sqlSessionFactory.openSession();
                Users user = new Users();
                user.setId(34);
                user.setUsername("波杰克");
                session.update("com.jihe.domain.updateUser",user);
                //容易忽略提交,事务会自动开启,但是要手动提交
                session.commit();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(session != null){
                    session.close();
                }
            }
    }
    

    做完更新后要记得提交事务。MyBatis 会默认开启事务,但是需要手动提交。
    以上就是 MyBatis 实现简单的更新操作。

    查询

    通过 id 查询对应的用户

    <!--通过 id 查询相应的用户-->
    <select id="fingUserById" parameterType="int" resultType="com.jihe.domain.Users">
        select * from users where id=#{id}
    </select>
    

    resultType 定义返回结果类型
    parameterType 定义输入参数类型

    //通过 id 查找用户
    @Test
    public void testFindUserById(){
        SqlSession session = null;
        try{
            session = sqlSessionFactory.openSession();
             Users user = session.selectOne("com.jihe.domain.fingUserById", 34);
             System.out.print(user);
        }catch (Exception e){
             e.printStackTrace();
        }finally {
             if(session != null){
                 session.close();
             }
        }
    }
    

    selectOne 查找一条记录

    模糊查询

    根据用户名模糊查询

    <!--通过用户名模糊查询-->
    <select id="fingByUsername" parameterType="string" resultType="com.jihe.domain.Users">
        select * from users where username like '%${value}%'
    </select>
    
    //通过名称模糊查询
    public void testFindByUsername(){
        SqlSession session = null;
        try{
            session = sqlSessionFactory.openSession();
            List<Users> list = session.selectList("com.jihe.domain.findByUsername", "波");
            System.out.print(list);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(session != null){
                session.close();
            }
        }
    }
    
    • selectList 查找多条记录
    • #{} 相当于一个占位符,可以在 preparedStatement 预编译 SQL 语句时,将值赋给语句中的占位符,这样可以有效防止 SQL 注入。在传入值时,会给值默认加引号。
    • ${} 在执行预编译之前,也就是在动态 SQL 解析时已经将值付给了 SQL 语句,这里会存在 SQL 注入问题。

    小结

    1. MyBatis 将 SQL 语句单独写在映射文件中,提高了程序的可维护性。
    2. MyBatis 将数据可记录直接映射成了 Java 对象,很大程度的方便了解析。

    下面一篇就是很帅的 Mapper 动态代理的方式实现持久层的开发。

    相关文章

      网友评论

          本文标题:MyBatis 入坑(二)

          本文链接:https://www.haomeiwen.com/subject/zwypoxtx.html