亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis如何自動(dòng)生成sql語(yǔ)句

 更新時(shí)間:2021年12月14日 17:16:07   作者:GeBiao285869350  
這篇文章主要介紹了Mybatis如何自動(dòng)生成sql語(yǔ)句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis自動(dòng)生成sql語(yǔ)句

創(chuàng)建maven項(xiàng)目,將該配置文件運(yùn)行即可生成 sql 語(yǔ)句

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- MyBatis 自動(dòng)生成sql代碼  -->
<generatorConfiguration>
    <!-- 導(dǎo)入jar包(路徑) -->
    <classPathEntry location="E:\CourseWare\MYSQL\mysql-connector-java-5.1.26-bin.jar" />
    <!-- 設(shè)置生成代碼的規(guī)則 targetRuntime 開(kāi)發(fā)環(huán)境使用Mybatis3的版本 -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
        <commentGenerator>
            <!-- 這個(gè)元素用來(lái)去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) -->
            <!-- 如果生成日期,會(huì)造成即使修改一個(gè)字段,整個(gè)實(shí)體類所有屬性都會(huì)發(fā)生變化,不利于版本控制,所以設(shè)置為true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!-- 連接數(shù)據(jù)庫(kù)的四要素 -->
        <jdbcConnection 
            driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/user" 
            userId="root"
            password="root">
        </jdbcConnection>
        <!-- 該屬性用于指定MyBatis生成器是否應(yīng)該強(qiáng)制使用java.math。小數(shù)點(diǎn)和數(shù)字域的BigDecimal -->   
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 定義實(shí)體類 bean -->
        <javaModelGenerator targetPackage="en.et.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 接口映射的注解 或者xml文件路徑 -->
        <sqlMapGenerator targetPackage="cn.et.resource" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成的接口所在的位置 type="xml 或者 注解" -->
        <javaClientGenerator type="ANNOTATEDMAPPER"
            targetPackage="en.et.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 告訴mbg 需要生成代碼的數(shù)據(jù)庫(kù)的表 -->
        <table tableName="emp"></table>
    </context>
</generatorConfiguration>

Mybatis的動(dòng)態(tài)sql語(yǔ)句

Mybatis的動(dòng)態(tài)sql語(yǔ)句主要解決的問(wèn)題是不同條件sql語(yǔ)句的拼接。

例如:根據(jù)用戶信息,查詢用戶列表,當(dāng)不知道根據(jù)的是用戶的什么信息時(shí),寫(xiě)出查詢的SQL語(yǔ)句是有一定困難的,而動(dòng)態(tài)SQL語(yǔ)句主要解決的就是此類問(wèn)題。

if標(biāo)簽的使用

在持久層接口定義方法

    /**
     * 根據(jù)用戶信息,查詢用戶列表
     * @param user
     * @return
     */
    List<User> findByUser(User user);

編寫(xiě)持久層接口對(duì)應(yīng)的映射文件

 <!-- 根據(jù)用戶信息,查詢用戶列表 -->
    <select id="findByUser" resultType="User" parameterType="User">
        select *from user where 1 = 1
        <if test="id != 0">
            and id = #{id}
        </if>
        <if test="username != null and username != '' ">
            and username like #{username}
        </if>
        <if test="birthday != null">
            and birthday = #{birthday}
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
        <if test="address != null">
            and address = #{address}
        </if>
    </select>

編寫(xiě)測(cè)試方法

  /**
     * 根據(jù)用戶信息,查詢用戶列表
     */
    @Test
    public void testFindByUser()
    {
        User user = new User();
        user.setUsername("%王%");
        List<User> users = userDao.findByUser(user);
        for (User u : users)
        {
            System.out.println(u);
        }
    }

where標(biāo)簽的使用

為了簡(jiǎn)化上面 where 1=1 的條件拼接,我們可以采用標(biāo)簽來(lái)簡(jiǎn)化開(kāi)發(fā),因此修改持久層映射文件

 <!-- 根據(jù)用戶信息,查詢用戶列表 -->
    <select id="findByUser" resultType="User" parameterType="User">
        select *from user
        <where>
            <if test="id != 0">
                and id = #{id}
            </if>
            <if test="username != null and username != '' ">
                and username like #{username}
            </if>
            <if test="birthday != null">
                and birthday = #{birthday}
            </if>
            <if test="sex != null">
                and sex = #{sex}
            </if>
            <if test="address != null">
                and address = #{address}
            </if>
        </where>
    </select>

foreach標(biāo)簽的使用

