- 编写customer表的domain类
@Setter@Getter
public class Customer {
????private Integer cust_id;
????private String cust_name;
????private String cust_profession;
????private String cust_phone;
????private String email;
????@Override
????public String toString() {
????????return "Customer{" +
????????????????"cust_id=" + cust_id +
????????????????", cust_name='" + cust_name + '\'' +
????????????????", cust_profession='" + cust_profession + '\'' +
????????????????", cust_phone='" + cust_phone + '\'' +
????????????????", email='" + email + '\'' +
????????????????'}';
????}
}
- Mapping中添加更新用户的sql语句
<?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="myTest">
????<!--更新用户-->
????<update id="updateeCustomerById" parameterType="demo1.Customer">
????????UPDATE customer SET cust_name=#{cust_name}where cust_id=#{cust_id}
????</update>
</mapper>
- 将Mapping配置到SqlMappingConfig中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE?configuration?PUBLIC?"-//mybatis.org//DTD Config 3.0//EN"
????????"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
????<environments default="development">
????????<environment id="development">
????????????<!--使用jdbc事务管理-->
????????????<transactionManager type="JDBC"/>
????????????<!--配置数据库连接池-->
????????????<dataSource type="POOLED">
????????????????<property name="driver" value="com.mysql.jdbc.Driver"/>
????????????????<property name="url" value="jdbc:mysql://localhost:3306/development"/>
????????????????<property name="username" value="root"/>
????????????????<property name="password" value="123456"/>
????????????</dataSource>
????????</environment>
????</environments>
????<!--加载映射文件-->
????<mappers>
????????<mapper resource="demo1/Mapping"/>
????</mappers>
</configuration>
- 编写测试类
// 更新用户
@Test
public void test4() throws IOException {
????// 1、创建SqlSessionFactoryBuilder对象
????SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
????//2、加载SqlMappingConfig配置文件
????InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
????//3、创建SqlSessionFactory对象
????SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
????//4、创建SqlSession对象
????SqlSession sqlSession = sqlSessionFactory.openSession();
????//5、设置对象参数
????Customer customer = new Customer();
????customer.setCust_name("赵老六");
????customer.setCust_id(12);
????//6、执行sql语句修改记录
????sqlSession.update("updateeCustomerById",customer);
????//7、提交事务,将数据写入到数据库中
????sqlSession.commit();
????System.out.println("修改数据成功");
????//8、关闭连接,释放资源
????sqlSession.close();
}
运行结果
修改前数据
- 在Mapping中添加
<?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="myTest">
????<!--更新用户-->
????<update id="updateeCustomerById" parameterType="demo1.Customer">
????????UPDATE customer SET cust_name=#{cust_name}where cust_id=#{cust_id}
????</update>
????<!--删除用户-->
????<delete id="deleteById" parameterType="Integer">
????????DELETE FROM customer WHERE cust_id=#{cust_id}
????</delete>
</mapper>
- 编写测试类
// 删除用户
@Test
public void test5() throws IOException {
????// 1、创建SqlSessionFactoryBuilder对象
????SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
????//2、加载SqlMappingConfig配置文件
????InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
????//3、创建SqlSessionFactory对象
????SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
????//4、创建SqlSession对象
????SqlSession sqlSession = sqlSessionFactory.openSession();
????//5、执行sql语句修改记录
????sqlSession.update("deleteById",12);
????//6、提交事务,将数据写入到数据库中
????sqlSession.commit();
????System.out.println("删除数据成功");
????//8、关闭连接,释放资源
????sqlSession.close();
}
运行结果
原始数据
|