mybatis-generator的使用(Maven方式)

mybatis-generator可以帮助我们自动建立代码,这是根据数据库的字段,然后通过反射机制,自动生成mapper,dao,mapping文件,以后就可以解脱双手去写这些代码了

这里先使用简单的方式(Maven方式)

一、准备数据库字段

 二、在eclipse中建立一个Maven项目

pom文件:

<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.jsoup</groupId>
  <artifactId>gen</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
   <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.7.3</version>
</dependency>
<dependency>  
<groupId>org.codehaus.jackson</groupId>  
<artifactId>jackson-mapper-asl</artifactId>  
<version>1.9.4</version>
</dependency>
 <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.35</version>
 </dependency>
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <dependencies>
 <dependency>
 <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies>
    <configuration>
 <!--配置文件的路径-->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> 
<overwrite>true</overwrite>
</configuration>
</plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

注: mysql-connector-java必须要做插件和依赖里都出现,不然会出错;mybatis-generator-maven-plugin必须放在后面的插件而不是前面,不然会报错:

[INFO] Scanning for projects...
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 KB at 4.5 

KB/sec)
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 KB at 4.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.270 s
[INFO] Finished at: 2019-04-14T13:37:37+08:00
[INFO] Final Memory: 7M/19M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'mybatis-generator' in the current project and in the plugin groups 

[org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\Administrator

\.m2\repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

三、在source下建立一个文件

内容如下: 

<?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 id="DB2Tables" > 
       <!-- 是否生成注释-->
        <commentGenerator>       
            <property name="suppressAllComments" value="true"/>    
        </commentGenerator>    
        <!--数据库链接URL,用户名、密码 -->    
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/jsoup" userId="root" password="123456">    
        </jdbcConnection>      
        <!-- 生成模型的包名和位置-->    
        <javaModelGenerator targetPackage="com.example.pojo" targetProject="src/main/java"/>   
        <!-- 生成映射文件的包名和位置-->    
        <sqlMapGenerator targetPackage="com.example.mapping" targetProject="src/main/java"/>   
        <!-- 生成DAO的包名和位置-->    
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java"/>    
        <!-- 要生成的表 名-->
        <table schema="" tableName="student_info" />
</context>
</generatorConfiguration>

四、 建立包名

建立空的包,只有名字

 注:这是有文件,是因为运行后已经生成的文件在里面

五、选中项目,右键进行Maven build

出现BUILD SUCCESS说明成功:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gen 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ gen ---
[INFO] Deleting G:\gen\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.652 s
[INFO] Finished at: 2019-04-14T14:03:28+08:00
[INFO] Final Memory: 5M/15M

刷新一下项目就能出现我们想要的结果(全部字段都是通过驼峰映射,效果很理想)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值