「Mybatis实战三」:Mybatis实现数据更新详析

一、前言

​ 本文将在 Mybatis初体验:一小时从入门到运行你的第一个应用 所构建的基础代码结构之上,并实现利用Mybatis进行数据更新的操作。

二、代码演示

1、修改映射文件UserMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="user">
    
    <select id="findAll" resultType="domain.User">
        select *
        from user
    select>

    
    
    <insert id="save" parameterType="domain.User">
        insert into user(username, birthday, sex, address)
        values (#{username}, #{birthday}, #{sex}, #{address})
    insert>

    
    <update id="update" parameterType="domain.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
    update>

mapper>

2、修改测试类

package test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import domain.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MybatisTest {

    @Test
    public void test() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");

        //获取SqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //获取SqlSession会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> list = sqlSession.selectList("user.findAll");
        for (User user : list) {
            System.out.println(user);
        }

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


    }

    @Test
    public void testInsert() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行更新
        User user = new User();
        user.setUsername("lily");
        user.setBirthday(new Date());
        user.setSex("女");
        user.setAddress("杭州");
        sqlSession.insert("user.save", user);

        //DML语句需要手动提交事务
        sqlSession.commit();

        sqlSession.close();

    }

    @Test
    public void testUpdate() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user = new User();
        user.setId(1);
        user.setUsername("钱三");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("深圳");
        //更新操作
        sqlSession.update("user.update", user);

        sqlSession.commit();

        sqlSession.close();

    }


}

3、测试结果

「Mybatis实战三」:Mybatis实现数据更新详析_第1张图片

总结

  • 修改语句使用update标签
  • 修改操作使用的API是sqlSession.update(“命名空间.id”,实体对象);

你可能感兴趣的:(mybatis,mybatis,java)