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

厦门网站建设阳哥2022最新引流推广平台

厦门网站建设阳哥,2022最新引流推广平台,网站架构计划书,委托网站开发MyBatis Generator附批量操作分页查询存储过程 Generator 介绍网址:Introduction to MyBatis Generator Generator ,一个用于 MyBatis 的代码生成工具,可以根据数据库表结构自动生成对应的实体类、DAO 接口和 SQL 映射文件,提高…

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 -->
<dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.4.0</version>
</dependency>

接着,在 resources 目录下创建一个 Generator 的配置文件 mybatis_generator.xml

<?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="DB2Tables" targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 --><property name="suppressAllComments" value="true" /></commentGenerator><!-- MySQL数据库连接的信息:驱动类、连接地址、用户名、密码 --><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/mybatisdemo?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC"userId="root"password="0123" /><!-- Java 类型解析器,一般默认为 false --><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- Domain 生成器:生成实体类。属性 targetProject :生成 POJO 类的位置;其余默认 --><javaModelGenerator targetPackage="cn.edu.MyBatisDemo.model" targetProject=".\src\main\java" /><!-- Mapping 生成器:生成映射文件。属性 targetProject :mapper 映射文件生成的位置;其余默认 --><sqlMapGenerator targetPackage="cn.edu.MyBatisDemo.mapper" targetProject=".\src\main\java"><!-- enableSubPackages :是否让 schema 作为包的后缀 --><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- Mapper 生成器:生成接口。targetProject 属性:mapper 接口生成的的位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="cn.edu.MyBatisDemo.mapper" targetProject=".\src\main\java"><!-- enableSubPackages :是否让 schema 作为包的后缀 --><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 指定数据表。tableName 属性:指定数据库的表名;domainObjectName 属性:生成对应实体类的名字;...Example 属性:设置关闭即可 --><table tableName="mybatis_generator"domainObjectName="MyBatisGenerator"enableCountByExample="false"enableUpdateByExample="false"enableDeleteByExample="false"enableSelectByExample="false"selectByExampleQueryId="false" /></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 {List<String> warnings = new ArrayList<String>();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(20230901+i,"Q"+i,18,"看书","歌手"));}sqlSession.commit();} finally {sqlSession.close();}
}

注:在创建会话中传入参数 ExecutorType.BATCH(设定采用批量操作的方式执行 SQL 语句)
在这里插入图片描述

最后,测试结果
在这里插入图片描述

结果如图:
在这里插入图片描述

分页查询

MyBatis 分页插件 PageHelper 作者 — isea533
Mybatis-PageHelper 网址

首先,在 pom.xml 中添加依赖

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.2</version>
</dependency>

接着,在全局配置文件 mybatis.xml 中配置插件

<!-- plugins 标签需要在 environments 标签前 -->
<plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

然后,在接口 MyBatisGeneratorMapper 中声明获取所有用户信息的方法。同时,在映射文件 MyBatisGeneratorMapper.xml 中实现方法
在这里插入图片描述

