91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Boot整合mybatis并自動生成mapper和實體實例解析

發布時間:2020-08-24 20:11:56 來源:腳本之家 閱讀:411 作者:mrr 欄目:編程語言

最近一直都在學習Java,發現目前Java招聘中,mybatis出現的頻率挺高的,可能是目前Java開發中使用比較多的數據庫ORM框架。于是我準備研究下Spring Boot和mybatis的整合。

1.在pom.xml文件中添加下面的配置

<dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.29</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
 <!-- 這個是官方的mybatis依賴,這個你不加沒法用噻 -->
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
  </dependency>
  <!-- 這個是自動生成mapper等的依賴,必須得加-->
  <dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator</artifactId>
   <version>1.3.5</version>
   <type>pom</type>
  </dependency>
  <!--這個是自動生成mapper等的依賴,必須得加 https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
  <dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.5</version>
  </dependency> 
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
     <!--配置文件存放的地址-->
     <!--<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>-->
     <verbose>true</verbose>
     <overwrite>true</overwrite>
    </configuration>
   </plugin>
  </plugins>
 </build>

2.在application.properties配置文件中進行數據庫和mybatis掃描的配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/yddy?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
mybatis.config-locations=classpath:mybatis-config.xml
# mapper文件
mybatis.mapper-locations=classpath:mapper/*.xml #這里直接寫*,對后面自定義sql,編寫xxxExt.xml有好處,自動掃描到這些文件,不用再改配置了
mybatis.type-aliases-package=com.dameiweb.learn.model
logging.level.com.dameiweb.learn.dao=debug
#mybatis.config = mybatis 配置文件名稱
#mybatis.mapperLocations = mapper xml 文件地址
#mybatis.typeAliasesPackage = 實體類包路徑
#mybatis.typeHandlersPackage = type handlers 處理器包路徑
#mybatis.check-config-location = 檢查 mybatis 配置是否存在,一般命名為 mybatis-config.xml
#mybatis.executorType = 執行模式。默認是 SIMPLE

3.mybatis的配置和自動生成mapper的配置

在resource目錄下新建一個generatorConfig.xml和mybatis-config.xml。內容如下:

generatorConfig.xml內容

<?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">
<generatorConfiguration>
 <classPathEntry location="C:\Users\nick\.m2\repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar"/>
 <context id="DB2Tables" targetRuntime="MyBatis3">
  <commentGenerator>
   <property name="suppressDate" value="true"/>
   <property name="suppressAllComments" value="true"/>
  </commentGenerator>
  <!--數據庫鏈接地址賬號密碼-->
  <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/yddy?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" userId="root" password="123456">
  </jdbcConnection>
  <javaTypeResolver>
   <property name="forceBigDecimals" value="false"/>
  </javaTypeResolver>
  <!--生成Model類存放位置-->
  <javaModelGenerator targetPackage="com.dameiweb.learn.model" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
   <!-- 從數據庫返回的值被清理前后的空格 -->
   <property name="trimStrings" value="false"/>
  </javaModelGenerator>
  <!--生成映射文件存放位置-->
  <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
   <property name="enableSubPackages" value="true"/>
  </sqlMapGenerator>
  <!--生成Dao類存放位置-->
  <!-- 客戶端代碼,生成易于使用的針對Model對象和XML配置文件 的代碼
    type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象
    type="MIXEDMAPPER",生成基于注解的Java Model 和相應的Mapper對象
    type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
  -->
  <javaClientGenerator type="XMLMAPPER" targetPackage="com.dameiweb.learn.dao" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
  </javaClientGenerator>
 </context>
</generatorConfiguration>

mybatis-config.xml的內容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <settings>
  <setting name="callSettersOnNulls" value="true"/>
  <setting name="cacheEnabled" value="true"/>
  <setting name="lazyLoadingEnabled" value="true"/>
  <setting name="aggressiveLazyLoading" value="true"/>
  <setting name="multipleResultSetsEnabled" value="true"/>
  <setting name="useColumnLabel" value="true"/>
  <setting name="useGeneratedKeys" value="false"/>
  <setting name="autoMappingBehavior" value="PARTIAL"/>
  <setting name="defaultExecutorType" value="SIMPLE"/>
  <setting name="mapUnderscoreToCamelCase" value="true"/>
  <setting name="localCacheScope" value="SESSION"/>
  <setting name="jdbcTypeForNull" value="NULL"/>
 </settings>
 <typeAliases>
  <typeAlias alias="Integer" type="java.lang.Integer" />
  <typeAlias alias="Long" type="java.lang.Long" />
  <typeAlias alias="HashMap" type="java.util.HashMap" />
  <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
  <typeAlias alias="ArrayList" type="java.util.ArrayList" />
  <typeAlias alias="LinkedList" type="java.util.LinkedList" />
 </typeAliases>
</configuration>

這樣我們就配置好了Spring Boot和mybatis與mybatis自動生成mapper和實體的功能。這樣我們要生成某個表的實體和mapper的時候,只需要在generatorConfig.xml中javaClientGenerator標簽之后添加下面的內容即可。

此處我們已yd_movies表為例(主要數據庫表名字不要用復數,這個表名肯定是我之前腦子抽了,搞個復數的名字)

<!--生成對應表及類名-->
  <table tableName="yd_movies" domainObjectName="Movie" enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
   <columnOverride column="cover_photos" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="cover_photos_loc" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="pubdates" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="photographs_origin" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="photographs_origin_loc" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="languages" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="awards" javaType="java.lang.String" jdbcType="VARCHAR" />
   <columnOverride column="summary" javaType="java.lang.String" jdbcType="VARCHAR" />
  </table>
<!--columnOverride使用這個標簽是為了防止數據庫字段類型為text時候,mybatis會生成xxxxWithBLOBs文件。-->

然后點擊idea右側Maven projects,就可以看到mybatis-generate下的mybatis-generate:generate,雙擊mybatis-generate:generate即可。

**generator 注意點,也可以說是BUG

就是在生成的時候,在第二次生成的時候,dao 和entity 都會直接覆蓋,而Mapper.xml 會直接追加,導致運行報錯,而且很難找。**

4.mybatis自定義sql

當我們需要自定義sql的時候,我們不能把這些也放到mapper文件中。不然,當數據庫表的字段有更新需要重新生成mapper的時候,這樣我們自定義的sql不就被覆蓋沒了么!所以我們需要在MovieMapper.xml的同級目錄里,新建一個MovieMapperExt.xml文件,把自定義的sql放到這里,例子如下:

MovieMapperExt.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="com.dameiweb.learn.dao.MovieMapper" >
 <select id="selectByCondition" resultMap="BaseResultMap" parameterType="com.dameiweb.learn.dto.MovieRequest">
 SELECT yd_movies.* FROM yd_movies
 LEFT JOIN yd_r_movie_countries ON yd_movies.id = yd_r_movie_countries.movie_id
 LEFT JOIN yd_countries cou ON yd_r_movie_countries.country_id = cou.id
 LEFT JOIN yd_r_movie_category ON yd_movies.id = yd_r_movie_category.movie_id
 LEFT JOIN yd_category cat ON yd_r_movie_category.category_id = cat.id
 LEFT JOIN yd_r_movie_actor ON yd_movies.id = yd_r_movie_actor.movie_id
 LEFT JOIN yd_actors a ON yd_r_movie_actor.actor_id = a.id
 WHERE
 yd_movies.status = 1
 <if test="movieRequest.title != null">
  AND yd_movies.title LIKE "%"#{movieRequest.title}"%"
 </if>
 <if test="movieRequest.country != null">
  AND cou.id = #{movieRequest.country}
 </if>
 <if test="movieRequest.category != null">
  AND cat.id = #{movieRequest.category}
 </if>
 <if test="movieRequest.year != null">
  AND year = #{movieRequest.year}
 </if>
 <if test="movieRequest.rate != null">
  AND rate >= #{movieRequest.rate}
 </if>
 <if test="movieRequest.rate != null">
  AND rate < (#{movieRequest.rate}+1)
 </if>
 <if test="movieRequest.subtype != null">
  AND yd_movies.subtype = #{movieRequest.subtype}
 </if>
 GROUP BY yd_movies.id
 ORDER BY updated_at DESC, year
 </select>
</mapper>

總結

以上所述是小編給大家介紹的Spring Boot整合mybatis并自動生成mapper和實體,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

塘沽区| 宿州市| 鲁山县| 来宾市| 太白县| 奉贤区| 九江县| 灌南县| 沂水县| 嘉兴市| 苍南县| 高邑县| 若羌县| 南充市| 沅江市| 乌审旗| 金塔县| 横峰县| 八宿县| 尤溪县| 越西县| 新沂市| 广汉市| 涿州市| 鄂州市| 定结县| 衡水市| 安福县| 方城县| 兴安县| 衡南县| 海南省| 茂名市| 三穗县| 措勤县| 南木林县| 秦安县| 高清| 汶川县| 阳山县| 淮安市|