app网站开发学习,主机公园安装wordpress要多久,做静态网站的步骤,化妆品网站建设目的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最后测试结果