<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、 面對場景的最佳實踐 mybatis代碼實現方式 1. 使用mybatis generator做代碼自動生成 2. 使用XML方式實現 3. 使用注解方式實現 以上三種實現方式,有自己適合的應用場景,按照4.9章節中集成方式,三種方法全部可以支持。下面是結合筆者多年的mybatis使用經驗,總結出在不同的場景下,使用不同的實現方式 ### 場景一:單表的增刪改查 mybatis generator生成的代碼能夠完成90%的**單表操作**,而且不用自己去書寫SQL。使用非常方便! > 這種用法面對開發人員非常友好,有的人說經常用這個會忘記怎么寫SQL。我可以斬釘截鐵的回答:不會的,因為你腦袋里面沒有SQL,是用不明白這種方式的。但是這種用法雖然簡單易用,也會產生一個問題,就是通常寫一個關聯查詢就可以得到的結果,開發人員會傾向于用多次使用單表查詢(因為寫起來簡單,可以犯懶)。說實話性能倒不會一定下降,但代碼會很冗余。項目組如果想避免這種情況發生,要特意強調做好規范。 **自動生成的代碼 最大程度幫你完成單表操作。涉及到關聯查詢、繼承,Mybatis文件和SQL還是要你自己寫,但是不要在生成的代碼基礎上面改!切記!**如果使用自動代碼生成感覺不適合自己或自己的項目,使用類似于Mybatis-Plus這種第三方增強庫,也是很方便的。 ### 場景二: 多查詢條件的查詢(或多表關聯查詢) 在web開發中,有一個典型的應用場景是:一個web table頁面有多個查詢條件,選擇填寫不同的查詢條件得到不同的查詢結果,多個查詢條件只填寫幾個條件進行查詢,其他的條件不填寫。 面對這種場景,就需要ORM框架對**動態SQL(根據傳入參數不同,SQL會發生變化)**有很好的支持,包括書寫的方便度等。從這個角度上講,mybatis的xml的是實現方式獨占鰲頭。 ~~~ <select id="findStudent" parameterType="int" resultType="com.zimug.bootlaunch.testdb1.model.Student"> SELECT STUD_ID AS studId,name,email,dob FROM STUDENT <trim prefix="WHERE" prefixOverrides="AND|OR" suffixOverrides="AND|OR"> <if test="stuId != null" > AND STUD_ID = #{stuId} </if> <if test="name != null and name != '' " > AND name = #{name} </if> <if test="email != null and email != '' " > AND email= #{email} </if> </trim> </select> ~~~ ~~~ List<Student> findStudent(@Param("stuId") Integer stuId, @Param("name") String name, @Param("email") String email); ~~~ 另外如果你做一個多表的關聯查詢,不需要使用動態SQL的情況下,使用XML方式也是一個不錯的選擇。比起在注解方式里面字符串拼SQL要更好。 ### 場景三: 除上面兩種場景外的其他場景 其實除去上面兩種場景,剩下的情況已經不多了,但是還是可以舉幾個例子: 比如:針對單表只有插入操作,你有不想因此生成一套完整的針對單表的操作代碼。 比如:只是臨時起意,寫一個較為簡單的SQL。 ~~~ public interface AnonStudentMapper { @Select("SELECT STUD_ID AS studId, NAME, EMAIL, DOB " + "FROM STUDENT " + "WHERE STUD_ID=#{studId}") List<Student> findStudentById(Integer studId); } ~~~ 可以看到這種方式,最好是SQL在兩三行以內,并且沒有嵌套SQL,否則會陷入維護的災難! ## 二、查詢結果屬性映射的最佳實踐 ### 使用駝峰映射結果屬性(約定大于配置的最佳實踐) Mybatis給我們提供了一種映射方式,如果屬性的命名是遵從駝峰命名法的,數據列名遵從下劃線命名。這樣就可以一勞永逸,無論以后寫多少查詢SQL都不需要單獨制定映射規則。 那么可以使用這種方式,類似如下: * 實體類屬性userName對應SQL的字段user\_name; * 實體類屬性userId對應SQL的字段user\_id; 在Spring boot環境下只需要寫這樣一個配置即可。 ~~~ mybatis: configuration: mapUnderscoreToCamelCase: true ~~~ ### 其他的實現方式(知道有這幾種方式即可,一旦別人用你知道怎么回事,自己不要用) 其他的實現方式都很不友好,都需要寫一個查詢SQL,做一套映射配置。 第一種:xml的屬性映射舉例resultMap ~~~ <mapper namespace="data.UserMapper"> <resultMap type="data.User" id="userResultMap"> <!-- 用id屬性來映射主鍵字段 --> <id property="id" column="user_id"/> <!-- 用result屬性來映射非主鍵字段 --> <result property="userName" column="user_name"/> </resultMap> </mapper> ~~~ 第二種:通過注解 @Results 和 @Result 這兩個注解是與XML文件中的標簽相對應的: * @Results對應resultMap * @Result對應result 這兩個注解是應用在方法的級別上的,也就是在mapper方法上,如下: ~~~ @Select("select * from t_user where user_name = #{userName}") @Results( @Result(property = "userId", column = "user_id"), @Result(property = "userName", column = "user_name") ) User getUserByName(@Param("userName") String userName); ~~~ 第三種:通過SQL字段別名來完成映射 ~~~ @Select("select user_name as userName, user_id as userId from t_user where user_name = #{userName}") User getUserByName(@Param("userName") String userName); ~~~ ## 三、使用@MapperScan而不是@mapper ~~~ @SpringBootApplication @MapperScan(basePackages = {"com.zimug.**.mapper"}) public class DemoMybatisApplication { public static void main(String[] args) { SpringApplication.run(DemoMybatisApplication.class, args); } } ~~~ 這樣就會自動掃描com.zimug.\*\*.mapper目錄下面的所有XXXXMapper文件,并且完成自動注入。 不需要在每一個Mapper上面都加@Mapper注解。 如下,不需要這么做: ~~~ @Mapper public interface DemoMapper { @Insert("insert into Demo(name) values(#{name})") @Options(keyProperty="id",keyColumn="id",useGeneratedKeys=true) public void save(Demo demo); } ~~~ ## 四、 將XxxxMapper.java文件和XxxxMapper.xml文件放在同一個目錄下面 我們寫代碼的時候,通常是XxxxMapper.java文件和XxxxMapper.xml文件一起寫的,新建和修改幾乎都是一起進行。 但是按照springboot和maven的約定,java文件放在/src/main/java下面,xml文件要放在resources目錄下面。 這樣我們就要來回的切換目錄,找文件,很麻煩。可以通過如下pom.xml配置來解決這個問題。通過如下配置,我們就可以將XML文件和java文件都放在/src/main/java下面子目錄,在一起。 ~~~ <resources> <resource> <directory>src/main/java</directory> <filtering>false</filtering> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> </resource> </resources> ~~~ ## 五、使用PageHelper分頁插件 引入maven依賴包 ~~~ <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> ~~~ 測試用例,下面的方法查詢第一頁的數據,每頁查詢返回2條數據 ~~~ @RunWith(SpringRunner.class) @SpringBootTest public class MybatisTest { @Resource ArticleMapper articleMapper; @Test public void testPageHelper(){ // 只有緊跟在PageHelper.startPage方法后的第一個Mybatis的查詢(Select)方法會被分頁!!!! PageHelper.startPage(1, 2); List<Article> articles = articleMapper.selectByExample(null); PageInfo<Article> page = PageInfo.of(articles); System.out.println(page); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看