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 @ 2021-02-20 15:10  BNTang  阅读(235)  评论(0编辑  收藏  举报