网站html有了怎么建设网站,书香气的域名做网站,wordpress自适应手机修改,wordpress站点地址灰显保存更新删除
目录
插入操作获取插入的最后一个id更新操作删除操作插入操作
映射文件 Customer.xml #xff1a; 插入数据的标签为 insert#xff0c;与查询 select 区分开来。 parameterType 是输入参数类型#xff0c;这里指定为 Customer 对象#xff0c;即需要传入一…保存更新删除
目录
插入操作获取插入的最后一个id更新操作删除操作插入操作
映射文件 Customer.xml 插入数据的标签为 insert与查询 select 区分开来。 parameterType 是输入参数类型这里指定为 Customer 对象即需要传入一个 Customer 类型的参数。由于传入参数不是单个简单类型值#{} 中的名字必须与类的属性对应。
映射文件中的代码如下
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacemyTest!--添加客户--insert idinsertCustom parameterTypecom.itlike.domain.CustomerINSERT INTO customer (cust_id, cust_name, cust_profession, cust_phone, email)VALUES (#{cust_id}, #{cust_name}, #{cust_profession}, #{cust_phone}, #{email});/insert/mapper测试类 MyTest.java 查询的时候不需要提交事务但是当要更改数据库当中的记录时执行 sql 时需要手动提交事务sqlSession.commit();
测试类中的代码如下
public class MyTest {/*添加客户*/Testpublic void test4(){SqlSession sqlSession MybatisUtils.openSession();Customer customer new Customer();customer.setCust_name(后裔);customer.setCust_phone(18937485936);sqlSession.insert(insertCustom, customer);// 当要更改数据库当中的记录时执行 sql 时要自己提交事务// 手动提交事务sqlSession.commit();sqlSession.close();}}运行效果数据库中成功插入数据。
Opening JDBC Connection
Created connection 1615056168.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection6043cd28]Preparing: INSERT INTO customer (cust_id, cust_name, cust_profession, cust_phone, email) VALUES (?, ?, ?, ?, ?); Parameters: null, 后裔(String), null, 18937485936(String), nullUpdates: 1获取插入的最后一个id
!--添加客户--
insert idinsertCustom parameterTypecom.itlike.domain.Customer/*获取插入的最后一个id*/selectKey keyColumncust_id keyPropertycust_id resultTypeInteger orderAFTERSELECT last_insert_id()/selectKeyINSERT INTO customer (cust_id, cust_name, cust_profession, cust_phone, email)VALUES (#{cust_id}, #{cust_name}, #{cust_profession}, #{cust_phone}, #{email});
/insert参数说明
keyColunm指定要获取的最后一个属性。keyProperty将获取的属性赋给插入对象的哪个属性。resultType获取的属性的类型。orderBEFORE为在sql执行之前获取属性AFTER 为在sql执行之后获取属性。
public void test4(){SqlSession sqlSession MybatisUtils.openSession();Customer customer new Customer();customer.setCust_name(后裔6);customer.setCust_phone(18937485936);sqlSession.insert(insertCustom, customer);// 当要更改数据库当中的记录时执行 sql 时要自己提交事务// 手动提交事务sqlSession.commit();System.out.println(customer);sqlSession.close();
}运行结果省略大部分日志留下打印结果插入后可以获取插入的最后一个ID。
Customer{cust_id20, cust_name后裔6, cust_professionnull, cust_phone18937485936, emailnull}注为什么需要 keyProperty为什么获取了ID后还要将它赋给插入对象 因为如果不设定ID插入对象的话实际上插入对象的ID一般是靠自动生成的在插入完后获取时获取的为 null如下是不写 keyProperty 的后果
更新操作
映射文件 Customer.xml 更新数据的标签为 update; 代码如下
!--更新--
update idupdateCustomer parameterTypecom.itlike.domain.CustomerUPDATE customer SET cust_name #{cust_name} WHERE cust_id #{cust_id}
/update测试类 MyTest.java更新操作需要传入一个对象这个对象可以根据 id 查出来也可以自己创建。如果是自己创建的对象必须要有 id 属性。
/*更新操作*/
Test
public void update(){SqlSession sqlSession MybatisUtils.openSession();Customer customer sqlSession.selectOne(queryCustomerById,11); // 根据id查出来一个对象customer.setCust_name(孙悟空); // 修改名字sqlSession.update(updateCustomer, customer);sqlSession.commit();sqlSession.close();
}运行效果成功更新id为11的客户。 Preparing: SELECT * FROM customer WHERE cust_id ? Parameters: 11(Integer)Columns: cust_id, cust_name, cust_profession, cust_phone, emailRow: 11, 李信, 战士, 13728964922, lixinqq.comTotal: 1Preparing: UPDATE customer SET cust_name ? WHERE cust_id ? Parameters: 孙悟空(String), 11(Integer)Updates: 1删除操作
映射文件 Customer.xml 删除数据的标签为 delete 代码如下
!--删除--
delete iddeleteCustomer parameterTypecom.itlike.domain.CustomerDELETE FROM customer WHERE cust_id #{cust_id}
/delete测试类 MyTest.java删除操作需要传入一个对象。
/*删除操作*/
Test
public void delete(){SqlSession sqlSession MybatisUtils.openSession();Customer customer sqlSession.selectOne(queryCustomerById, 10);sqlSession.delete(deleteCustomer, customer);sqlSession.commit();sqlSession.close();
}运行效果成功删除id为10的客户。 Preparing: SELECT * FROM customer WHERE cust_id ? Parameters: 10(Integer)Columns: cust_id, cust_name, cust_profession, cust_phone, emailRow: 10, 阿木木, 辅助, 13398908928, 13398908928qq.comTotal: 1Preparing: DELETE FROM customer WHERE cust_id ? Parameters: 10(Integer)Updates: 1