欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis實現(xiàn)CRUD的示例代碼

 更新時間:2024年12月08日 09:57:30   作者:耀耀_很無聊  
本文主要介紹了MyBatis實現(xiàn)CRUD的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

準備工作

創(chuàng)建module(Maven的普通Java模塊):mybatis-002-crud

pom.xml

  • 打包方式j(luò)ar
  • 依賴:
    • mybatis依賴
    • mysql驅(qū)動依賴
    • junit依賴
    • logback依賴
  • mybatis-config.xml放在類的根路徑下
  • CarMapper.xml放在類的根路徑下
  • logback.xml放在類的根路徑下
  • 提供com.study.mybatis.utils.SqlSessionUtil工具類
  • 創(chuàng)建測試用例:com.study.mybatis.CarMapperTest

1 insert(Create)

分析以下SQL映射文件中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">

<!--namespace先隨便寫-->
<mapper namespace="car">
    <insert id="insertCar">
        insert into t_car(car_num, brand, guide_price, produce_time, car_type) values ('103', '奔馳E300L', 50.3, '2022-01-01', '燃油車')
    </insert>
</mapper>

存在的問題是:SQL語句中的值不應(yīng)該寫死,值應(yīng)該是用戶提供的。之前的JDBC代碼是這樣寫的:

// JDBC中使用 ? 作為占位符。那么MyBatis中會使用什么作為占位符呢?
String sql = "insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(?,?,?,?,?)";
// ......
// 給 ? 傳值。那么MyBatis中應(yīng)該怎么傳值呢?
ps.setString(1,"103");
ps.setString(2,"奔馳E300L");
ps.setDouble(3,50.3);
ps.setString(4,"2022-01-01");
ps.setString(5,"燃油車");

在MyBatis中可以這樣做:
在Java程序中,將數(shù)據(jù)放到Map集合中在sql語句中使用 #{map集合的key} 來完成傳值,#{} 等同于JDBC中的 ? ,#{}就是占位符Java程序這樣寫:

package com.study.mybatis;

import com.study.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 測試MyBatis的CRUD
 * @author sqnugy
 * @version 1.0
 * @since 1.0
 */
public class CarMapperTest {
    @Test
    public void testInsertCar(){
        // 準備數(shù)據(jù)
        Map<String, Object> map = new HashMap<>();
        map.put("k1", "103");
        map.put("k2", "奔馳E300L");
        map.put("k3", 50.3);
        map.put("k4", "2020-10-01");
        map.put("k5", "燃油車");
        // 獲取SqlSession對象
        SqlSession sqlSession = SqlSessionUtil.openSession();
        // 執(zhí)行SQL語句(使用map集合給sql語句傳遞數(shù)據(jù))
        int count = sqlSession.insert("insertCar", map);
        System.out.println("插入了幾條記錄:" + count);
    }
}

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">

<!--namespace先隨便寫-->
<mapper namespace="car">
    <insert id="insertCar">
        insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{k1},#{k2},#{k3},#{k4},#{k5})
    </insert>
</mapper>

**#{} 的里面必須填寫map集合的key,不能隨便寫。**運行測試程序,查看數(shù)據(jù)庫:

如果#{}里寫的是map集合中不存在的key會有什么問題?

<?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="car">
    <insert id="insertCar">
        insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{kk},#{k2},#{k3},#{k4},#{k5})
    </insert>
</mapper>

運行程序:

通過測試,看到程序并沒有報錯。正常執(zhí)行。不過 #{kk} 的寫法導(dǎo)致無法獲取到map集合中的數(shù)據(jù),最終導(dǎo)致數(shù)據(jù)庫表car_num插入了NULL。
在以上sql語句中,可以看到#{k1} #{k2} #{k3} #{k4} #{k5}的可讀性太差,為了增強可讀性,我們可以將Java程序做如下修改:

Map<String, Object> map = new HashMap<>();
// 讓key的可讀性增強
map.put("carNum", "103");
map.put("brand", "奔馳E300L");
map.put("guidePrice", 50.3);
map.put("produceTime", "2020-10-01");
map.put("carType", "燃油車");

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="car">
    <insert id="insertCar">
        insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
    </insert>
</mapper>

運行程序,查看數(shù)據(jù)庫表:

使用Map集合可以傳參,那使用pojo(簡單普通的java對象)可以完成傳參嗎?測試一下:

  • 第一步:定義一個pojo類Car,提供相關(guān)屬性。
package com.study.mybatis.pojo;

/**
 * POJOs,簡單普通的Java對象。封裝數(shù)據(jù)用的。
 * @author sqnugy
 * @version 1.0
 * @since 1.0
 */
