最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Mybatis 基本使用過(guò)程(推薦)

 更新時(shí)間:2024年09月23日 11:27:18   作者:JOJO___  
Mybatis是一個(gè)半自動(dòng)ORM(Object Relational Mapping)框架,它可以簡(jiǎn)化數(shù)據(jù)庫(kù)編程,讓開(kāi)發(fā)者更專注于SQL本身,本文給大家介紹Spring Mybatis 基本使用過(guò)程,感興趣的朋友跟隨小編一起看看吧

1. 簡(jiǎn)介

Mybatis庫(kù)可以簡(jiǎn)化數(shù)據(jù)庫(kù)的操作,專注于sql語(yǔ)句。

2.搭建步驟

2.1 在pom.xml引入mybatis

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.11</version>
</dependency>

2.2 在resources下新建mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Mapper 3.0/EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
<!--        <setting name="logImpl" value="STDOUT_LOGGING"/>&lt;!&ndash; 開(kāi)啟mybatis的日志輸出 &ndash;&gt;-->
        <setting name="mapUnderscoreToCamelCase" value="true"/><!-- 開(kāi)啟駝峰式自動(dòng)映射 a_big => aBig -->
    </settings>
    <typeAliases>
        <typeAlias alias="goods" type="com.jojo.pojo.Goods"/><!-- 單獨(dú)設(shè)置別名 -->
        <package name="com.jojo.pojo"/><!-- 批量設(shè)置別名, com.jojo.pojo包下的所有類名的別名為類的首字母小寫-->
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>  <!-- 自動(dòng)開(kāi)啟事務(wù) -->
            <dataSource type="POOLED"><!-- mybatis維護(hù)連接池 -->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis-example"/>
                <property name="username" value="root"/>
                <property name="password" value="a12345678"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 指定mapper xml文件的位置 -->
        <mapper resource="mappers/GoodsMapper.xml"/>
    </mappers>
</configuration>

