从零开始学Mybatis(四)——Mybatis更新与删除

  • 更新用户
  1. 编写customer表的domain类
@Setter@Getter
public class Customer {
    private Integer cust_id;
    private String cust_name;
    private String cust_profession;
    private String cust_phone;
    private String email;

    @Override
    public String toString() {
        return "Customer{" +
                "cust_id=" + cust_id +
                ", cust_name='" + cust_name + '\'' +
                ", cust_profession='" + cust_profession + '\'' +
                ", cust_phone='" + cust_phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
  1. Mapping中添加更新用户的sql语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="myTest">
    <!--更新用户-->
    <update id="updateeCustomerById" parameterType="demo1.Customer">
        UPDATE customer SET cust_name=#{cust_name}where cust_id=#{cust_id}
    </update>
</mapper>
  1. 将Mapping配置到SqlMappingConfig中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <!--使用jdbc事务管理-->
            <transactionManager type="JDBC"/>
            <!--配置数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/development"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载映射文件-->
    <mappers>
        <mapper resource="demo1/Mapping"/>
    </mappers>
</configuration>
  1. 编写测试类
    // 更新用户
    @Test
    public void test4() throws IOException {
        // 1、创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //2、加载SqlMappingConfig配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
        //3、创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        //4、创建SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //5、设置对象参数
        Customer customer = new Customer();
        customer.setCust_name("赵老六");
        customer.setCust_id(12);
        //6、执行sql语句修改记录
        sqlSession.update("updateeCustomerById",customer);
        //7、提交事务,将数据写入到数据库中
        sqlSession.commit();
        System.out.println("修改数据成功");
        //8、关闭连接,释放资源
        sqlSession.close();
    }

运行结果

修改前数据

  • 删除用户
  1. 在Mapping中添加
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="myTest">
    <!--更新用户-->
    <update id="updateeCustomerById" parameterType="demo1.Customer">
        UPDATE customer SET cust_name=#{cust_name}where cust_id=#{cust_id}
    </update>
    <!--删除用户-->
    <delete id="deleteById" parameterType="Integer">
        DELETE FROM customer WHERE cust_id=#{cust_id}
    </delete>
</mapper>
  1. 编写测试类
// 删除用户
@Test
public void test5() throws IOException {
    // 1、创建SqlSessionFactoryBuilder对象
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    //2、加载SqlMappingConfig配置文件
    InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
    //3、创建SqlSessionFactory对象
    SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
    //4、创建SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //5、执行sql语句修改记录
    sqlSession.update("deleteById",12);
    //6、提交事务,将数据写入到数据库中
    sqlSession.commit();
    System.out.println("删除数据成功");
    //8、关闭连接,释放资源
    sqlSession.close();
}

运行结果

原始数据

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值