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

 更新时间:2019年01月03日 09:48:33   作者:staHuri   我要评论

今天小编就为大家分享一篇关于在SpringBoot项目中利用maven的generate插件,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

使用maven 插件 generate生成MyBatis相关文件

在项目中增加 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驱动-->
    <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 用来指定链接驱动
  • 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

此项内容为直接修改相关配置即可

<?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>
  <!-- 本地数据库驱动程序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>
    <!-- 数据库的相关配置 -->
    <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>
    <!-- 相关表的配置-->
    <table tableName="t"
        domainObjectName="Tq"
        enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false"
    />
  </context>
</generatorConfiguration>

创建相关目录

查看插件

执行

选中maven 下 generator 双击运行

SpringBootMyBatis 使用

在 service 的实现类上添加 @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;
  }
}

相关调用

@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
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

最新评论