MyBatis的新增和修改命令
阿新 • • 發佈:2022-05-10
-
1.編寫介面方法:Mapper介面
在BrandMapper類新增方法
/** * 新增 */ void add(Brand brand);
引數:除了id之外的所有資料
結果:void
2.編寫SQL語句:SQL對映檔案
在BrandMapper.xml類新增
<insert id="add"> insert into tb_brand (brand_name,company_name,ordered,description,status) values (#{brandName},#{companyName},#{ordered},#{description},#{status});
3.執行方法,測試
Add方法測試
@Test public void testAdd() throws IOException { //接收引數 int status = 1; String companyName = "波導手機"; String brandName = "波導"; String description = "手機中的戰鬥機"; int ordered = 100; //封裝物件 Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName); brand.setDescription(description); brand.setOrdered(ordered);
MyBatis事務:
openSession():預設開啟事務,進行增刪改操作後需要使用sqlSession.commit();手動提交事務
openSession(true):可以設定為自動提交事務(關閉事務)
新增-主鍵返回
在資料新增成功後,需要獲取插入資料庫資料的主鍵
返回新增資料的主鍵
對映檔案
<insert id="add" useGeneratedKeys="true" keyProperty="id">
比如:新增訂單和訂單項
1.新增訂單
2.新增訂單項,訂單項中需要設定所屬訂單的id
測試類
@Test public void testAdd2() throws IOException { //接收引數 int status = 1; String companyName = "波導手機"; String brandName = "波導"; String description = "手機中的戰鬥機"; int ordered = 100; //封裝物件 Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName); brand.setDescription(description); brand.setOrdered(ordered); //1.獲取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.獲取SqlSession物件 //SqlSession sqlSession = sqlSessionFactory.openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(true); //3.獲取Mapper介面的代理物件 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); //4.執行方法 brandMapper.add(brand); Integer id = brand.getId(); System.out.println(id); //提交事務 sqlSession.commit(); //5.釋放資源 sqlSession.close(); }
-
修改
-
修改全部自動
1.編寫介面方法Mapper介面
引數:所有資料
結果:void
BrandMapper類
/** * 修改 */ int update(Brand brand);
2.編寫SQL語句:SQL對映檔案
BrandMapper.xml檔案
<update id="update"> update tb_brand set brand_name = #{brandName}, company_name = #{companyName}, ordered = #{ordered}, description = #{description}, status = #{status} where id = #{id}; </update>
3.執行方法測試
測試類
@Test public void testUpdate() throws IOException { //接收引數 int status = 1; String companyName = "波導手機"; String brandName = "波導"; String description = "波導手機,手機中的戰鬥機"; int ordered = 200; int id = 5; //封裝物件 Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName); brand.setDescription(description); brand.setOrdered(ordered); brand.setId(id); //1.獲取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.獲取SqlSession物件 //SqlSession sqlSession = sqlSessionFactory.openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(true); //3.獲取Mapper介面的代理物件 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); //4.執行方法 int count = brandMapper.update(brand); System.out.println(count); //提交事務 sqlSession.commit(); //5.釋放資源 sqlSession.close(); }
-
修改動態欄位
1.編寫介面方法:Mapper介面
引數:部分資料,封裝到物件中
結果:void
跟BrandMapper類一樣沒變化。
2.編寫SQL語句:SQL對映檔案
改變BrandMapper.xml的SQL語句
<update id="update"> update tb_brand <set> <if test="brandName != null and brandName != ''"> brand_name = #{brandName}, </if> <if test="companyName != null and companyName != '' "> company_name = #{companyName}, </if> <if test="ordered != null"> ordered = #{ordered}, </if> <if test="description != null and description != ''"> description = #{description}, </if> <if test="status != null"> status = #{status} </if> </set> where id = #{id}; </update>
3.執行方法,測試
測試類
@Test public void testUpdate() throws IOException { //接收引數 int status = 0; String companyName = "波導手機"; String brandName = "波導"; String description = "波導手機,手機中的戰鬥機"; int ordered = 200; int id = 6; //封裝物件 Brand brand = new Brand(); brand.setStatus(status); // brand.setCompanyName(companyName); // brand.setBrandName(brandName); // brand.setDescription(description); // brand.setOrdered(ordered); brand.setId(id); //1.獲取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.獲取SqlSession物件 //SqlSession sqlSession = sqlSessionFactory.openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(true); //3.獲取Mapper介面的代理物件 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); //4.執行方法 int count = brandMapper.update(brand); System.out.println(count); //提交事務 sqlSession.commit(); //5.釋放資源 sqlSession.close(); }
-