VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • mybatis(CRUD)

3、mybatis(CRUD)

有了mybatis,我们要对数据库进行增删改查只需要操作接口和mapper.xml文件,然后进行测试就可以了。

实例代码如下:

接口

public interface UserMapper {
    List<User> getUserList();
    //查
    User getUserById(int id);
    //增
    int addUser(User user);
    //改
    int updateUser(User user);
    //删
    int deleteUser(int id);
}

mapper.xml

<mapper namespace="com.kuang.dao.UserMapper">
    <!--查-->
    <select id="getUserList" resultType="com.kuang.pojo.User">
        select * from mybatis.user
    </select>
    <!--查-->
    <select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
        select * from mybatis.user where id=#{id}
    </select>
    <!--增-->
    <insert id="addUser" parameterType="com.kuang.pojo.User">
        insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd});
    </insert>
    <!--改-->
    <update id="updateUser" parameterType="com.kuang.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>
    <!--删-->
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>
</mapper>

注意:namespace要写接口,因为我们相当于实现了这个接口,而id后要写接口中的方法名,resultType就是返回的结果类型,paramterType就是传入的参数类型

测试

public class UserDaoTest {
    //查
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userDao = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userDao.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    //查
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User userById = userMapper.getUserById(1);
        System.out.println(userById);
        sqlSession.close();
    }
    //增(增删改需要提交事务)
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.addUser(new User(4,"赵六","123156"));
        sqlSession.commit();
        sqlSession.close();
    }
    //改(增删改需要提交事务)
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.updateUser(new User(4,"张jiu","987654"));
        sqlSession.commit();
        sqlSession.close();
    }
    //删(增删改需要提交事务)
    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.deleteUser(4);
        sqlSession.commit();
        sqlSession.close();
    }
}

注意:

  1. 增删改需要提交事务
  2. 记得关闭sqlSession
  3. 参数是对象的话我们要new一个对象传参
树越是向往高处的光亮,它的根就越要向下,向泥土向黑暗的深处。


原文:https://www.cnblogs.com/H-scholar/p/15783277.html

 

相关教程