froeach是對(duì)一個(gè)集合進(jìn)行遍歷,通常在構(gòu)建in條件語(yǔ)句的時(shí)候應(yīng)用

例如:根據(jù)一個(gè)用戶id集合查詢用戶。

對(duì)id集合進(jìn)行封裝,加入到List集合

在這里插入圖片描述

編寫(xiě)持久層接口方法

  /**
     * 根據(jù)id集合查詢用戶
     * @param queryUR
     * @return
     */
    List<User> findInIds(QueryUR queryUR);

編寫(xiě)持久層接口映射文件

  <!--根據(jù)id集合查詢用戶 -->
    <select id="findInIds" resultType="user" parameterType="int">
       select *from user
       <where>
           <if test="ids != null and ids.size() > 0">
                <!-- foreach:用于遍歷集合
                    collection:代表要遍歷的集合
                    open:代表語(yǔ)句的開(kāi)始部分
                    close:代表語(yǔ)句的結(jié)束部分
                    item:代表需要遍歷的集合的每個(gè)元素
                    sperator:代表分隔符
                 -->
               <foreach collection="ids" open="id in (" close=")" item="uid" separator=",">
                    #{uid}
               </foreach>
           </if>
       </where>
    </select>

編寫(xiě)測(cè)試方法

  /**
     * 根據(jù)id集合查詢用戶
     */
    @Test
    public void testFindInIds()
    {
        QueryUR queryUR = new QueryUR();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(41);
        ids.add(43);
        ids.add(45);
        queryUR.setIds(ids);
        List<User> users = userDao.findInIds(queryUR);
        for (User user : users)
        {
            System.out.println(user);
        }
    }

sql語(yǔ)句的簡(jiǎn)化編寫(xiě)

在映射文件中,可以將重復(fù)的sql語(yǔ)句通過(guò)sql標(biāo)簽提取出來(lái),使用include標(biāo)簽引用即可,已達(dá)到sql重用的效果。如下所示:

    <!--抽取重復(fù)的語(yǔ)句代碼片段-->
    <sql id="querySql">
        select *from user
    </sql>
    <select id="findAll" resultType="USER" >
        <include refid="querySql"></include>
    </select>
    <!--根據(jù)id查詢用戶-->
    <select id="findById" resultType="User" parameterType="int">
        <include refid="querySql"></include> where id= #{userID};
    </select>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring cloud 的監(jiān)控turbine-rabbitmq的示例

    spring cloud 的監(jiān)控turbine-rabbitmq的示例

    這篇文章主要介紹了spring cloud 的監(jiān)控turbine-rabbitmq的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Java?17新特性詳細(xì)講解與代碼實(shí)例

    Java?17新特性詳細(xì)講解與代碼實(shí)例

    這篇文章主要給大家介紹了關(guān)于Java?17新特性詳細(xì)講解與代碼實(shí)例的相關(guān)資料,Java 17是2021年9月發(fā)布的最新版本,其中包含了很多新特性和改進(jìn),這些新特性和改進(jìn)將進(jìn)一步提高 Java 語(yǔ)言的性能和可用性,需要的朋友可以參考下
    2023-09-09
  • java如何從不規(guī)則的字符串中截取出日期

    java如何從不規(guī)則的字符串中截取出日期

    這篇文章主要介紹了java從不規(guī)則的字符串中截取出日期的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Mybatis實(shí)現(xiàn)動(dòng)態(tài)增刪改查功能的示例代碼

    Mybatis實(shí)現(xiàn)動(dòng)態(tài)增刪改查功能的示例代碼

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)動(dòng)態(tài)增刪改查功能的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Presto支持Elasticsearch數(shù)據(jù)源配置詳解

    Presto支持Elasticsearch數(shù)據(jù)源配置詳解

    這篇文章主要為大家介紹了Presto支持Elasticsearch數(shù)據(jù)源配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringMVC中的http Caching的具體使用

    SpringMVC中的http Caching的具體使用

    本文主要介紹了SpringMVC中的http Caching的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 排查Failed?to?validate?connection?com.mysql.cj.jdbc.ConnectionImpl

    排查Failed?to?validate?connection?com.mysql.cj.jdbc.Connec

    這篇文章主要介紹了Failed?to?validate?connection?com.mysql.cj.jdbc.ConnectionImpl問(wèn)題排查,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2023-02-02
  • java中String StringBuffer和StringBuilder的區(qū)別詳解

    java中String StringBuffer和StringBuilder的區(qū)別詳解

    大家好,本篇文章主要講的是java中String StringBuffer和StringBuilder的區(qū)別詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • 最新評(píng)論