使用Spring Boot Mybatis 怎么實(shí)現(xiàn)一個(gè)反向工程?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
如果使用 高版本 , 驅(qū)動(dòng)類變?yōu)椋篶om.mysql.cj.jdbc.Driver
url 后面應(yīng)該加屬性 nullCatalogMeansCurrent=true ,否則生成有問(wèn)題
當(dāng)前版本 MySQL 數(shù)據(jù)庫(kù)為 5.7
主要根據(jù)注釋來(lái)修改自己的內(nèi)容
<?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> <!-- 指定連接數(shù)據(jù)庫(kù)的 JDBC 驅(qū)動(dòng)包所在位置,指定到你本機(jī)的完整路徑 --> <classPathEntry location="E:\Java\tool\maven_repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar"/> <!-- 配置 table 表信息內(nèi)容體,targetRuntime 指定采用 MyBatis3 的版本 --> <context id="tables" targetRuntime="MyBatis3"> <!-- 抑制生成注釋,由于生成的注釋都是英文的,可以不讓它生成 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 配置數(shù)據(jù)庫(kù)連接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot" userId="root" password="123456"> </jdbcConnection> <!-- 生成 model 類,targetPackage 指定 model 類的包名, targetProject 指定 生成的 model 放在 IDEA 的哪個(gè)工程下面--> <javaModelGenerator targetPackage="com.md.springboot.model" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="false"/> </javaModelGenerator> <!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的 包名, targetProject 指定生成的 mapper.xml 放在 IDEA 的哪個(gè)工程下面 --> <sqlMapGenerator targetPackage="com.md.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成 MyBatis 的 Mapper 接口類文件,targetPackage 指定 Mapper 接口類的包 名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪個(gè)工程下面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.md.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 數(shù)據(jù)庫(kù)表名及對(duì)應(yīng)的 Java 模型類名,有幾個(gè)表寫幾個(gè)table --> <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>
此時(shí)會(huì)報(bào)錯(cuò),如下
這個(gè)時(shí)候可以不用理會(huì),項(xiàng)目也是會(huì)正常運(yùn)行的
Spring Boot 理論+實(shí)戰(zhàn)系列教程大家看這個(gè):
<build> <plugins> <!--mybatis 代碼自動(dòng)生成插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <!--配置文件的位置--> <configurationFile>GeneratorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
自動(dòng)生成model/Student、實(shí)體類
以及StudentMapper,接口
StudentMapper.xml 具體對(duì)數(shù)據(jù)庫(kù)的操作
這樣方便我們使用,具體的下面詳細(xì)介紹,注意看注釋
Student
package com.md.springboot.model; public class Student { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
StudentMapper
package com.md.springboot.mapper; import com.md.springboot.model.Student; public interface StudentMapper { int deleteByPrimaryKey(Integer id); int insert(Student record); int insertSelective(Student record); Student selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Student record); int updateByPrimaryKey(Student record); }
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.md.springboot.mapper.StudentMapper"> <!-- 數(shù)據(jù)庫(kù)字段名稱 實(shí)體對(duì)象屬性名稱 user_name userName user_age userAge --> <!-- 如果數(shù)據(jù)表中的字段是多個(gè)單詞構(gòu)成的,通過(guò)Mybatis逆向工程生成的對(duì)象屬性名稱 會(huì)按照駝峰命名法的規(guī)則生成屬性名稱 自己設(shè)計(jì)數(shù)據(jù)表的時(shí)候,多個(gè)單詞之前使用下劃線分隔 --> <!-- resultMap的作用 1. 當(dāng)數(shù)據(jù)庫(kù)中的字段名稱和實(shí)體類對(duì)象的屬性名不一致,可以進(jìn)行轉(zhuǎn)換 2. 當(dāng)前查詢的結(jié)果對(duì)象沒(méi)有對(duì)應(yīng)一個(gè)表時(shí),可以自定義一個(gè)結(jié)果集 --> <resultMap id="BaseResultMap" type="com.md.springboot.model.Student"> <!-- id標(biāo)簽只能修飾主鍵字段,result標(biāo)簽修飾其他字段 column 數(shù)據(jù)庫(kù)中的字段名稱 property 映射對(duì)象的屬性名稱 jdbcType 對(duì)應(yīng)的類型 --> <id column="id" jdbcType="INTEGER" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="age" jdbcType="INTEGER" property="age" /> </resultMap> <!--sql語(yǔ)句片段,將公共部分抽出--> <sql id="Base_Column_List"> id, name, age </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_student where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from t_student where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.md.springboot.model.Student"> insert into t_student (id, name, age ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER} ) </insert> <!--動(dòng)態(tài)sql--> <insert id="insertSelective" parameterType="com.md.springboot.model.Student"> insert into t_student <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="name != null"> name, </if> <if test="age != null"> age, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="age != null"> #{age,jdbcType=INTEGER}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.md.springboot.model.Student"> update t_student <set> <if test="name != null"> name = #{name,jdbcType=VARCHAR}, </if> <if test="age != null"> age = #{age,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.md.springboot.model.Student"> update t_student set name = #{name,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> </mapper>
看完上述內(nèi)容,你們掌握使用Spring Boot Mybatis 怎么實(shí)現(xiàn)一個(gè)反向工程的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文標(biāo)題:使用SpringBootMybatis怎么實(shí)現(xiàn)一個(gè)反向工程-創(chuàng)新互聯(lián)
鏈接分享:http://jinyejixie.com/article10/dsejdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站制作、服務(wù)器托管、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容