public class Car {
    private Long id;
    private String carNum;
    private String brand;
    private Double guidePrice;
    private String produceTime;
    private String carType;

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", carNum='" + carNum + '\'' +
                ", brand='" + brand + '\'' +
                ", guidePrice=" + guidePrice +
                ", produceTime='" + produceTime + '\'' +
                ", carType='" + carType + '\'' +
                '}';
    }

    public Car() {
    }

    public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {
        this.id = id;
        this.carNum = carNum;
        this.brand = brand;
        this.guidePrice = guidePrice;
        this.produceTime = produceTime;
        this.carType = carType;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCarNum() {
        return carNum;
    }

    public void setCarNum(String carNum) {
        this.carNum = carNum;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Double getGuidePrice() {
        return guidePrice;
    }

    public void setGuidePrice(Double guidePrice) {
        this.guidePrice = guidePrice;
    }

    public String getProduceTime() {
        return produceTime;
    }

    public void setProduceTime(String produceTime) {
        this.produceTime = produceTime;
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType;
    }
}
  • 第二步:Java程序
@Test
public void testInsertCarByPOJO(){
    // 創(chuàng)建POJO,封裝數(shù)據(jù)
    Car car = new Car();
    car.setCarNum("103");
    car.setBrand("奔馳C200");
    car.setGuidePrice(33.23);
    car.setProduceTime("2020-10-11");
    car.setCarType("燃油車");
    // 獲取SqlSession對象
    SqlSession sqlSession = SqlSessionUtil.openSession();
    // 執(zhí)行SQL,傳數(shù)據(jù)
    int count = sqlSession.insert("insertCarByPOJO", car);
    System.out.println("插入了幾條記錄" + count);
}
  • 第三步:SQL語句
<insert id="insertCarByPOJO">
  <!--#{} 里寫的是POJO的屬性名-->
  insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
  • 運行程序,查看數(shù)據(jù)庫表:

#{} 里寫的是POJO的屬性名,如果寫成其他的會有問題嗎?

<insert id="insertCarByPOJO">
  insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{a},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>

運行程序,出現(xiàn)了以下異常:

錯誤信息中描述:在Car類中沒有找到a屬性的getter方法。

修改POJO類Car的代碼,只將getCarNum()方法名修改為getA(),其他代碼不變,如下:

再運行程序,查看數(shù)據(jù)庫表中數(shù)據(jù):

經(jīng)過測試得出結(jié)論:

如果采用map集合傳參,#{} 里寫的是map集合的key,如果key不存在不會報錯,數(shù)據(jù)庫表中會插入NULL。

如果采用POJO傳參,#{} 里寫的是get方法的方法名去掉get之后將剩下的單詞首字母變小寫(例如:getAge對應(yīng)的是#{age},getUserName對應(yīng)的是#{userName}),如果這樣的get方法不存在會報錯。

注意:其實傳參數(shù)的時候有一個屬性parameterType,這個屬性用來指定傳參的數(shù)據(jù)類型,不過這個屬性是可以省略的

<insert id="insertCar" parameterType="java.util.Map">
  insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>

<insert id="insertCarByPOJO" parameterType="com.study.mybatis.pojo.Car">
  insert into t_car(car_num,brand,guide_price,produce_time,car_type) values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>

2 delete(Delete)

需求:根據(jù)car_num進行刪除。
SQL語句這樣寫:

<delete id="deleteByCarNum">
  delete from t_car where car_num = #{SuiBianXie}
</delete>

Java程序這樣寫:

@Test
public void testDeleteByCarNum(){
    // 獲取SqlSession對象
    SqlSession sqlSession = SqlSessionUtil.openSession();
    // 執(zhí)行SQL語句
    int count = sqlSession.delete("deleteByCarNum", "102");
    System.out.println("刪除了幾條記錄:" + count);
}

運行結(jié)果:

注意:當占位符只有一個的時候,${} 里面的內(nèi)容可以隨便寫。

3 update(Update)

需求:修改id=34的Car信息,car_num102,brand比亞迪漢,guide_price30.23,produce_time2018-09-10car_type電車修改前:

SQL語句如下:

<update id="updateCarByPOJO">
  update t_car set 
    car_num = #{carNum}, brand = #{brand}, 
    guide_price = #{guidePrice}, produce_time = #{produceTime}, 
    car_type = #{carType} 
  where id = #{id}
</update>

Java代碼如下:

    @Test
    public void testUpdateCarByPOJO(){
        // 準備數(shù)據(jù)
        Car car = new Car();
        car.setId(34L);
        car.setCarNum("102");
        car.setBrand("比亞迪漢");
        car.setGuidePrice(30.23);
        car.setProduceTime("2018-09-10");
        car.setCarType("電車");
        // 獲取SqlSession對象
        SqlSession sqlSession = SqlSessionUtil.openSession();
        // 執(zhí)行SQL語句
        int count = sqlSession.update("updateCarByPOJO", car);
        System.out.println("更新了幾條記錄:" + count);
    }

運行結(jié)果:

0670B92C-E654-49a7-839C-814850A4D79A.png

當然了,如果使用map傳數(shù)據(jù)也是可以的。

4 select(Retrieve)

select語句和其它語句不同的是:查詢會有一個結(jié)果集。來看mybatis是怎么處理結(jié)果集的?。。?/p>

查詢一條數(shù)據(jù)

需求:查詢id為1的Car信息
SQL語句如下:

<select id="selectCarById">
  select * from t_car where id = #{id}
</select>

Java程序如下:

@Test
public void testSelectCarById(){
    // 獲取SqlSession對象
    SqlSession sqlSession = SqlSessionUtil.openSession();
    // 執(zhí)行SQL語句
    Object car = sqlSession.selectOne("selectCarById", 1);
    System.out.println(car);
}

運行結(jié)果如下:

### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: 
    A query was run and no Result Maps were found for the Mapped Statement 'car.selectCarById'.  【翻譯】:對于一個查詢語句來說,沒有找到查詢的結(jié)果映射。
    It's likely that neither a Result Type nor a Result Map was specified.                         【翻譯】:很可能既沒有指定結(jié)果類型,也沒有指定結(jié)果映射。

以上的異常大致的意思是:對于一個查詢語句來說,你需要指定它的“結(jié)果類型”或者“結(jié)果映射”。

所以說,你想讓mybatis查詢之后返回一個Java對象的話,至少你要告訴mybatis返回一個什么類型的Java對象,可以在<select>標簽中添加resultType屬性,用來指定查詢要轉(zhuǎn)換類型:

<select id="selectCarById" resultType="com.study.mybatis.pojo.Car">
  select * from t_car where id = #{id}
</select>

運行結(jié)果:

運行后之前的異常不再出現(xiàn)了,這說明添加了resultType屬性之后,解決了之前的異常,可以看出resultType是不能省略的。

仔細觀察控制臺的日志信息,不難看出,結(jié)果查詢出了一條。并且每個字段都查詢到值了:Row: 1, 100, 寶馬520Li, 41.00, 2022-09-01, 燃油車

但是奇怪的是返回的Car對象,只有id和brand兩個屬性有值,其它屬性的值都是null,這是為什么呢?我們來觀察一下查詢結(jié)果列名和Car類的屬性

名是否能一一對應(yīng):

查詢結(jié)果集的列名:id, car_num, brand, guide_price, produce_time, car_type

Car類的屬性名:id, carNum, brand, guidePrice, produceTime, carType

通過觀察發(fā)現(xiàn):只有id和brand是一致的,其他字段名和屬性名對應(yīng)不上,這是不是導(dǎo)致null的原因呢?我們嘗試在sql語句中使用as關(guān)鍵字來給查

詢結(jié)果列名起別名試試:

<select id="selectCarById" resultType="com.study.mybatis.pojo.Car">
  select 
    id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType 
  from 
    t_car 
  where 
    id = #{id}
</select>

運行結(jié)果如下:

在這里插入圖片描述

通過測試得知,如果當查詢結(jié)果的字段名和java類的屬性名對應(yīng)不上的話,可以采用as關(guān)鍵字起別名,當然還有其它解決方案,我們后面再看。

查詢多條數(shù)據(jù)

需求:查詢所有的Car信息。

SQL語句如下:

<!--雖然結(jié)果是List集合,但是resultType屬性需要指定的是List集合中元素的類型。-->
<select id="selectCarAll" resultType="com.study.mybatis.pojo.Car">
  <!--記得使用as起別名,讓查詢結(jié)果的字段名和java類的屬性名對應(yīng)上。-->
  select
    id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType
  from
    t_car
</select>

Java代碼如下:

@Test
public void testSelectCarAll(){
    // 獲取SqlSession對象
    SqlSession sqlSession = SqlSessionUtil.openSession();
    // 執(zhí)行SQL語句
    List<Object> cars = sqlSession.selectList("selectCarAll");
    // 輸出結(jié)果
    cars.forEach(car -> System.out.println(car));
}

運行結(jié)果如下:

5 關(guān)于SQL Mapper的namespace

在SQL Mapper配置文件中<mapper>標簽的namespace屬性可以翻譯為命名空間,這個命名空間主要是為了防止sqlId沖突的。
創(chuàng)建CarMapper2.xml文件,代碼如下:

<?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="car2">
    <select id="selectCarAll" resultType="com.study.mybatis.pojo.Car">
        select
            id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType
        from
            t_car
    </select>
</mapper>

不難看出,CarMapper.xmlCarMapper2.xml文件中都有 id="selectCarAll"CarMapper2.xml配置到mybatis-config.xml文件中。

<mappers>
  <mapper resource="CarMapper.xml"/>
  <mapper resource="CarMapper2.xml"/>
</mappers>

編寫Java代碼如下:

@Test
public void testNamespace(){
    // 獲取SqlSession對象
    SqlSession sqlSession = SqlSessionUtil.openSession();
    // 執(zhí)行SQL語句
    List<Object> cars = sqlSession.selectList("selectCarAll");
    // 輸出結(jié)果
    cars.forEach(car -> System.out.println(car));
}

運行結(jié)果如下:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: 
  selectCarAll is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries) 
  【翻譯】selectCarAll在Mapped Statements集合中不明確(請嘗試使用包含名稱空間的全名,或重命名其中一個條目)
  【大致意思是】selectCarAll重名了,你要么在selectCarAll前添加一個名稱空間,要有你改個其它名字。

