Mybatis逆向生成XML
- 引入mybatis-generator-core依赖
<!-- mybatis逆向工程依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
- 配置自动生成的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>
- 执行自动生成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();
}
}
版权声明:本文为qq_45429856原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。