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

MyBatis-保存更新删除

<insert >
    INSERT INTO customer (cust_name, cust_profession, cust_phone, email)
    VALUES (#{cust_name}, #{cust_profession}, #{cust_phone}, #{email})
</insert>

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

MyBatis-保存更新删除

/**
 * @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 的类型需要和实体类的一致。

MyBatis-保存更新删除

<insert >
    <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 添加内容如下。

MyBatis-保存更新删除

<update >
    UPDATE `customer`
    SET cust_name = #{cust_name}
    WHERE cust_id = #{cust_id}
</update>

修改测试类代码,如下。

MyBatis-保存更新删除

/**
 * @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 添加内容如下。

MyBatis-保存更新删除

<delete >
    DELETE
    FROM customer
    WHERE cust_id = #{cust_id}
</delete>

修改测试类代码,如下。

MyBatis-保存更新删除

/**
 * @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();
    }

}

相关文章:

  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2021-06-29
  • 2021-09-25
  • 2022-01-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2021-09-16
  • 2021-12-17
  • 2022-01-17
  • 2022-12-23
相关资源
相似解决方案