MyBatis-保存更新删除

添加客户

修改 CustomerMapper.xml 添加内容如下。

<insert id="saveCustomer" parameterType="top.it6666.domain.Customer">
    INSERT INTO customer (cust_name, cust_profession, cust_phone, email)
    VALUES (#{cust_name}, #{cust_profession}, #{cust_phone}, #{email})
</insert>

修改测试类代码,如下,主要就是把之前的查询改为了插入。

/**
 * @author BNTang
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        // 1.创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 2.加载 SqlMapConfig.xml 配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMappingConfig.xml");

        // 3.创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        // 4.创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 5.执行 SqlSession 对象执行查询
        Customer customer = new Customer();
        customer.setCust_name("BNTang");
        customer.setCust_phone("18819522017");
        customer.setCust_profession("刺客");
        customer.setEmail("303158131@qq.com");

        sqlSession.insert("saveCustomer", customer);

        // 6.提交事务
        sqlSession.commit();

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

}

返回添加过后自增的主键

修改 CustomerMapper.xml 添加内容如下,需要注意 resultType 的类型需要和实体类的一致。

<insert id="saveCustomer" parameterType="top.it6666.domain.Customer">
    <selectKey keyColumn="cust_id" keyProperty="cust_id" order="AFTER" resultType="Integer">
        SELECT LAST_INSERT_ID()
    </selectKey>
    INSERT INTO customer (cust_name, cust_profession, cust_phone, email)
    VALUES (#{cust_name}, #{cust_profession}, #{cust_phone}, #{email})
</insert>

修改测试类代码,如下,主要就是打印一下自增之后的主键值内容如下。

/**
 * @author BNTang
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        // 1.创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 2.加载 SqlMapConfig.xml 配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMappingConfig.xml");

        // 3.创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        // 4.创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 5.执行 SqlSession 对象执行查询
        Customer customer = new Customer();
        customer.setCust_name("BNTang");
        customer.setCust_phone("18819522017");
        customer.setCust_profession("刺客");
        customer.setEmail("303158131@qq.com");

        sqlSession.insert("saveCustomer", customer);

        System.out.println(customer.getCust_id());

        // 6.提交事务
        sqlSession.commit();

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

}

更新客户

修改 CustomerMapper.xml 添加内容如下。

<update id="updateCustomerById" parameterType="top.it6666.domain.Customer">
    UPDATE `customer`
    SET cust_name = #{cust_name}
    WHERE cust_id = #{cust_id}
</update>

修改测试类代码,如下。

/**
 * @author BNTang
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        // 1.创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 2.加载 SqlMapConfig.xml 配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMappingConfig.xml");

        // 3.创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        // 4.创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 5.执行 SqlSession 对象执行查询
        Customer customer = new Customer();
        customer.setCust_name("newBNTang");
        customer.setCust_id(14);

        sqlSession.update("updateCustomerById", customer);

        // 6.提交事务
        sqlSession.commit();

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

}

删除客户

修改 CustomerMapper.xml 添加内容如下。

<delete id="deleteCustomerById" parameterType="Integer">
    DELETE
    FROM customer
    WHERE cust_id = #{cust_id}
</delete>

修改测试类代码,如下。

/**
 * @author BNTang
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        // 1.创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 2.加载 SqlMapConfig.xml 配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMappingConfig.xml");

        // 3.创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        // 4.创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 5.执行 SqlSession 对象执行查询
        sqlSession.delete("deleteCustomerById", 14);

        // 6.提交事务
        sqlSession.commit();

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

}
posted @   BNTang  阅读(243)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示