Detailed description of MyBatis reverse engineering and mybatis Reverse Engineering

Source: Internet
Author: User

Detailed description of MyBatis reverse engineering and mybatis Reverse Engineering

1. What is mybatis reverse engineering?

When using mybatis, programmers need to write their own SQL statements. The number of SQL statements for a single table is large. mybatis provides a tool to generate mybatis code execution based on the database table, this tool is a reverse engineering.
Reverse Engineering: generates code (mapper. xml, mapper. java, and pojo) for a single database table ..)

Mybatis-generator-core-1.3.2.jar-jar core package required for reverse engineering operation

2. Configure the configuration file of the Reverse Engineering

Configuration File generatorConfig. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE generatorConfiguration PUBLIC "-// mybatis.org//DTD MyBatis Generator Configuration 1.0 //" http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <context id =" testTables "targetRuntime =" MyBatis3 "> <commentGenerator> <! -- Whether to remove automatically generated comments true: Yes: false: No --> <property name = "suppressAllComments" value = "true"/> </commentGenerator> <! -- Database connection information: Driver Class, connection address, user name, 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> --> <! -- The default value is false. The jdbc decimal and NUMERIC types are parsed to Integer. If the value is true, the jdbc decimal and NUMERIC types are parsed to java. math. bigDecimal --> <javaTypeResolver> <property name = "forceBigDecimals" value = "false"/> </javaTypeResolver> <! -- TargetProject: location where the PO class is generated --> <javaModelGenerator targetPackage = "cn. zm. mybatis. po" targetProject = ". \ src"> <! -- EnableSubPackages: whether to use schema as the package suffix --> <property name = "enableSubPackages" value = "false"/> <! -- Space before and after the value returned from the database --> <property name = "trimStrings" value = "true"/> </javaModelGenerator> <! -- TargetProject: er ing file generation location --> <sqlMapGenerator targetPackage = "cn. zm. mybatis. mapper" targetProject = ". \ src"> <! -- EnableSubPackages: whether to use schema as the package suffix --> <property name = "enableSubPackages" value = "false"/> </sqlMapGenerator> <! -- TargetPackage: Location generated by the ER interface --> <javaClientGenerator type = "XMLMAPPER" targetPackage = "cn. zm. mybatis. mapper" targetProject = ". \ src"> <! -- EnableSubPackages: whether to use schema as the package suffix --> <property name = "enableSubPackages" value = "false"/> </javaClientGenerator> <! -- Specify a 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_role"> </table> <table schema = "" tableName "= "sys_permission"> </table> <table schema = "" tableName = "sys_user_role"> </table> <table schema = "" tableName = "sys_role_permission"> </table> --> <! -- Some table fields must specify the java type <table schema = "" tableName = ""> <columnOverride column = "" javaType = "/> </table> --> </ context> </generatorConfiguration>

3. Execute reverse engineering to generate code

Execute the 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 the query condition criteria. andnamephoto to ("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 for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.