Java代碼修改如下:

@Test
public void testNamespace(){
    // 獲取SqlSession對象
    SqlSession sqlSession = SqlSessionUtil.openSession();
    // 執(zhí)行SQL語句
    //List<Object> cars = sqlSession.selectList("car.selectCarAll");
    List<Object> cars = sqlSession.selectList("car2.selectCarAll");
    // 輸出結(jié)果
    cars.forEach(car -> System.out.println(car));
}

運行結(jié)果如下:

到此這篇關(guān)于MyBatis實現(xiàn)CRUD的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis實現(xiàn)CRUD內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java kafka寫入數(shù)據(jù)到HDFS問題

    java kafka寫入數(shù)據(jù)到HDFS問題

    這篇文章主要介紹了java kafka寫入數(shù)據(jù)到HDFS問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SpringBoot如何自動生成API文檔詳解

    SpringBoot如何自動生成API文檔詳解

    網(wǎng)絡(luò)程序正朝著移動設(shè)備的方向發(fā)展,前后端分離、APP,最好的交互交互方式莫過于通過API接口實現(xiàn),這篇文章主要給大家介紹了關(guān)于SpringBoot如何自動生成API文檔的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • javaWeb中使用Redis緩存實例解析

    javaWeb中使用Redis緩存實例解析

    這篇文章主要介紹了javaWeb中使用Redis緩存實例解析,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 迅速掌握Java容器中常用的ArrayList類與Vector類用法

    迅速掌握Java容器中常用的ArrayList類與Vector類用法

    這篇文章主要介紹了Java容器中常用的ArrayList類與Vector類用法,文中只對其最基本的功能給出了示例代碼,需要的朋友可以參考下
    2015-11-11
  • java?kafka如何動態(tài)設(shè)置用戶讀寫權(quán)限

    java?kafka如何動態(tài)設(shè)置用戶讀寫權(quán)限

    這篇文章主要介紹了java?kafka如何動態(tài)設(shè)置用戶讀寫權(quán)限問題,具有很好的參考家價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳解spring cloud使用Hystrix實現(xiàn)單個方法的fallback

    詳解spring cloud使用Hystrix實現(xiàn)單個方法的fallback

    本篇文章主要介紹了詳解spring cloud-使用Hystrix實現(xiàn)單個方法的fallback,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • SpringBoot詳解shiro過濾器與權(quán)限控制

    SpringBoot詳解shiro過濾器與權(quán)限控制

    當shiro被運用到web項目時,shiro會自動創(chuàng)建一些默認的過濾器對客戶端請求進行過濾。比如身份驗證、授權(quán)的相關(guān)的,這篇文章主要介紹了shiro過濾器與權(quán)限控制
    2022-07-07
  • SpringBoot中的Spring Cloud Hystrix原理和用法詳解

    SpringBoot中的Spring Cloud Hystrix原理和用法詳解

    在Spring Cloud中,Hystrix是一個非常重要的組件,Hystrix可以幫助我們構(gòu)建具有韌性的分布式系統(tǒng),保證系統(tǒng)的可用性和穩(wěn)定性,在本文中,我們將介紹SpringBoot中的Hystrix,包括其原理和如何使用,需要的朋友可以參考下
    2023-07-07
  • Java 多線程有序執(zhí)行的幾種方法總結(jié)

    Java 多線程有序執(zhí)行的幾種方法總結(jié)

    這篇文章主要介紹了Java 多線程有序執(zhí)行的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java實現(xiàn)curl調(diào)用帶參數(shù)接口方法

    Java實現(xiàn)curl調(diào)用帶參數(shù)接口方法

    本文主要介紹了Java實現(xiàn)curl調(diào)用帶參數(shù)接口方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04

最新評論