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

app网站开发学习服务之家网站推广公司

app网站开发学习,服务之家网站推广公司,网站建设 排名宝下拉,万网建网站MyBatis Generator附批量操作分页查询存储过程 Generator 介绍网址#xff1a;Introduction to MyBatis Generator Generator #xff0c;一个用于 MyBatis 的代码生成工具#xff0c;可以根据数据库表结构自动生成对应的实体类、DAO 接口和 SQL 映射文件#xff0c;提高… MyBatis Generator附批量操作分页查询存储过程 Generator 介绍网址Introduction to MyBatis Generator Generator 一个用于 MyBatis 的代码生成工具可以根据数据库表结构自动生成对应的实体类、DAO 接口和 SQL 映射文件提高开发效率和代码质量。同时MyBatis Generator 还支持自定义生成规则可以按照自己的需求进行配置。 简单示例 首先在 pom.xml 中添加依赖 !-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -- dependencygroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-core/artifactIdversion1.4.0/version /dependency接着在 resources 目录下创建一个 Generator 的配置文件 mybatis_generator.xml ?xml version1.0 encodingUTF-8? !DOCTYPE generatorConfigurationPUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtdgeneratorConfigurationcontext idDB2Tables targetRuntimeMyBatis3commentGenerator!-- 是否去除自动生成的注释 --property namesuppressAllComments valuetrue //commentGenerator!-- MySQL数据库连接的信息驱动类、连接地址、用户名、密码 --jdbcConnection driverClasscom.mysql.cj.jdbc.DriverconnectionURLjdbc:mysql://localhost:3306/mybatisdemo?useUnicodetrueamp;characterEncodingutf8amp;useSSLfalseamp;serverTimezoneUTCuserIdrootpassword0123 /!-- Java 类型解析器一般默认为 false --javaTypeResolverproperty nameforceBigDecimals valuefalse //javaTypeResolver!-- Domain 生成器生成实体类。属性 targetProject 生成 POJO 类的位置其余默认 --javaModelGenerator targetPackagecn.edu.MyBatisDemo.model targetProject.\src\main\java /!-- Mapping 生成器生成映射文件。属性 targetProject mapper 映射文件生成的位置其余默认 --sqlMapGenerator targetPackagecn.edu.MyBatisDemo.mapper targetProject.\src\main\java!-- enableSubPackages :是否让 schema 作为包的后缀 --property nameenableSubPackages valuetrue //sqlMapGenerator!-- Mapper 生成器生成接口。targetProject 属性mapper 接口生成的的位置 --javaClientGenerator typeXMLMAPPER targetPackagecn.edu.MyBatisDemo.mapper targetProject.\src\main\java!-- enableSubPackages :是否让 schema 作为包的后缀 --property nameenableSubPackages valuetrue //javaClientGenerator!-- 指定数据表。tableName 属性指定数据库的表名domainObjectName 属性生成对应实体类的名字...Example 属性设置关闭即可 --table tableNamemybatis_generatordomainObjectNameMyBatisGeneratorenableCountByExamplefalseenableUpdateByExamplefalseenableDeleteByExamplefalseenableSelectByExamplefalseselectByExampleQueryIdfalse //context/generatorConfiguration然后只需在数据库中创建一个数据表 mybatis_generator 。表名称需要与 mybatis_generator.xml 的 table 标签中的 tableName 属性值对应 表结构信息如图 最后测试结果 package cn.edu.MyBatisDemo.test;import org.junit.Test; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback;import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;public class MyBatisGeneratorTest {Testpublic void mbgTest() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {ListString warnings new ArrayListString();boolean overwrite true;// 只需修改 Generator 的配置文件名称即可String path this.getClass().getClassLoader().getResource(mybatis_generator.xml).getPath();File configFile new File(path);ConfigurationParser cp new ConfigurationParser(warnings);Configuration config cp.parseConfiguration(configFile);DefaultShellCallback callback new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);} }结果如图实体类、接口与映射文件会自动生成在设定的目录下 接口中声明一系列方法介绍如图 附 在上面案例的基础下简单实现批量操作与分页查询。 批量操作 首先在实体类 MyBatisGenerator 中添加无参构造方法、有参构造方法与 toString() 方法 public MyBatisGenerator() {super(); }public MyBatisGenerator(Integer id, String name, Integer age, String hobby, String career) {this.id id;this.name name;this.age age;this.hobby hobby;this.career career; }Override public String toString() {return MyBatisGenerator{ id id , name name \ , age age , hobby hobby \ , career career \ }; }然后在测试类 MyBatisGenerator 中添加批量操作测试方法 Test public void test() throws IOException {//1.根据配置文件创建数据库连接会话的工厂类InputStream inputStream Resources.getResourceAsStream(mybatis.xml);//获取工厂类SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);//2.通过工厂类获取数据库连接的会话SqlSession sqlSession sqlSessionFactory.openSession(ExecutorType.BATCH);//3.通过 sqlSession 操作数据库try {MyBatisGeneratorMapper myBatisGeneratorMapper sqlSession.getMapper(MyBatisGeneratorMapper.class);for (int i 0 ; i 36 ; i){//实体类的名字 MyBatisGenerator 与导入的 org.mybatis.generator.api.MyBatisGenerator 名字重复。故此写上 cn.edu.MyBatisDemo.model.myBatisGeneratorMapper.insert(new cn.edu.MyBatisDemo.model.MyBatisGenerator(20230901i,Qi,18,看书,歌手));}sqlSession.commit();} finally {sqlSession.close();} }注在创建会话中传入参数 ExecutorType.BATCH设定采用批量操作的方式执行 SQL 语句 最后测试结果 结果如图 分页查询 MyBatis 分页插件 PageHelper 作者 — isea533 Mybatis-PageHelper 网址 首先在 pom.xml 中添加依赖 dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion5.3.2/version /dependency接着在全局配置文件 mybatis.xml 中配置插件 !-- plugins 标签需要在 environments 标签前 -- pluginsplugin interceptorcom.github.pagehelper.PageInterceptor/plugin /plugins然后在接口 MyBatisGeneratorMapper 中声明获取所有用户信息的方法。同时在映射文件 MyBatisGeneratorMapper.xml 中实现方法 select idselectAll resultTypemyBatisGenerator selectinclude refidBase_Column_List /from mybatis_generator /select最后测试结果 存储过程 在 MySQL调优 文章中了解过存储过程。接下来简单介绍 MyBatis 如何调用存储过程。 首先创建一个存储过程 mybatis_generator_storedProcedure DELIMITER $$CREATE/*[DEFINER { user | CURRENT_USER }]*/PROCEDURE mybatisdemo.mybatis_generator_storedProcedure(IN start_id INT,IN end_id INT)BEGINSELECT id,name,age,hobby,career FROM mybatis_generator WHERE id start_id AND id end_id;END$$DELIMITER ;然后在接口 MyBatisGeneratorMapper 中声明通过调用存储过程获取指定用户信息的方法。同时在映射文件 MyBatisGeneratorMapper.xml 中实现方法 public ListMyBatisGenerator selectByStoredProcedure(Param(start_id) int start_id,Param(end_id) int end_id); //通过调用存储过程获取指定用户的信息!-- 指定 statementType 属性值为 CALLABLE -- select idselectByStoredProcedure resultTypemyBatisGenerator statementTypeCALLABLE {call mybatis_generator_storedProcedure(#{start_id,modeIN,jdbcTypeINTEGER},#{end_id,modeIN,jdbcTypeINTEGER})} /select最后测试结果
http://www.dnsts.com.cn/news/151030.html

