四,Mybatis进行数据写入

一,Mybatis写操作包含三种:

  • 插入

  • 更新

  • 删除

二,数据库事务:

客户端向mysql发起连接操作后,如果遇到写操作,并不是直接写入到某个表中,而是需要先被事务日志记录(类似流水账),然后当客户端向mysql发起commit命令的时候,事务日志才会将数据同时写入到数据表中,commit后才是真正的写入。当数据被成功写入数据表中,事务日志会被清空。如果客户端处理数据时候有些数据未执行成功,客户端会发起rollback回滚命令,当mysql收到回滚命令后,当前事务日志中所有已经产生的数据都会被清除,这些数据就不会被放入数据表中,只有当所有数据都完成,再由客户端发起提交请求,我们的数据才会被成功写入。
在这里插入图片描述
三,Mybatis进行数据新增

新增操作比起更新和删除操作,有一点比较复杂,新增完数据后,因为大多数情况下,id是自动生成的,新增完数据后,我们希望原始的数据对象得到新插入数据的编号,我们需要在insert语句中,增加selectkey子标签,在标签中指定select last_insert_id(),获取当前数据库连接最后产生的id号,并重新赋值给goods_id

配置文件

<?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="goods">
    <insert id="insert" parameterType="com.imooc.mybatis.entity.Goods">
        insert into t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)
        values (#{title},#{subTitle},#{originalCost},#{currentPrice},#{discount},#{isFreeDelivery},#{categoryId})
        <!--order:执行顺序,设置成after表示在上面语句执行后操作。resultType:设置的函数的返回值类型,keyProperty:要设置的
         上面语句执行完毕后,执行下面的sql语句,将得到的值回添到goodsid属性中-->
        <selectKey resultType="integer" keyProperty="id" order="AFTER">
           <!-- 获取当前连接最后产生的id号,并自动填充给goods里面的id主键属性-->
            select last_insert_id()
        </selectKey>
    </insert>
</mapper>

代码

@Test
public void testSelect() {
    SqlSession sqlSession = null;
    try {
        sqlSession = MyBatisUtils.openSession();
        Goods goods = new Goods();
        goods.setTitle("测试商品");
        goods.setSubTitle("测试子商品");
        goods.setCurrentPrice(30.2f);
        goods.setDiscount(2f);
        goods.setIsFreeDelivery(1);
        goods.setOriginalCost(300f);
        goods.setCategoryId(12);
        // insert方法返回插入成功的数量
        int num = sqlSession.insert("goods.insert", goods);
        System.out.println(goods.getTitle());
        // 提交
        sqlSession.commit();
    } catch (Exception e) {
        // 回滚
        if (sqlSession != null) {
            sqlSession.rollback();
        }
        throw e;
    } finally {
        MyBatisUtils.closeSqlSession(sqlSession);
    }
}

四,selectKey与useGeneratedKeys

上面我们提到插入数据的时候,如果需要获取自增主键,需要使用selectKey进行获取。我们除了可以使用这种方式,还可以使用useGeneratedKeys的方式进行主键的获取,方式如下:

 <!--keyProperty:在实体类里的id名称,keyColumn:表里的id名称,useGeneratedKeys:是否自动生成主键,默认是否-->
<insert id="insert" parameterType="com.imooc.mybatis.entity.Goods" useGeneratedKeys="true"
        keyProperty="id" keyColumn="goods_id">
    insert into t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)
    values (#{title},#{subTitle},#{originalCost},#{currentPrice},#{discount},#{isFreeDelivery},#{categoryId})
</insert>

两者书写方式对比:
在这里插入图片描述
在这里插入图片描述
selectKey与useGeneratedKeys的区别:

  1. 显示与隐式:
    selectKey标签需要明确编写获取最新主键的sql语句:(select last_insert_id()
    useGeneratedKeys属性会自动根据驱动生成对应的sql语句,会随着数据库的变化自动变化
  2. 应用场景不同:
    seleKey适用于所有的关系型数据库
    useGeneratedKeys只支持自增主键类型的数据库(支持的数据库比如mysql, sqlserver,不支持的数据库为Oracle等)
    总结:
    selectKey标签是通用方案,适用于所有数据库,但编写麻烦(因为每种数据库获取最新主键的方式都不一致),适用于复杂的应用场景,比如大公司的项目有多种数据库对数据进行支撑和保存
    useGeneratedKeys只支持自增主键类型的数据库,使用简单(绝大多数场景适用,推荐)
    在这里插入图片描述

五,更新和删除操作

  1. 更新:
    更新是对已有数据进行处理,不建议通过手动set来组织原始数据,推荐先获取原 有数据,再对原有数据进行更新操作,保证数据的影响是最小的
    配置如下:
<update id="update" parameterType="com.imooc.mybatis.entity.Goods">
    update t_goods set title= #{title} where goods_id= #{id};
</update>
  @Test
    public void testUpdate() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById", 2680);
            goods.setTitle("更新商品");
            int num = sqlSession.update("goods.update", goods);
            sqlSession.commit();
            System.out.println(goods.getTitle());
            System.out.println(goods.getSubTitle());
        } catch (Exception e) {
            if (sqlSession != null) {
                sqlSession.rollback();
            }
        } finally {
            sqlSession.close();
        }
    }
  1. 删除:
    因为大多数删除操作是根据主键来进行的,因此在xml文件配置的时候,parameterType只需要声明主键类型是int即可。
    配置:
<delete id="delete" parameterType="int">
    delete from t_goods where goods_id=#{id}
</delete>
  @Test
    public void testDelete() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.openSession();
            int num = sqlSession.delete("goods.delete", 2680);
            sqlSession.commit();
        } catch (Exception e) {
            if (sqlSession != null) {
                sqlSession.rollback();
            }
        } finally {
            sqlSession.close();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 配置数据库连接信息 在application.properties文件中添加以下配置信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 2. 添加MyBatis依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> ``` 3. 创建数据表 创建一个名为user的数据表,包含id、name和age三个字段。 4. 创建实体类 创建一个User实体类,包含id、name和age三个属性,同时添加对应的getters和setters方法。 5. 创建Mapper层 创建一个UserMapper接口,定义一个insert方法,用于向数据库中插入User对象。 ``` @Mapper public interface UserMapper { @Insert("INSERT INTO user(name, age) VALUES (#{name}, #{age})") int insert(User user); } ``` 6. 创建Service层 创建一个UserService接口,定义一个addUser方法,用于向数据库中插入User对象。 ``` @Service public interface UserService { int addUser(User user); } ``` 在UserService接口的实现类UserServiceImpl中注入UserMapper,并实现addUser方法。 ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int addUser(User user) { return userMapper.insert(user); } } ``` 7. 创建Controller层 创建一个UserController类,注入UserService,并添加addUser方法,用于接收前端传递的User对象,并调用UserService的addUser方法插入到数据库中。 ``` @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/add") public int addUser(@RequestBody User user) { return userService.addUser(user); } } ``` 至此,使用SpringBoot MyBatis数据库中写入数据的整个流程完成。可以通过Postman等工具测试接口,向数据库中插入数据

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值