新建一個SpringBoot項目
1.首先在Maven的官網(wǎng)上下載Maven的包室埋,建立一個本地的maven倉庫
2.再在idea中的Setting中設(shè)置maven的本地路徑以及本地倉庫路徑
3.新建一個Maven項目明棍,選擇quickstart下面的release
4.修改解壓后maven包中conf中的setting,添加相應(yīng)的mirror芋类,這里這樣做的原因是maven在下載包時默認(rèn)是走的國外的網(wǎng)站,這樣下載就會很慢,所以添加一些國內(nèi)下載的倉庫鏡像惕橙,比如阿里的mirror之類的雇庙。
5.打開pom.xml谓形,添加springboot相關(guān)的依賴,包括spring-boot-starter-web疆前、mysql-connector-java寒跳、druid、mybatis-spring-boot-starter這些包的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
自動生成mapper等文件
使用mybatis-generator自動生成代碼峡继,這時再配置文件中需要加入的是一個插件:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</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>
再通過建立一個mybatis-generator.xml文件冯袍,配置其對應(yīng)的jdbc的連接,以及生成的數(shù)據(jù)類的存放位置碾牌、映射文件xml的存放位置康愤、mapper文件接口的存放位置
<?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" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/loveconveniece?serverTimezone=GMT"
userId="root"
password="root">
</jdbcConnection>
<!--生成DataObject類存放位置-->
<javaModelGenerator targetPackage="com.muchen.DataObject" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放的位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Dao類的存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.muchen.Dao" targetProject="src/main/java">
<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>
<table tableName="store" domainObjectName="StoreDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="orders" domainObjectName="OrdersDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="goods_info" domainObjectName="GoodsDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
配置maven命令,通過mybatis-generator:generate命令執(zhí)行舶吗,創(chuàng)建出有關(guān)的文件征冷。
過程中的錯誤情況
mysql時區(qū)問題
由于我的mysql是直接下載的解壓包,在時區(qū)上會默認(rèn)為美國的時間誓琼,所以需要調(diào)整時區(qū)检激,在jdbc的連接的后面加上一個參數(shù)serverTimezone=GMT,否則在運行時腹侣,與mysql對接時會報錯叔收。
mysql版本問題
我的mysql的版本是下的最新的版本5.8版本,其mysql-connector-java是應(yīng)該是8.0.11版本的傲隶,之前的5.1.14版本的跟mysql5.8版本對接有問題饺律,在加密方式上有不同,所以兩處的版本都應(yīng)該改成8.0.11版本