评论

收藏

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

编程语言 编程语言 发布于:2021-10-06 12:09 | 阅读数:230 | 评论:0

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

  • - mybatis-spring-boot-starter
  • - mysql-connector-java
  • - 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>
创建相关目录
DSC0000.png

查看插件
DSC0001.png

执行
选中maven 下 generator 双击运行
DSC0002.png

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
}
DSC0003.png

总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/staHuri/article/details/81097069

关注下面的标签,发现更多相似文章