前言:本来老师用的eclipse只需要安装插件,然后运行一个配置文件就行,由于本人对于IDEA的喜爱,只能说自愿入坑!!网上找了一大堆资料,一天一个报错,自己都整懵逼了,。。。。。
下面就跟着教程来吧,步骤尽量给大家弄详细点
1、创建maven项目(这个就不用说了吧,只是网上很多教程在下面红色箭头的地方会让你选某个选项,起始你可以不用选!!)
2、输入项目名,然后点next,然后finish
3、创建maven项目后,你的项目结构可能不会马上显示出来,这时候右下方会有一个improt changes,点击它,他会下载一些maven必须的架包,以后你只要对pom.xml进行修改,就会有这个选项记得点击导包
4、下载完后,在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>
<properties resource="db.properties"></properties>
<!--数据库驱动-->
<classPathEntry location="D:\apache-tomcat-8.5.38\lib\mysql-connector-java-5.1.36-bin.jar"/>
<context id="context1">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="%"> </table>
</context>
</generatorConfiguration>
注意:
1)、2)、 生成mapper,model文件夹的位置
5、创建连接数据库的资源文件db.properties,这里你只需要保证3306后面是你的胃数据库名字、用户名以及密码正确就行
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/maventest?characterEncoding=utf8
username=root
password=123456
6、基本文件都配好了(如果前面出现报红的情况,不要着急),现在就差运行generatorConfig.xml文件的Mybatis-generator插件了,但是eclipse只能需要通过配置来生成运行选项,所以找到pom.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.hd</groupId>
<artifactId>lastMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--这个东西就是需要运行的文件 !!!!!!!!!!!!!!!!!!!!!!-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
7、现在看到图上的位置,双击它
不出意外的话你在下方应该就能够看见一个BUILD SUCCESS的字样
如果有说明你已经成功了…一半!!
8、现在看项目结构
如果最后你的目录结构和我的差不多,基本上没有任何问题了,但是mapper文件夹中两种类型的文件,一种相当于是Dao的接口,xml则是其实现类,在elipse中放在一起是完全没有问题的,但是放在IDEA中便会有意想不到的错误。由于时间原因,我会在下一篇文章讲述如何避免这个错误以及用mapper.xml文件来操作数据库。
下一篇:https://blog.csdn.net/qq_38261445/article/details/91345972
PS:如果上面的操作有什么不正确的地方或者有什么地方没有理清楚,欢迎留言指正,一起交流,谢谢