版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/83095000
依托上一篇博客的代码(上一篇博客点这里)
一、添加客户
1、在MyBatis的映射文件中添加操作是通过<insert>元素实现的,例如向t_customer表中插入一条数据可以通过如下配置来实现
<!-- 添加客户信息 -->
<insert id="addCustomer" parameterType="com.itheima.po.Customer">
insert into t_customer (username,jobs,phone)
values(#{usermname},#{jobs},#{phone})
</insert>
2、在测试类MybatisTest类中,编写如下测试方法:
@Test
public void addCustomerTest() throws Exception{
//1.读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.SqlSession执行映射文件中定义的sql,并返回映射结果
Customer customer = new Customer();
customer.setUsername("rose");
customer.setJobs("student");
customer.setPhone("13333533092");
int i=sqlSession.insert("com.itheima.mapper.CustomerMapper.addCustomer",customer);
//输出打印结果
if(i>0) {System.out.println("成功插入"+i+"条数据");}
else {
System.out.println("插入失败了");
}
//5.提交事务
sqlSession.commit();
//6.关闭SqlSession
sqlSession.close();
}
执行测试方法,控制台结果如下:
我们再来查询数据库
二、更新客户信息
1、在MyBatis的映射文件中添加操作是通过<update>元素实现的,例如向t_customer表中插入一条数据可以通过如下配置来实现
<!-- 更新客户信息 -->
<update id="updateCustomer" parameterType="com.itheima.po.Customer">
update t_customer set username=#{username},jobs=#{jobs},phone=#{phone}
where id=#{id}
</update>
2、在测试类MybatisTest类中,编写如下测试方法:
@Test
public void updateCustomerTest() throws Exception{
//1.读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.SqlSession执行映射文件中定义的sql,并返回映射结果
Customer customer = new Customer();
customer.setId(4);
customer.setUsername("rose");
customer.setJobs("programmer");
customer.setPhone("13311111111");
int i = sqlSession.update("com.itheima.mapper.CustomerMapper.updateCustomer",customer);
//输出打印结果
if(i>0) {System.out.println("成功修改"+i+"条数据");}
else {
System.out.println("修改失败了");
}
//5.提交事务
sqlSession.commit();
//6.关闭SqlSession
sqlSession.close();
执行测试方法,控制台结果如下:
再来看看mysql数据库
三、删除客户信息
1、在MyBatis的映射文件中添加操作是通过<delete>元素实现的,例如向t_customer表中插入一条数据可以通过如下配置来实现
<!-- 删除客户信息 -->
<delete id="deleteCustomer" parameterType="Integer">
delete from t_customer
where id=#{id}
</delete>
2、在测试类MybatisTest类中,编写如下测试方法:
@Test
public void deleteCustomerTest() throws Exception{
//1.读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.SqlSession执行映射文件中定义的sql,并返回映射结果
int i = sqlSession.delete("com.itheima.mapper.CustomerMapper.deleteCustomer",7);
//输出打印结果
if(i>0) {System.out.println("成功删除"+i+"条数据");}
else {
System.out.println("删除失败了");
}
//5.提交事务
sqlSession.commit();
//6.关闭SqlSession
sqlSession.close();
}
执行测试方法,控制台结果如下: