溫馨提示×

溫馨提示×

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

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

在SpringBoot項目中利用maven的generate插件

發(fā)布時間:2020-10-07 13:11:05 來源:腳本之家 閱讀:268 作者:staHuri 欄目:編程語言

使用maven 插件 generate生成MyBatis相關(guān)文件

在項目中增加 maven 依賴

  1. - mybatis-spring-boot-starter
  2. - mysql-connector-java
  3. - mybatis-generator-maven-plugin 插件 自動讀取 resources 下的generatorConfig.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wangSpringBoot</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.18</version>
      <scope>provided</scope>
    </dependency>
    <!--熱部署-->
    <dependency>
<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
    <!--test-->
    <dependency>
<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!--MyBatis-->
    <dependency>
<groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!--Mysql JDBC驅(qū)動-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
        </configuration>
      </plugin>
      <plugin>
<groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.7</version>
        <executions>
          <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
            <version>2.3.4</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
<directory>src/main/resources</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>
      <resource>
<directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>

SpringBoot 項目中application.properties 配置

  • mybatis.mapper-locations 用來指定mapper 存放路徑
  • spring.datasource.username 用來指定 用戶名
  • spring.datasource.password 用來指定密碼
  • spring.datasource.driver-class-name 用來指定鏈接驅(qū)動
  • spring.datasource.url 用來指定鏈接路由地址
mybatis.mapper-locations=classpath:com/wangspringboot/demo/mapper/*.xml
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/wt?useUnicode=true&characterEncoding=utf8&useSSL=false

resources 下配置 generatorConfig.xml

此項內(nèi)容為直接修改相關(guān)配置即可

<?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>
  <!-- 本地數(shù)據(jù)庫驅(qū)動程序jar包的全路徑 -->
  <classPathEntry
      location="D:\Program Files\MavenRepository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
  <context id="context" targetRuntime="MyBatis3">
    <commentGenerator>
      <property name="suppressAllComments" value="false"/>
      <property name="suppressDate" value="true"/>
    </commentGenerator>
    <!-- 數(shù)據(jù)庫的相關(guān)配置 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/wt" userId="root"
            password="root"/>
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!-- 實體類生成的位置 -->
    <javaModelGenerator targetPackage="com.wangspringboot.demo.model" targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
      <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!-- *Mapper.xml 文件的位置 -->
    <sqlMapGenerator targetPackage="com.wangspringboot.demo.mapper" targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator>
    <!-- Mapper 接口文件的位置 -->
    <javaClientGenerator targetPackage="com.wangspringboot.demo.mapper" targetProject="src/main/java"
               type="XMLMAPPER">
      <property name="enableSubPackages" value="false"/>
    </javaClientGenerator>
    <!-- 相關(guān)表的配置-->
    <table tableName="t"
        domainObjectName="Tq"
        enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false"
    />
  </context>
</generatorConfiguration>

創(chuàng)建相關(guān)目錄

在SpringBoot項目中利用maven的generate插件

查看插件

在SpringBoot項目中利用maven的generate插件

執(zhí)行

選中maven 下 generator 雙擊運行

在SpringBoot項目中利用maven的generate插件

SpringBootMyBatis 使用

在 service 的實現(xiàn)類上添加 @Service 注解

@Service
public class TqServiceImpl implements TqService {
  @Autowired
  private TqMapper tqmapper;
  @Override
  public Tq insTq() {
    Tq t = new Tq();
    t.setZ(12.0);
    t.setY(12.0);
    t.setX(12.0);
    tqmapper.insert(t);
    return t;
  }
}

相關(guān)調(diào)用

@RestController
public class MyBatisController {
  @Autowired
  private TqService tqService;
  @GetMapping("/boot/tq")
  public Object tq(){
    return tqService.insTq();
  }
}

啟動SpringBoot WEB項目后 訪問

{
x: 12,
y: 12,
z: 12
}

在SpringBoot項目中利用maven的generate插件

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

向AI問一下細節(jié)

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

AI