当前位置: 首页 > news >正文

网站域名和网址网络架构 书籍

网站域名和网址,网络架构 书籍,被窝家装公司,哈尔滨做网站的价格在现代企业级开发中#xff0c;使用Spring和MyBatis进行快速、高效的数据库操作是非常常见的。本文将深入探讨如何使用Spring和MyBatis进行逆向工程#xff0c;帮助开发者自动生成数据库相关的代码#xff0c;提高开发效率和代码质量。 一、什么是逆向工程 逆向工程是指从…在现代企业级开发中使用Spring和MyBatis进行快速、高效的数据库操作是非常常见的。本文将深入探讨如何使用Spring和MyBatis进行逆向工程帮助开发者自动生成数据库相关的代码提高开发效率和代码质量。 一、什么是逆向工程 逆向工程是指从数据库表结构自动生成对应的Java实体类、Mapper接口和XML映射文件的过程。这种方法能够大大减少手动编写代码的时间提高开发效率减少人为错误。MyBatis提供了强大的逆向工程工具MyBatis GeneratorMBG结合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.xMySQL数据库或其他数据库 2. 项目依赖 在Maven项目的 pom.xml中添加以下依赖 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.10/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.10/version/dependencydependencygroupIdorg.mybatis.spring/groupIdartifactIdmybatis-spring/artifactIdversion2.0.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.25/version/dependencydependencygroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-core/artifactIdversion1.4.0/version/dependency /dependencies ​四、MyBatis Generator配置 创建 generatorConfig.xml文件用于配置MyBatis Generator ?xml version1.0 encodingUTF-8? !DOCTYPE generatorConfigurationPUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtd generatorConfigurationcontext idMySqlContext targetRuntimeMyBatis3jdbcConnection driverClasscom.mysql.cj.jdbc.DriverconnectionURLjdbc:mysql://localhost:3306/your_databaseuserIdyour_usernamepasswordyour_password/javaModelGenerator targetPackagecom.example.model targetProjectsrc/main/java/sqlMapGenerator targetPackagecom.example.mapper targetProjectsrc/main/resources/javaClientGenerator typeXMLMAPPER targetPackagecom.example.mapper targetProjectsrc/main/java/table tableNameyour_table domainObjectNameYourEntity//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 version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdbean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/your_database/property nameusername valueyour_username/property namepassword valueyour_password//beanbean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/property namemapperLocations valueclasspath*:com/example/mapper/*.xml//beanbean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.example.mapper/property namesqlSessionFactoryBeanName valuesqlSessionFactory//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 namespacecom.example.mapper.YourEntityMapperdelete iddeleteByPrimaryKey parameterTypejava.lang.IntegerDELETE FROM your_table WHERE id #{id}/deleteinsert idinsert parameterTypecom.example.model.YourEntityINSERT INTO your_table (name) VALUES (#{name})/insertselect idselectByPrimaryKey resultTypecom.example.model.YourEntity parameterTypejava.lang.IntegerSELECT id, name FROM your_table WHERE id #{id}/selectupdate idupdateByPrimaryKey parameterTypecom.example.model.YourEntityUPDATE 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.hkea.cn/news/14587361/

相关文章:

  • 免费建自己的网站赚钱周口在线网站建设
  • 关键词百度指数查询长沙优化排名推广
  • 西安 网站建设 费用网站建设实现后台数据导出excel
  • 万家建设有限公司网站重庆南岸营销型网站建设公司哪家好
  • 网站怎么放404页面网站建设搜索
  • 凡科网站建设7个基本流程腾讯域名怎么建设网站
  • 湘潭做网站 i磐石网络关于网站建设的可行性报告
  • 网站建设的建议例子网易企业邮箱手机上登录不了
  • 定制企业网站建设哪家好网店美工课程总结
  • 免费开源网站建设系统动物网站建设
  • 网站增长期怎么做店铺网络推广方案
  • 好的网站建设公司有哪些wordpress找不到后台
  • 怎样创建网站dw如何做网络营销推广就属金手指饣
  • 刚做的网站适合做外链吗深圳公司网络推广该怎么做
  • 自己做网站还有出路吗企业网站怎么扣费的
  • 检测网站是否安全广州越秀区酒店推荐
  • wordpress超炫模板南宁seo规则
  • 娄底网站推广农业电商网站建设方案
  • 网站建设毕业设计 任务书企业网站设计中常见的排版类型
  • 国外界面设计网站军博网站建设公司
  • 怎么创建私人网站创作图片的软件
  • 当当网站建设优点论坛网站备案流程图
  • 营销型网站案例分析备案用的网站建设规划书怎么写
  • 潍坊网站建设建站wordpress相册展示
  • 国外的云服务器租用中国seo关键词优化工具
  • 中英文企业网站php源码wordpress背景特效
  • 石家庄建站凡科商标注册查询网官网查询
  • 宜宾网站设计衡水网站建设在哪里
  • 网站程序设计软件mysql网站数据库
  • 中国建设银行u盾官方网站产品策划书范文案例