spring和Mybatis的逆向工程

news/2025/2/1 22:36:00/文章来源:https://www.cnblogs.com/she20250124/p/18696548

在现代企业级开发中,使用Spring和MyBatis进行快速、高效的数据库操作是非常常见的。本文将深入探讨如何使用Spring和MyBatis进行逆向工程,帮助开发者自动生成数据库相关的代码,提高开发效率和代码质量。

一、什么是逆向工程

逆向工程是指从数据库表结构自动生成对应的Java实体类、Mapper接口和XML映射文件的过程。这种方法能够大大减少手动编写代码的时间,提高开发效率,减少人为错误。MyBatis提供了强大的逆向工程工具MyBatis Generator(MBG),结合Spring,可以实现快速开发。

二、Spring和MyBatis简介

1. Spring

Spring是一个开源的Java开发框架,提供全面的基础设施支持,包括依赖注入(DI)、面向切面编程(AOP)和数据访问框架。Spring与MyBatis的整合可以通过Spring提供的 SqlSessionFactoryBean和 MapperScannerConfigurer等类实现。

2. MyBatis

MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射。MyBatis相比Hibernate更加灵活和轻量级,特别适合复杂查询的应用场景。

三、逆向工程的准备工作

1. 环境配置

确保已经安装了以下环境:

  • JDK 1.8或以上版本
  • Maven 3.x
  • MySQL数据库(或其他数据库)

2. 项目依赖

在Maven项目的 pom.xml中添加以下依赖:

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.10</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.10</version></dependency><dependency><groupId>org.mybatis.spring</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.4.0</version></dependency>
</dependencies>
​
 
 

四、MyBatis Generator配置

创建 generatorConfig.xml文件,用于配置MyBatis Generator:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><context id="MySqlContext" targetRuntime="MyBatis3"><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/your_database"userId="your_username"password="your_password"/><javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"/><sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"/><javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"/><table tableName="your_table" domainObjectName="YourEntity"/></context>
</generatorConfiguration>
​
 
 

五、运行MyBatis Generator

在Maven项目中运行MyBatis Generator命令:

mvn mybatis-generator:generate
​
 
 

这将根据 generatorConfig.xml配置文件生成对应的Java实体类、Mapper接口和XML映射文件。

六、Spring与MyBatis的整合

1. Spring配置文件

在 applicationContext.xml中配置Spring和MyBatis:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/your_database"/><property name="username" value="your_username"/><property name="password" value="your_password"/></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath*:com/example/mapper/*.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.example.mapper"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean></beans>
​
 
 

七、编写业务逻辑

1. 实体类

在 src/main/java/com/example/model/YourEntity.java中自动生成的实体类:

public class YourEntity {private Integer id;private String name;// getters and setters
}
​
 
 

2. Mapper接口

在 src/main/java/com/example/mapper/YourEntityMapper.java中自动生成的Mapper接口:

public interface YourEntityMapper {int deleteByPrimaryKey(Integer id);int insert(YourEntity record);YourEntity selectByPrimaryKey(Integer id);int updateByPrimaryKey(YourEntity record);
}
​
 
 

3. XML映射文件

在 src/main/resources/com/example/mapper/YourEntityMapper.xml中自动生成的XML文件:

<mapper namespace="com.example.mapper.YourEntityMapper"><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">DELETE FROM your_table WHERE id = #{id}</delete><insert id="insert" parameterType="com.example.model.YourEntity">INSERT INTO your_table (name) VALUES (#{name})</insert><select id="selectByPrimaryKey" resultType="com.example.model.YourEntity" parameterType="java.lang.Integer">SELECT id, name FROM your_table WHERE id = #{id}</select><update id="updateByPrimaryKey" parameterType="com.example.model.YourEntity">UPDATE your_table SET name = #{name} WHERE id = #{id}</update>
</mapper>
​
 
 

4. 服务层

在 src/main/java/com/example/service/YourEntityService.java中编写服务层代码:

import com.example.mapper.YourEntityMapper;
import com.example.model.YourEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class YourEntityService {@Autowiredprivate YourEntityMapper yourEntityMapper;public YourEntity getYourEntityById(Integer id) {return yourEntityMapper.selectByPrimaryKey(id);}public void saveYourEntity(YourEntity yourEntity) {if (yourEntity.getId() == null) {yourEntityMapper.insert(yourEntity);} else {yourEntityMapper.updateByPrimaryKey(yourEntity);}}public void deleteYourEntityById(Integer id) {yourEntityMapper.deleteByPrimaryKey(id);}
}
​
 
 