相关文章:

  • 做自己的网站花多钱商品房交易网
  • 域名网站空间做网站遵义
  • 营销型网站设计模板个人网站包含哪些内容
  • 山东seo网站推广360建筑网官网下载
  • 网站建设总结与丹阳论坛
  • 建设银行网站查余额新闻平台发布
  • 阳春县建设局网站深圳全屋整装哪家公司好
  • 网站降权不更新文章可以吗网站建设seo方案
  • 重庆seo网站排名优化有没有做电子名片的网站
  • 手机网站变灰枣庄市建设项目环评备案网站
  • 网站开发机构班级网站建设策划书
  • 广州制作网站公司哪家好南京百度推广网站
  • 网站建设项目策划书模板范文网站建设与制作模板
  • 福州网站建设 联系yanktcn 05东营网格通
  • 东莞家具行业营销型网站建设多少钱外贸接单网站
  • 电子商务网站建设配色网页搜索引擎
  • 个人网页制作素材图片湖南seo博客seo交流
  • 免费网站源码下载网页前端开发培训
  • 金融品牌网站设计网页程序开发语言
  • 查询网站备案进度查询四大网站
  • 网站制作运营公司wordpress文章自适应图片大小
  • 青岛市专业做网站的吗南京哪里可以做网站
  • 手机网站活动策划方案wordpress视频试看付费
  • 4399自己做游戏网站购物网站服务器硬件配置
  • 专做it招聘的网站2008系统做网站
  • 认证网站所有权要添加代码温州市建设局网站
  • 兼职开发网站开发网站制图软件
  • 帮助传销做网站违法吗模板网络结构图怎么画
  • soho做网站多少钱重庆长寿网站设计公司哪家好
  • 网站建设微商城电子商务网站系统的开发设计