Mybatis映射文件概述与增删改查操作

目录

 MyBatis的映射文件概述

 Mybatis增删改查操作

Mybatis的增加/插入操作

插入操作注意的问题

MyBatis的修改数据操作

Mybatis 删除数据操作

 删除操作注意的问题


 MyBatis的映射文件概述

 Mybatis增删改查操作

Mybatis的增加/插入操作

mybatisTest下

    @Test
    public void test2() throws IOException {
        //模拟user对象
        User user=new User();
        user.setUsername("ZengHui");
        user.setPassword("1234");

        //获得核心配置文件
        InputStream resourceAsFile = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsFile);
        //获得session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
        int result= sqlSession.insert("userMapper.insertUser",user);
        //mybatis默认不自动提交事务,提交后才能持久化到数据库中
        sqlSession.commit();
        System.out.println(result);
        //释放资源
        sqlSession.close();

    }

 UserMapper.xml下

  <!--插入操作-->
    <insert id="insertUser" parameterType="com_mybatis.pojo.User">
        insert into user values (#{id},#{username},#{password})
    </insert>

 运行结果

插入操作注意的问题

  • 插入语句使用insert标签
  • 在映射文件中时而用parameterType属性指定要插入的数据类型
  • Sq语句中使用#{实体属性名}方式引用实体中的属性值
  • 插入操作使用的ApI是sqlSession.insert(“命名空间.id,实体对象”);
  • 插入操作设计数据库变化,所以要使用sqlSession对象显示的提交事务,即sqlSession,commit()

MyBatis的修改数据操作

在UserMapper.xml文件下

<!--   修改操作-->
<update id="update" parameterType="com_mybatis.pojo.User">
    update user  set username=#{username},password=#{password} where id=#{id}
</update>

MybatisTest类下


    @Test
    //修改操作
    public void test3() throws IOException {
        //模拟user对象
        User user=new User();
        user.setId(3);
        user.setUsername("ZhaoLiu");
        user.setPassword("12345");

        //获得核心配置文件
        InputStream resourceAsFile = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsFile);
        //获得session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
     sqlSession.update("userMapper.update", user);
        //mybatis默认不自动提交事务,提交后才能持久化到数据库中
        sqlSession.commit();

        //释放资源
        sqlSession.close();

    }

运行之后,数据库中变化

Mybatis 删除数据操作

userMapper.xml

<!--删除操作-->
    <delete id="delete" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>

  mybatisTest类下

    @Test
    //删除
    public void test4() throws IOException {

        //获得核心配置文件
        InputStream resourceAsFile = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsFile);
        //获得session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
        sqlSession.delete("userMapper.delete", 3);
        //mybatis默认不自动提交事务,提交后才能持久化到数据库中
        sqlSession.commit();

        //释放资源
        sqlSession.close();

    }

运行结果

 删除操作注意的问题

删除语句使用delete标签

Sql语句中使你#{任意字符串}方式引用传递的单个参数

删除操作使用的API是sqlSession.delete("命名空间.id",Object)

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执久呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值