八、运行和测试

通过JUnit或Spring的测试框架测试逆向工程生成的代码,确保其能够正常工作

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/877698.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MyBatis之作用域和生命周期

弄清楚SqlSessionFactoryBuild,SqlSessionFactory,SqlSession的作用域和生命周期,可以避免高并发程序中的资源浪费 具体知识点看狂神的两张ppt就行了

CF856C 题解

很厉害的排列计数0 原题链接:luogu & CF 在任务清单里放了半年,今天终于做出来了 qwq,不得不写题解了。 1 能被 \(11\) 整除的数长什么样子?它的奇数位之和与偶数位之和应当模 \(11\) 同余。 我们不妨把一个数的价值定为从前往后奇数位之和减去偶数位之和模 \(11\) 的值…

人生不止于职业发展

0 你的问题,我知道! 工作意义是啥?职业发展在人生啥角色? 1 工作意义 农村人努力学习考上大学,得好工作,为逃离同村同龄人十几岁就工厂打工命运,过不凡人生,实现改命的唯一途径。毕业就进入自带光环的大厂,有份让所有亲戚羡慕的公司和薪水。我认为工作价值是让自己自立…

『Python底层原理』--CPython如何编译代码

前一篇我们介绍了CPython VM的运行机制,它基于一系列字节码指令来实现程序逻辑。 不过,Python字节码在完整描述代码功能上存在局限性,于是代码对象应运而生。像模块、函数这类代码块的执行,本质上就是对应代码对象的运行,代码对象涵盖了字节码、常量、变量名以及各类属性信…

MyBatis之jdbc属性外部配置

将jdbc数据库连接属性写在db.properties中,如图 然然后在配置文件中引入

25.2.1小记

Object类Object类中自带的toString和equals函数(默认比较管理者是否管理相同的对象,可以通过对子类函数的重构实现正常比较) // @Override//重写,编译器会默认构造类型检查public boolean equals(Object obj) {//向下造型CD cc = (CD)obj;return artist.equals(cc.artist…

“尝试一下挣钱的辛苦”之装师

我尝试了把我做的兽头卖出去,记录一下从孩子出生到找到妈咪领养的过程 因为没有太多预算,所以兽设没有找画师,我自己构思的;整个制作过程都要用到热熔胶,很容易烫到>_<(热熔胶——手作娘离不开的东西),梳理毛布也很让人头疼,弄得房间里都是毛毛,虽然但是,孩子…

“尝试一下挣钱的辛苦”

我尝试了把我做的兽头卖出去,记录一下从孩子出生到找到妈咪领养的过程 因为没有太多预算,所以兽设没有找画师,我自己构思的;整个制作过程都要用到热熔胶,很容易烫到>_<(热熔胶——手作娘离不开的东西),梳理毛布也很让人头疼,弄得房间里都是毛毛,虽然但是,孩子…

VScode使用插件open-in-browser在默认浏览器中打开html文件

1. vscode extension中搜索open in browser,并下载。 我下载的第一个2. 打开.html文件 alt+shift+b手动选择特定浏览器打开 alt+b用默认浏览器打开,如果没有设置默认浏览器,可能出现以下问题3. 配置默认浏览器 ctrl+shift+p打开command palette 输入settings.json,打开Open…

在MacOS上安装sqllite

参考教程 https://www.runoob.com/sqlite/sqlite-installation.html 1.下载sqllite安装包 https://www.sqlite.org/2025/sqlite-autoconf-3480000.tar.gz wget https://www.sqlite.org/2025/sqlite-autoconf-3480000.tar.gz tar -xvzf sqlite-autoconf-3480000.tar.gz cd sqlit…

【译】MongoDB EF Core 提供程序:有什么新功能?

原文 | Rishit, Luce 翻译 | 郑子铭 这是 Rishit Bhatia 和 Luce Carter 的客座文章。Rishit 是 MongoDB 的高级产品经理,专注于 .NET 开发人员体验,在进入产品管理部门之前,他已经使用 C# 工作多年。Luce 是 MongoDB 的开发倡导者、Microsoft MVP,热爱代码、阳光和学习。本…

06. 文件权限

一、文件属性Linux 系统是一个典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限。为了保证系统的安全性,Linux 系统对不同的访问用户访问同一个文件(包括目录文件)的权限做了不同的规定。在 Linux 中,我们可以使用 ll 或者 ls -l 命令来显示一个文件的属性以及…