电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:6日期: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
相關文章:
主站蜘蛛池模板: 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 物联网卡_物联网卡购买平台_移动物联网卡办理_移动联通电信流量卡通信模组采购平台? | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 北京燃气公司 用户服务中心| 影视模板素材_原创专业影视实拍视频素材-8k像素素材网 | 衢州装饰公司|装潢公司|办公楼装修|排屋装修|别墅装修-衢州佳盛装饰 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 金联宇电缆|广东金联宇电缆厂家_广东金联宇电缆实业有限公司 | 红酒招商加盟-葡萄酒加盟-进口红酒代理-青岛枞木酒业有限公司 | 烽火安全网_加密软件、神盾软件官网 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 电动不锈钢套筒阀-球面偏置气动钟阀-三通换向阀止回阀-永嘉鸿宇阀门有限公司 | 万濠投影仪_瑞士TRIMOS高度仪_尼康投影仪V12BDC|量子仪器 | 食品质构分析仪-氧化诱导分析仪-瞬态法导热系数仪|热冰百科 | 液压中心架,数控中心架,自定心中心架-烟台恒阳机电设计有限公司 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 | 建筑消防设施检测系统检测箱-电梯**检测仪器箱-北京宇成伟业科技有限责任公司 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 体坛网_体坛+_体坛周报新闻客户端| 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 厂房出租_厂房出售_产业园区招商_工业地产&nbsp;-&nbsp;中工招商网 | 南京雕塑制作厂家-不锈钢雕塑制作-玻璃钢雕塑制作-先登雕塑厂 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | loft装修,上海嘉定酒店式公寓装修公司—曼城装饰 | 同步带轮_同步带_同步轮_iHF合发齿轮厂家-深圳市合发齿轮机械有限公司 | CNC机加工-数控加工-精密零件加工-ISO认证厂家-鑫创盟 | 主题班会网 - 安全教育主题班会,各类主题班会PPT模板 | 扒渣机厂家_扒渣机价格_矿用扒渣机_铣挖机_撬毛台车_襄阳永力通扒渣机公司 | 福州甲醛检测-福建室内空气检测_环境检测_水质检测-福建中凯检测技术有限公司 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 成都治疗尖锐湿疣比较好的医院-成都治疗尖锐湿疣那家医院好-成都西南皮肤病医院 | 房车价格_依维柯/大通/东风御风/福特全顺/江铃图片_云梯搬家车厂家-程力专用汽车股份有限公司 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! |