亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

使用Spring Boot Mybatis 搞反向工程的步驟

瀏覽:4日期:2023-07-26 15:32:05
1. 拷貝 Mybatis 反向工程配置文件到項目的根目錄下

使用Spring Boot Mybatis 搞反向工程的步驟

2. 根據項目及表的情況,修改 GeneratorMapper.xml 配置 如果使用 高版本 , 驅動類變為:com.mysql.cj.jdbc.Driver url 后面應該加屬性 nullCatalogMeansCurrent=true ,否則生成有問題

當前版本 MySQL 數據庫為 5.7主要根據注釋來修改自己的內容

<?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> <!-- 指定連接數據庫的 JDBC 驅動包所在位置,指定到你本機的完整路徑 --> <classPathEntry location='E:Javatoolmaven_repositorymysqlmysql-connector-java5.1.9mysql-connector-java-5.1.9.jar'/> <!-- 配置 table 表信息內容體,targetRuntime 指定采用 MyBatis3 的版本 --> <context targetRuntime='MyBatis3'> <!-- 抑制生成注釋,由于生成的注釋都是英文的,可以不讓它生成 --> <commentGenerator> <property name='suppressAllComments' value='true'/> </commentGenerator> <!-- 配置數據庫連接信息 --> <jdbcConnection driverClass='com.mysql.jdbc.Driver' connectionURL='jdbc:mysql://localhost:3306/springboot' userId='root' password='123456'> </jdbcConnection> <!-- 生成 model 類,targetPackage 指定 model 類的包名, targetProject 指定 生成的 model 放在 IDEA 的哪個工程下面--> <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 的哪個工程下面 --> <sqlMapGenerator targetPackage='com.md.springboot.mapper' targetProject='src/main/java'> <property name='enableSubPackages' value='false'/> </sqlMapGenerator> <!-- 生成 MyBatis 的 Mapper 接口類文件,targetPackage 指定 Mapper 接口類的包 名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪個工程下面 --> <javaClientGenerator type='XMLMAPPER'targetPackage='com.md.springboot.mapper' targetProject='src/main/java'> <property name='enableSubPackages' value='false'/> </javaClientGenerator> <!-- 數據庫表名及對應的 Java 模型類名,有幾個表寫幾個table --> <table tableName='t_student' domainObjectName='Student' enableCountByExample='false' enableUpdateByExample='false' enableDeleteByExample='false' enableSelectByExample='false' selectByExampleQueryId='false'/> </context> </generatorConfiguration>

此時會報錯,如下

使用Spring Boot Mybatis 搞反向工程的步驟

這個時候可以不用理會,項目也是會正常運行的

Spring Boot 理論+實戰系列教程大家看這個:

3. 在pom.xml 文件中添加 mysql 反向工程依賴

<build> <plugins> <!--mybatis 代碼自動生成插件--> <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> 4. 雙擊生成相關文件

使用Spring Boot Mybatis 搞反向工程的步驟

5. 生成的文件

自動生成model/Student、實體類以及StudentMapper,接口StudentMapper.xml 具體對數據庫的操作這樣方便我們使用,具體的下面詳細介紹,注意看注釋

使用Spring Boot Mybatis 搞反向工程的步驟

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'> <!-- 數據庫字段名稱 實體對象屬性名稱 user_name userName user_age userAge --> <!-- 如果數據表中的字段是多個單詞構成的,通過Mybatis逆向工程生成的對象屬性名稱 會按照駝峰命名法的規則生成屬性名稱 自己設計數據表的時候,多個單詞之前使用下劃線分隔 --> <!-- resultMap的作用 1. 當數據庫中的字段名稱和實體類對象的屬性名不一致,可以進行轉換 2. 當前查詢的結果對象沒有對應一個表時,可以自定義一個結果集 --> <resultMap type='com.md.springboot.model.Student'> <!-- id標簽只能修飾主鍵字段,result標簽修飾其他字段 column 數據庫中的字段名稱 property 映射對象的屬性名稱 jdbcType 對應的類型 --> <id column='id' jdbcType='INTEGER' property='id' /> <result column='name' jdbcType='VARCHAR' property='name' /> <result column='age' jdbcType='INTEGER' property='age' /> </resultMap> <!--sql語句片段,將公共部分抽出--> <sql id='Base_Column_List'> id, name, age </sql> <select parameterType='java.lang.Integer' resultMap='BaseResultMap'> select <include refid='Base_Column_List' /> from t_student where id = #{id,jdbcType=INTEGER} </select> <delete parameterType='java.lang.Integer'> delete from t_student where id = #{id,jdbcType=INTEGER} </delete> <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> <!--動態sql--> <insert 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 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 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>

以上就是使用Spring Boot Mybatis 搞反向工程的步驟的詳細內容,更多關于Spring Boot Mybatis 搞反向工程的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产免费啪视频观看网站 | 黄色毛片免费进入 | 男人的天堂久久精品激情 | 国产精品亚欧美一区二区三区 | 99久久综合狠狠综合久久aⅴ | 国产chinese视频在线观看 | 亚洲淫片 | 日韩美女一区 | 国产精品亚洲va在线观看 | 免费黄色片视频 | 国内精品福利爱丝视频_速发 | 国产一区二区三区四区五区六区 | 亚洲国产精品一区二区三区久久 | 亚洲一二三区久久五月天婷婷 | 成人污污视频 | 欧美精品国产制服第一页 | 欧美日本一级在线播放 | 国产精品福利久久2020 | 国产农村1级毛片 | 一本色道久久综合亚洲精品加 | 国产成 人 综合 亚洲网 | 精品欧美一区二区在线观看 | 2021国产精品一区二区在线 | 免费va国产高清不卡大片 | 免费观看性欧美毛片 | 超级97碰碰碰碰久久久久最新 | 中文一区二区视频 | 精品国产一区二区三区久久 | www在线视频在线播放 | 久久97久久97精品免视看秋霞 | 欧美xxxx性疯狂bbbb | 福利国产视频 | 一本到不卡 | 尤物精品在线观看 | 亚洲美女啪啪 | 亚洲精品中文字幕乱码一区二区 | 日韩精品一区二区三区不卡 | a级黄色免费 | 生活毛片| 美女毛片在线 | 成人免费视频无遮挡在线看 |