springboot创建项目并使用mybatis generator生成实体类

本文介绍了如何使用Eclipse和Maven快速创建一个SpringBoot Web项目,并结合MyBatis Generator来生成数据库对应的实体类。详细步骤包括配置数据库连接、设置MyBatis Generator的XML文件、配置`application.properties`以及修改`pom.xml`以添加相关依赖。通过运行Maven目标,可以自动生成UserDo和UserPasswordDo等实体类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用eclispe配合maven快速创建一个web项目,并且为其建立一个数据库

然后可以找到src文件夹下的main文件夹

打开main文件夹里面的java文件夹

新建dao文件夹、dataobject文件夹。

回退到main文件夹下面新建resources文件夹

resources文件夹下面在新建一个  mapping 文件夹、一个叫 application.properties的 file文件、一个叫 mybatis-generator.xml的xml文件

 

 

mybatis-generator.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>


    <!-- 一个数据库一个context -->
    <context id="infoGuardian">
        <!-- 注释 -->
        <commentGenerator >
            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://127.0.0.1:3306/miaosha" userId="root"
            password="" />

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->    
        <javaModelGenerator targetPackage="com.miaoshaproject.dataobject"
            targetProject="src/main/java" >
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="mapping"
            targetProject="src/main/resources" >
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成mapxml对应client,也就是接口dao -->    
        <javaClientGenerator targetPackage="com.miaoshaproject.dao"
            targetProject="src/main/java" type="XMLMAPPER" >
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 配置表信息 -->    
        <table  tableName="user_info" domainObjectName="UserDo" enableCountByExample="false"
         enableUpdateByExample="false" enableDeleteByExample="false"
          enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table  tableName="user_password" domainObjectName="UserPasswordDo" enableCountByExample="false"
         enableUpdateByExample="false" enableDeleteByExample="false"
          enableSelectByExample="false" selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

 

 

application.properties内容

 

server.port=8081
mybatis.mapperLocations=classpath:mapping/*.xml

spring.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha
spring.datasource.username=root
spring.datasource.password=

#use druid data source
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver

 

找到pom.xml文件,修改内容

<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.miaoshaproject</groupId>
  <artifactId>miaosha</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <!-- 引入依赖,源自于springboot官方文档Building a RESTful Web Service,Build with maven -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    
  <name>miaosha</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
      <!-- 引入依赖,源自于springboot官方文档Building a RESTful Web Service,Build with maven -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            
      <!-- Mysql依赖 -->
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.41</version>
          </dependency>            
          
      <!-- 链接池依赖 -->
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>druid</artifactId>
              <version>1.1.3</version>
          </dependency>        
            
      <!-- 对mybatis的支持 -->
          <dependency>
              <groupId>org.mybatis.spring.boot</groupId>
              <artifactId>mybatis-spring-boot-starter</artifactId>
              <version>1.3.1</version>
          </dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  
  <!-- 手动添加 -->
    <build>
        <plugins>
            <!-- 插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                      <groupId>org.mybatis.generator</groupId>
                      <artifactId>mybatis-generator-core</artifactId>
                      <version>1.3.5</version>
                    </dependency>
                    <dependency>
                      <groupId>mysql</groupId>
                      <artifactId>mysql-connector-java</artifactId>
                      <version>5.1.41</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>mybatis generator</id>
                        <phase>package</phase>
                        <goals>
                          <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                
                <configuration>
                    <!-- 允许移动生成文件 -->
                    <verbose>true</verbose>
                    <!-- 允许自动覆盖文件 -->
                    <overwrite>true</overwrite>
                    <configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

 

点击eclipse上方的run,run configuration

找到build maven设置goals为mybatis-generator:generate

base directory设置为工作空间下的项目目录

不要退出该窗口,点击run。

成功生成了实体类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值