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

Mybatis如何自動生成sql語句

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

Mybatis自動生成sql語句

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

<?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 自動生成sql代碼  -->
<generatorConfiguration>
    <!-- 導(dǎo)入jar包(路徑) -->
    <classPathEntry location="E:\CourseWare\MYSQL\mysql-connector-java-5.1.26-bin.jar" />
    <!-- 設(shè)置生成代碼的規(guī)則 targetRuntime 開發(fā)環(huán)境使用Mybatis3的版本 -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
        <commentGenerator>
            <!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) -->
            <!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發(fā)生變化,不利于版本控制,所以設(shè)置為true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!-- 連接數(shù)據(jù)庫的四要素 -->
        <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ù)點和數(shù)字域的BigDecimal -->   
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 定義實體類 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ù)庫的表 -->
        <table tableName="emp"></table>
    </context>
</generatorConfiguration>

Mybatis的動態(tài)sql語句

Mybatis的動態(tài)sql語句主要解決的問題是不同條件sql語句的拼接。

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

if標(biāo)簽的使用

在持久層接口定義方法

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

編寫持久層接口對應(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>

編寫測試方法

  /**
     * 根據(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)簽的使用

為了簡化上面 where 1=1 的條件拼接,我們可以采用標(biāo)簽來簡化開發(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是對一個集合進(jìn)行遍歷,通常在構(gòu)建in條件語句的時候應(yīng)用

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

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

在這里插入圖片描述

編寫持久層接口方法

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

編寫持久層接口映射文件

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

編寫測試方法

  /**
     * 根據(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語句的簡化編寫

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

    <!--抽取重復(fù)的語句代碼片段-->
    <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>

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

相關(guān)文章

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

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

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

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

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

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

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

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

    這篇文章主要介紹了Mybatis實現(xiàn)動態(tài)增刪改查功能的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    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的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    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問題排查,具有很好的參考價值,希望對大家有所幫助
    2023-02-02
  • java中String StringBuffer和StringBuilder的區(qū)別詳解

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

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