Springboot+mybatis
首先新建項目,

一直創建下去這里就不說了。。。
配置application.properties文件:
#配置端口號
server.port=8080
#配置數據源
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydata?useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
#mybatis
#配置mybatis的mapper.xml文件所在位置
mybatis.mapper-locations=classpath:com/example/springboot_mybaits01/mapper/*.xmls
pom里面添加
<dependency><!--Mysql的jdbc驅動包-->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<!--此處添加一個mysql-connector-java依賴可以防止找不到jdbc Driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<configuration>
<!-- mybatis用於生成代碼的配置文件 -->
<configurationFile>generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
逆向工程的xml文檔:
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> <context id="mybatis" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mydata" userId="root" password="123456"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL和NUMERIC類型解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類的位置,重要!! --> <javaModelGenerator targetPackage="com.example.springboot_mybaits01.pojo" targetProject="src/main/java"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數據庫返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置,重要!! --> <sqlMapGenerator targetPackage="com.example.springboot_mybaits01.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置,重要!! --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboot_mybaits01.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定數據庫表,要生成哪些表,就寫哪些表,要和數據庫中對應,不能寫錯! --> <table tableName="info"></table> </context> </generatorConfiguration>
mybatis的逆向工程基本的事情就已經做好了,接下來可以開始生產代碼
點擊旁邊的Maven

雙擊2


這樣就完成了
github:https://github.com/fzzzjjj/springboot_mybaits01
遇到過的問題:
依賴沒導入
project報紅:原因是jar導入失敗,要去倉庫把project報出錯的那個包,找到並且刪除。然后重新reimport
找不到jdbc.Driver包:在pom文件中已經解決
