1. What is mybatis reverse engineering
When using mybatis, programmers need to write sql statements by themselves. The number of sql statements for single tables is very large. Mybatis official provides a tool to generate mybatis execution code based on database tables. This tool is a reverse engineering.
Reverse engineering: Generate code (mapper.xml, mappper.java, pojo) for single database tables - ->
mybatis-generator-core-1.3.2.jar - jar core package required for reverse engineering operation
2. Configure the configuration file for reverse engineering
Configuration file generatorConfig.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="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- Whether to remove the auto-generated comments true: Yes: false: No--> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--Database connection information: driver class, connection address, username, password--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- Default false, parse the JDBC DECIMAL and NUMERIC types to Integer, and when true, parse the JDBC DECIMAL and NUMERIC types to java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:The location where the PO class is generated --> <javaModelGenerator targetPackage="cn.zm.mybatis.po" targetProject="./src"> <!-- enableSubPackages:Where to let schema be used as the suffix of the package --> <property name="enableSubPackages" value="false" /> <!-- space before and after the value returned from the database is cleaned --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:The location where the mapper map file is generated --> <sqlMapGenerator targetPackage="cn.zm.mybatis.mapper" targetProject="./src"> <!-- enableSubPackages: Whether to use schema as the suffix of the package --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage: Location of mapper interface generation --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.zm.mybatis.mapper" targetProject="./src"> <!-- enableSubPackages: Whether to use schema as the suffix of the package --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- Specify database table--> <table tableName="items"></table> <!-- <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table>--> <!-- <table schema="" tableName="sys_user"></table> <table schema="" tableName="sys_permission"></table> <table schema="" tableName="sys_user_role"></table> <table schema="" tableName="sys_role_permission"></table> --> <!-- Some table fields require java type<table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context></generatorConfiguration>
3. Execute reverse engineering to generate code
Execute java class method:
The generated code is as follows:
4. Copy the generated code to the business system project for testing
public class ItemsMapperTest { private ApplicationContext applicationContext; private ItemsMapper itemsMapper; @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper"); } //Delete the root primary key @Test public void deleteByPrimaryKey() { itemsMapper.deleteByPrimaryKey(4); } @Test public void insert() { } @Test public void selectByExample() { ItemsExample itemsExample = new ItemsExample(); ItemsExample.Criteria criteria = itemsExample.createCriteria(); //Use criteria to customize query conditions criteria.andNameEqualTo("Water Cup"); criteria.andIdEqualTo(1); List<Items> list = itemsMapper.selectByExample(itemsExample); System.out.println(list); } @Test public void selectByPrimaryKey() { Items items = itemsMapper.selectByPrimaryKey(1); System.out.println(items); } @Test public void updateByPrimaryKey() { }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.