2.3 在resources/mapper下新建mapper的xml配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0/EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jojo.mapper.GoodsMapper"><!-- 對(duì)應(yīng)Mapper的全限定符 -->
<!--    這里寫sql語(yǔ)句 -->
    <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into goods (name) value(#{name})
    </insert>
    <update id="update">
        update goods set name=#{name} where id=#{id}
    </update>
    <delete id="delete">
        delete from goods where id = #{id}
    </delete>
    <select id="selectById" resultType="goods">
        select * from goods where id = #{id}
    </select>
    <select id="selectAll" resultType="goods">
        select * from goods
    </select>
</mapper>

2.4 新建pojo類

import lombok.Data;
@Data//lombook插件的@Data標(biāo)簽可以自動(dòng)生成get和set以及toString方法
public class Goods {
    private Integer id;
    private String name;
}

2.5 新建mapper接口

public interface GoodsMapper {
    int insert(Goods goods);
    int update(Goods goods);
    int delete(Integer id);
    Goods selectById(Integer id);
    List<Goods> selectAll();
}

2.6 測(cè)試

public class MybatisTest {
    @Test
    public void test() throws IOException {
        //1.讀取外部配置文件
        InputStream ips = Resources.getResourceAsStream("mybatis-config.xml");
        //2.創(chuàng)建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);
        //3.根據(jù)sqlSessionFactory創(chuàng)建sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.獲取接口的代理對(duì)象,調(diào)用代理對(duì)象的方法就會(huì)查找mapper接口的方法
        GoosdMapper mapper = sqlSession.getMapper(GoosdMapper.class);
        Goods goods = mapper.queryById(1);
        System.out.println(goods);
        //5.提交事務(wù)和釋放資源
        //sqlSession.commit();
        sqlSession.close();
    }
}

3.常用mapper語(yǔ)句

3.1 傳入值

<!-- #{id} = 使用占位符?,防止sql注入攻擊,但不能替代表名表項(xiàng)-->
<!-- ${id} = 不使用占位符?,不能防止sql注入攻擊,但可以替代表名表項(xiàng)-->
<select id="queryById" resultType="com.jojo.pojo.Employee">
    select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where emp_id = #{id}
</select>
<delete id="deleteById">
    delete from t_emp where emp_id = #{id} <!-- 傳入Integer類型,id可以改寫成任意字符串-->
</delete>
<select id="queryBySalary" resultType="com.jojo.pojo.Employee">
    select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{salary} <!-- 傳入Double類型,salary可以改寫成任意字符串-->
</select>
<insert id="insertEmp">
    insert into t_emp (emp_name, emp_salary) values (#{empName},#{empSalary});<!-- 傳入對(duì)象時(shí),要寫傳入對(duì)象的屬性 -->
</insert>
<select id="queryByNameAndSalary" resultType="com.jojo.pojo.Employee">
    select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{a} and empName = # <!-- 傳入兩個(gè)基本類型,根據(jù)接口中的@Param("名稱")來(lái)指定-->
</select>
<select id="queryByNameAndSalary" resultType="com.jojo.pojo.Employee">
    select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{arg0} and empName = #{arg1} <!-- 法2:傳入兩個(gè)基本類型,可以根據(jù)順序來(lái)取arg0...arg1...-->
</select>
<select id="queryByNameAndSalary" resultType="com.jojo.pojo.Employee">
    select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{param1} and empName = #{param2} <!-- 法3:傳入兩個(gè)基本類型,可以根據(jù)順序來(lái)取param1...param2...-->
</select>
<insert id="insertEmpMap">
    insert into t_emp (emp_name, emp_salary) values (#{name},#{salary});<!-- 傳入Map時(shí),要寫傳入Map的key -->
</insert>

3.2 返回值

<select id="queryNameById" resultType="string"><!-- resultType指定返回的類型,寫類的全限定符或者mybatis提供的別名(在mybatis官網(wǎng)查)-->
    select emp_name from t_emp where emp_id = #{id} 
</select>
<select id="queryById" resultType="employee"> <!-- resultType指定返回的為對(duì)象時(shí),select的行需要起別名來(lái)與類的屬性完全一致-->
    select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where emp_id = #{id} 
</select>
<select id="queryById" resultType="employee"><!-- resultType指定返回的為對(duì)象時(shí),開(kāi)啟駝峰映射(mapUnderscoreToCamelCase)后,select的行不再需要起別名來(lái)與類的屬性完全一致-->
    select * from t_emp where emp_id = #{id}  
</select>
<select id="selectEmpNameAndMaxSalary" resultType="map"> <!-- resultType返回的值沒(méi)有未定義類時(shí),可以用map接值,map的每一項(xiàng)的key對(duì)應(yīng)一個(gè)列名 -->
    select emp_name 員工姓名, emp_salary 員工工資, (SELECT AVG(emp_salary) from t_emp) 部門平均工資 from t_emp where emp_salary=(select max(emp_salary) from t_emp)
</select>
<select id="queryNamesBySalary" resultType="string"><!--如果返回類型時(shí)List<String>,那么指定String即可-->
    select emp_name from t_emp where emp_salary > #{ salary};
</select>
<select id="queryAll" resultType="employee"><!--如果返回類型時(shí)List<Employee>,那么指定Employee即可-->
    select * from t_emp;
</select>
<insert id="insertEmp" useGeneratedKeys="true" keyColumn="emp_id" keyProperty="empId"><!-- 主鍵自增長(zhǎng)型:插入時(shí),獲取插入的id放在empId中 -->
    insert into t_emp (emp_name, emp_salary) value(#{empName},#{empSalary});
</insert>
<insert id="insertTeacher">
    <selectKey order="BEFORE" resultType="string" keyProperty="tId">
        select replace(UUID(),'-',''); <!-- 插入前由數(shù)據(jù)庫(kù)生成uuid并放在tId中-->
    </selectKey>
    insert into teacher (t_id,t_name) value (#{tId},#{tName})
</insert>

4.多表查詢

4.1 一對(duì)一

1對(duì)1關(guān)系:一個(gè)A類中包含一個(gè)B類:

public class A {
    private Integer Id;
    private String aName;
    private Integer bId;
    private B b;
}
public class B {
    private Integer bId;
    private String bName;
}

使用resultMap來(lái)裝數(shù)據(jù):

<resultMap id="aMap" type="a">
    <!-- a的主鍵 id標(biāo)簽-->
    <id column="a_id" property="aId"/>
    <!-- order的普通列 custom標(biāo)簽-->
    <result column="a_name" property="aName"/>
    <result column="b_id" property="bId"/>
    <!-- 給第二層對(duì)象屬性賦值 -->
    <association property="b" javaType="b">
        <id column="b_id" property="bId"/>
        <result column="b_name"  property="bName"></result>
    </association>
</resultMap>
<select id="queryAById" resultMap="aMap">
    SELECT * FROM t_a ta join t_b tb on ta.b_id = tb.b_id where ta.a_id = #{id};
</select>

在config文件中加入:

<settings>
	<!-- 開(kāi)啟駝峰式自動(dòng)映射 a_big => aBig -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
    <!-- 開(kāi)啟自動(dòng)映射 a_big => aBig -->
    <setting name="autoMappingBehavior" value="FULL"/>
</settings>

后可省略主鍵以外的映射關(guān)系:

<resultMap id="aMap" type="a">
    <!-- a的主鍵 id標(biāo)簽-->
    <id column="a_id" property="aId"/>
    <!-- 給第二層對(duì)象屬性賦值 -->
    <association property="b" javaType="b">
        <id column="b_id" property="bId"/>
    </association>
</resultMap>
<select id="queryAById" resultMap="aMap">
    SELECT * FROM t_a ta join t_b tb on ta.b_id = tb.b_id where ta.a_id = #{id};
</select>

4.2 一對(duì)多

1對(duì)多關(guān)系:一個(gè)A類中包含多個(gè)B類(List):

public class A {
    private Integer Id;
    private String aName;
    private Integer bId;
    private List<B> bList;
}
public class B {
    private Integer bId;
    private String bName;
}

使用resultMap來(lái)裝數(shù)據(jù):

<resultMap id="aMap" type="a">
    <id column="a_id" property="aId"/>
    <result column="a_name" property="aName"/>
     <result column="b_id" property="bId"/>
    <!--針對(duì)List<A>屬性使用collection -->
    <collection property="bList" ofType="b">
        <id column="b_id" property="bId"></id>
        <result column="b_name" property="bName"/>
    </collection>
</resultMap>
<select id="queryAList" resultMap="aMap">
    select * from t_a ta join t_b tb on ta.customer_id = tb.customer_id
</select>

在config文件中加入:

<settings>
	<!-- 開(kāi)啟駝峰式自動(dòng)映射 a_big => aBig -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
    <!-- 開(kāi)啟自動(dòng)映射 a_big => aBig -->
    <setting name="autoMappingBehavior" value="FULL"/>
</settings>

后可省略主鍵以外的映射關(guān)系:

<resultMap id="aMap" type="a">
    <id column="a_id" property="aId"/>
    <!--針對(duì)List<A>屬性使用collection -->
    <collection property="bList" ofType="b">
        <id column="b_id" property="bId"></id>
    </collection>
</resultMap>
<select id="queryAList" resultMap="aMap">
    select * from t_a ta join t_b tb on ta.customer_id = tb.customer_id
</select>

到此這篇關(guān)于Spring Mybatis 基本使用 總結(jié)的文章就介紹到這了,更多相關(guān)Spring Mybatis 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go Java算法之K個(gè)重復(fù)字符最長(zhǎng)子串詳解

    Go Java算法之K個(gè)重復(fù)字符最長(zhǎng)子串詳解

    這篇文章主要為大家介紹了Go Java算法之K個(gè)重復(fù)字符最長(zhǎng)子串詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 調(diào)用Mybatis?plus中的saveBatch方法報(bào)找不到表的問(wèn)題

    調(diào)用Mybatis?plus中的saveBatch方法報(bào)找不到表的問(wèn)題

    在用Mybatis plus開(kāi)發(fā)的項(xiàng)目中,用自帶的API批量保存的方法saveBatch操作時(shí),發(fā)現(xiàn)報(bào)沒(méi)有找到表的錯(cuò)誤,本文就來(lái)詳細(xì)的介紹一下解決方法,感興趣的可以了解一下
    2024-03-03
  • Java編程打印購(gòu)物小票實(shí)現(xiàn)代碼

    Java編程打印購(gòu)物小票實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java編程打印購(gòu)物小票實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 使用GenericObjectPool避免泄漏設(shè)置方法

    使用GenericObjectPool避免泄漏設(shè)置方法

    這篇文章主要為大家介紹了使用GenericObjectPool避免泄漏的設(shè)置方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringMVC中處理靜態(tài)資源的過(guò)程詳解

    SpringMVC中處理靜態(tài)資源的過(guò)程詳解

    本文給大家介紹SpringMVC中處理靜態(tài)資源的過(guò)程,結(jié)合示例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • Java?8中?Stream小知識(shí)小技巧方法梳理

    Java?8中?Stream小知識(shí)小技巧方法梳理

    這篇文章主要介紹了Java8中Stream小知識(shí)小技巧方法梳理,Stream流和迭代器一樣,它只能夠迭代一次。當(dāng)它遍歷完的時(shí)候,我們就稱它已經(jīng)消費(fèi)完了。如果還想重新執(zhí)行操作,那么就只能從原來(lái)的地方再獲取一個(gè)流
    2022-09-09
  • SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson)

    SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson)

    有時(shí)候,我們接口返回的數(shù)據(jù)需要做一些處理,有一些敏感數(shù)據(jù),本文主要介紹了SpringBoot接口返回?cái)?shù)據(jù)脫敏(Mybatis、Jackson),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • 解讀@ConfigurationProperties使用時(shí)的幾個(gè)常見(jiàn)誤區(qū)

    解讀@ConfigurationProperties使用時(shí)的幾個(gè)常見(jiàn)誤區(qū)

    在Spring Boot中,@ConfigurationProperties注解用于綁定配置文件中的屬性到Java對(duì)象,它支持properties和yml文件格式,并且可以通過(guò)prefix屬性指定配置屬性的前綴,需要注意的是,@PropertySource注解默認(rèn)只支持properties文件,不支持yml文件
    2024-10-10
  • JAVA值傳遞和引用傳遞方式

    JAVA值傳遞和引用傳遞方式

    文章總結(jié):在Java中,處理不可變集合時(shí),直接修改操作會(huì)拋出異常,正確的做法是使用可變集合類型,如ArrayList,或者通過(guò)流操作(stream().filter())來(lái)實(shí)現(xiàn)修改,理解Java方法參數(shù)的傳遞方式(值傳遞)是關(guān)鍵,這決定了如何正確地修改對(duì)象的狀態(tài)
    2024-11-11
  • 使用IDEA創(chuàng)建SpringBoot項(xiàng)目的方法步驟

    使用IDEA創(chuàng)建SpringBoot項(xiàng)目的方法步驟

    這篇文章主要介紹了使用IDEA創(chuàng)建SpringBoot項(xiàng)目的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論

平邑县| 左权县| 孝义市| 黔江区| 林州市| 乐昌市| 浦江县| 辰溪县| 红安县| 太仆寺旗| 阿鲁科尔沁旗| 乐亭县| 桐梓县| 旌德县| 凉山| 察隅县| 钟祥市| 顺昌县| 肥东县| 两当县| 贵定县| 平顺县| 宁南县| 娄烦县| 阳谷县| 洪洞县| 荣成市| 沽源县| 博爱县| 剑川县| 崇义县| 岑溪市| 林芝县| 城步| 乐昌市| 西宁市| 河西区| 溧阳市| 寿宁县| 师宗县| 江达县|