Mybatis逆向生成XML

本文介绍了如何使用MyBatis Generator进行逆向工程,包括引入依赖、配置config.xml、生成代码的步骤,并展示了执行生成代码的示例。通过这个过程,可以自动生成基于MySQL数据库的实体类、Mapper接口和XML映射文件。
摘要由CSDN通过智能技术生成

Mybatis逆向生成XML

  1. 引入mybatis-generator-core依赖
<!-- mybatis逆向工程依赖 -->
	<dependency>
	    <groupId>org.mybatis.generator</groupId>
	    <artifactId>mybatis-generator-core</artifactId>
	    <version>1.3.7</version>
	</dependency>
  1. 配置自动生成的config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC
    "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
	<context id="sqlGenertor" targetRuntime="MyBatis3">
		<!-- 配置是否使用注释 -->
		<commentGenerator>
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		<!-- 配置连接信息 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/studb?characterEncoding=utf8" userId="root" password="123456" ></jdbcConnection>
		
		<!-- 配置生成的POJO -->
		<javaModelGenerator targetPackage="com.zuxia.entity" targetProject=".\src\main\java"></javaModelGenerator>
		
		<!-- 配置生成的Mapper映射文件 -->
		<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"></sqlMapGenerator>
		
		<!-- 配置生成的接口类-->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.zuxia.dao" targetProject=".\src\main\java"></javaClientGenerator>
		
		<!-- 配置需要生成的数据表 -->
		<table tableName="employeeinfo" domainObjectName="EmployeeInfo">
			<columnOverride column="salary" javaType="java.lang.Double" />
		</table>
		<table tableName="recruitinfo" domainObjectName="RecruitInfo"></table>
		<table tableName="applyinfo" domainObjectName="ApplyInfo"></table>
		<table tableName="interviewinfo" domainObjectName="InterViewInfo"></table>
	</context>
</generatorConfiguration>
  1. 执行自动生成config.xml的文件的类
package com.zxia.test;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
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 com.zuxia.dao.EmployeeInfoMapper;
import com.zuxia.entity.EmployeeInfo;
import com.zuxia.entity.EmployeeInfoExample;
import com.zuxia.entity.EmployeeInfoExample.Criteria;

public class Test1 {
	public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
		//加载配置文件
		Reader rd = Resources.getResourceAsReader("config.xml");
		ConfigurationParser cp=new ConfigurationParser(null);
		Configuration config=cp.parseConfiguration(rd);

		DefaultShellCallback dsc=new DefaultShellCallback(true);
		MyBatisGenerator mg=new MyBatisGenerator(config, dsc, null);
		mg.generate(null);
		System.out.println("ol。。。。。。。。。。。。。。");
	}
}

逆向生成的方法调用

package com.zxia.test;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
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 com.zuxia.dao.EmployeeInfoMapper;
import com.zuxia.entity.EmployeeInfo;
import com.zuxia.entity.EmployeeInfoExample;
import com.zuxia.entity.EmployeeInfoExample.Criteria;

public class Test1 {
//	public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
//		//加载配置文件
//		Reader rd = Resources.getResourceAsReader("config.xml");
//		ConfigurationParser cp=new ConfigurationParser(null);
//		Configuration config=cp.parseConfiguration(rd);
//		DefaultShellCallback dsc=new DefaultShellCallback(true);
//		MyBatisGenerator mg=new MyBatisGenerator(config, dsc, null);
//		mg.generate(null);
//		System.out.println("ol。。。。。。。。。。。。。。");
//	}
	
	public static void main(String[] args) throws IOException {
		Reader rd = Resources.getResourceAsReader("mybatis-config.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(rd);
		SqlSession session=factory.openSession();
		EmployeeInfoMapper empMapper = session.getMapper(EmployeeInfoMapper.class);

		//添加
		EmployeeInfo emp=new EmployeeInfo();
		emp.setEname("admin");
		emp.setEtel("1643535");
//		int count = empMapper.insert(emp);//添加所有列
		int count = empMapper.insertSelective(emp);//添加指定列
		
		emp.setEnme("zhagnsan");
		emp.setSalary(888.0);
		emp.setEid(1);
		int count = empMapper.updateByPrimaryKeySelective(emp);
		
		emp.setEsex("男");
		emp.setEname("lisi1");
		EmployeeInfoExample example=new EmployeeInfoExample();
		Criteria ct = example.createCriteria();
//		ct.andEidEqualTo(2);
		ct.andEnameLike("%s%");
		int count = empMapper.updateByExampleSelective(emp, example);
		
		int count = empMapper.deleteByPrimaryKey(1);
		session.commit();
		System.out.println("执行结果:"+count);
		
		EmployeeInfoExample example=new EmployeeInfoExample();
		example.createCriteria().andEidEqualTo(1).andSalaryIsNull();
		empMapper.selectByExample(example).stream().forEach(System.out::println);
		
		empMapper.selectPage(new RowBounds((2-1)*1, 1)).stream().forEach(System.out::println);
		long count = empMapper.countByExample(null);
		System.out.println("总条数:"+count);
		session.close();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 MyBatis Generator 工具可以快速生成 MyBatis 的 Mapper 接口、XML 映射文件和实体类等代码,实现逆向工程。 下面是使用 Spring Boot 集成 MyBatis Generator 的步骤: 1. 在 pom.xml 中引入 MyBatis Generator 插件: ```xml <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <dependencies> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> ``` 2. 在 resources 目录下创建 generatorConfig.xml 配置文件,定义 MyBatis Generator 的配置信息: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="MySQLTables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user"></table> </context> </generatorConfiguration> ``` 3. 在 Maven 中执行以下命令,生成 MyBatis 的 Mapper 接口、XML 映射文件和实体类等代码: ``` mvn mybatis-generator:generate ``` 执行成功后,会在 src/main/java/com/example/demo/entity 和 src/main/java/com/example/demo/mapper 目录下生成对应的代码。 以上就是使用 Spring Boot 集成 MyBatis Generator 的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值