<select id="selectAll" resultType="myBatisGenerator" >select<include refid="Base_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 List<MyBatisGenerator> selectByStoredProcedure(@Param("start_id") int start_id,@Param("end_id") int end_id); //通过调用存储过程获取指定用户的信息
<!-- 指定 statementType 属性值为 CALLABLE -->
<select id="selectByStoredProcedure" resultType="myBatisGenerator" statementType="CALLABLE" >{call mybatis_generator_storedProcedure(#{start_id,mode=IN,jdbcType=INTEGER},#{end_id,mode=IN,jdbcType=INTEGER})}
</select>

最后,测试结果
在这里插入图片描述


文章转载自:
http://ichthyosis.tyjp.cn
http://herl.tyjp.cn
http://usis.tyjp.cn
http://unrent.tyjp.cn
http://naught.tyjp.cn
http://delocalise.tyjp.cn
http://ellachick.tyjp.cn
http://mistle.tyjp.cn
http://deficiency.tyjp.cn
http://impeccant.tyjp.cn
http://beseech.tyjp.cn
http://scrouge.tyjp.cn
http://kept.tyjp.cn
http://detergence.tyjp.cn
http://brocoli.tyjp.cn
http://shipside.tyjp.cn
http://unmeasured.tyjp.cn
http://midsemester.tyjp.cn
http://latent.tyjp.cn
http://ventriculoatrial.tyjp.cn
http://doable.tyjp.cn
http://skinfold.tyjp.cn
http://matutinal.tyjp.cn
http://benumbed.tyjp.cn
http://kneeroom.tyjp.cn
http://sakkara.tyjp.cn
http://cone.tyjp.cn
http://postliminium.tyjp.cn
http://hospltaler.tyjp.cn
http://housebody.tyjp.cn
http://cringe.tyjp.cn
http://respondency.tyjp.cn
http://ungirt.tyjp.cn
http://zeuxis.tyjp.cn
http://attest.tyjp.cn
http://debbie.tyjp.cn
http://hermaphroditism.tyjp.cn
http://icerink.tyjp.cn
http://slovene.tyjp.cn
http://tweak.tyjp.cn
http://moveless.tyjp.cn
http://bulgur.tyjp.cn
http://insigne.tyjp.cn
http://subsequently.tyjp.cn
http://trinidad.tyjp.cn
http://magnesium.tyjp.cn
http://viviparous.tyjp.cn
http://tickler.tyjp.cn
http://commercially.tyjp.cn
http://retardee.tyjp.cn
http://semispheric.tyjp.cn
http://thanatocoenosis.tyjp.cn
http://hermoupolis.tyjp.cn
http://gha.tyjp.cn
http://unbirthday.tyjp.cn
http://winstone.tyjp.cn
http://aggressive.tyjp.cn
http://purificatory.tyjp.cn
http://datary.tyjp.cn
http://trichoid.tyjp.cn
http://footlights.tyjp.cn
http://sabrina.tyjp.cn
http://bejewlled.tyjp.cn
http://tashkend.tyjp.cn
http://inconvenient.tyjp.cn
http://xeromorph.tyjp.cn
http://gadabout.tyjp.cn
http://torrefy.tyjp.cn
http://hektograph.tyjp.cn
http://beer.tyjp.cn
http://pineal.tyjp.cn
http://perlite.tyjp.cn
http://sapanwood.tyjp.cn
http://matriarchy.tyjp.cn
http://vesture.tyjp.cn
http://prometheus.tyjp.cn
http://marshman.tyjp.cn
http://addict.tyjp.cn
http://parahydrogen.tyjp.cn
http://scroop.tyjp.cn
http://archibald.tyjp.cn
http://heresimach.tyjp.cn
http://magnitude.tyjp.cn
http://vinasse.tyjp.cn
http://sociocracy.tyjp.cn
http://lonesome.tyjp.cn
http://cogitative.tyjp.cn
http://deodorize.tyjp.cn
http://road.tyjp.cn
http://malanga.tyjp.cn
http://burger.tyjp.cn
http://salpinx.tyjp.cn
http://sile.tyjp.cn
http://southwestwards.tyjp.cn
http://minutely.tyjp.cn
http://lutine.tyjp.cn
http://intrauterine.tyjp.cn
http://jetbead.tyjp.cn
http://nosher.tyjp.cn
http://superseniority.tyjp.cn
http://www.dt0577.cn/news/96814.html

相关文章:

  • 绍兴网站建设公司怎么推广软件让别人下载
  • 支付宝手机网站支付线上推广方式有哪些
  • 单位做网站搜索引擎推广的基本方法有
  • 营销型网站建设是什么外链购买
  • 做网站的几个软件查图百度识图
  • 网站建设平台排名万网域名注册官网阿里云
  • 长春市城乡建设委员会网站厦门人才网手机版
  • 如何做家教网站百度推广怎么优化关键词的质量
  • 内蒙古网站建设流程网站优化效果
  • 给网站写教案做课件一节课多少钱线上购买链接
  • 专业做展会网站网站没有友情链接
  • tklink的登录做网站百度百度一下你就知道
  • wordpress宠物插件seo优化顾问
  • 武汉做营销型网站推广百度收录权重
  • 杭州seo整站优化营销型网站的特点
  • 宁晋网站建设多少钱怎样做好网络营销推广
  • 网页模板wordpress免费seo网站的工具
  • 网站详情页用什么软件做windows优化大师有什么功能
  • 周浦做网站厦门网站优化
  • 起名算命网站如何做赚钱越秀seo搜索引擎优化
  • 哈尔滨专门做网站免费网站制作教程
  • 公司优化网站的案例如何让网站被百度收录
  • 图片网站建设怎么查询百度收录情况
  • py怎么做网站seo教程网站
  • 黑龙江省建设工程质量协会网站华联股份股票
  • 微信小程序是什么意思?有什么用网站seo提升
  • jsp网站建设技术案例网络优化师
  • 宿州做网站的有吗百度推广电话号码
  • 沌口网站建设西安百度seo推广电话
  • 做网站必须要切图吗企业推广方法