Mybatis type conversion when using generator to automatically generate code, mybatisgenerator

Source: Internet
Author: User

Mybatis type conversion when using generator to automatically generate code, mybatisgenerator

Use the generator of mybatis to automatically generate code, but in the oracle database, number (6, 2) is always automatically converted to BigDecimal, and I want to convert it to float type

In this way, a type converter needs to inherit the JavaTypeResolver interface.

Add the type conversion configuration location in the mybaties configuration file generatorConfig. xml.

<JavaTypeResolver type = "com. generator. MyJavaTypeResolver"> <property name = "forceBigDecimals" value = "false"/> <! -- Type parser --> </javaTypeResolver>
Main Code of type converter MyJavaTypeResolver
Public FullyQualifiedJavaType calculateJavaType (IntrospectedColumn introspectedColumn) {// TODO Auto-generated method stub FullyQualifiedJavaType answer; JdbcTypeInformation jdbcTypeInformation = typeMap. get (introspectedColumn. getJdbcType (); if (jdbcTypeInformation = null) {switch (introspectedColumn. getJdbcType () {case Types. DECIMAL: case Types. NUMERIC: if (introspectedColumn. getScale ()> 0) {// convert to float answer = new FullyQualifiedJavaType (Float. class. getName ();} else {if (introspectedColumn. getLength ()> 18 | forceBigDecimals) {answer = new FullyQualifiedJavaType (BigDecimal. class. getName ();} else if (introspectedColumn. getLength ()> 9) {answer = new FullyQualifiedJavaType (Long. class. getName ();} else if (introspectedColumn. getLength ()> 4) {answer = new FullyQualifiedJavaType (Integer. class. getName ();} else {answer = new FullyQualifiedJavaType (Short. class. getName () ;}} break; default: answer = null; break ;}} else {answer = jdbcTypeInformation. getFullyQualifiedJavaType ();} return answer ;}




How to automatically generate integrated development codes for MyBatis (Ant)

Mybatis3.0 provides the code generation function. Currently the latest is the mybatis-generator-core-1.3.1. This is a more flexible plug-in.
Of course it is not integrated with IDE, but just a package. it can be used in the command line, Ant, Maven, or even directly written to Java code to generate the MyBatis code. if it still does not meet the requirements, it can be expanded. with it, you can connect to the database to generate the corresponding basic code after using the modeling tool to generate the database. includes the value object, data access object interface and its MyBatis implementation.
I personally prefer Ant. the following describes how to use Ant generation. first, write an Ant file to declare some attributes and several targets. these targets are for the modules to be generated. it is divided into multiple modules for ease of management and compilation.
Build. xml: copy the text to the clipboard <? Xml ?? Version = "1.0 "????? <Project ?? Default = "sysGenerator "?? Basedir =
"."???????????? <Property ?? Name = "generated. source. dir "?? Value = "$ {basedir }"?? /???????????????? <Target ?? Name = "sysGenerator "?? Description = "mybatis-generator "???????????????????? <Taskdef ?? Name = "sysGenerator "???????????????????????????? Classname = "org. mybatis. generator. ant. generatorAntTask "???????????????????????????? Classpath =
"../Common_lib/mybatis-generator-core-1.3.1.jar "?? /???????????????????? <SysGenerator ?? Overwrite = "true "?? Configfile = "sysGenerator. xml "?? Verbose = "false "?????????????????????????????? <Propertyset ???????????????????????????????????? <Propertyref ?? Name = "generated. source. dir "/???????????????????????????? </Propertyset ???????????????????? </SysGenerator ???????????? </Target ???? </Project ???? First define a Target named sysGenerator. in practice, you can create multiple modules. taskdef defines the task type and the package to be referenced. then it is the body content. configfile is used to configure the generated code. we recommend that you use system modules. let's take a look at sysGenerator. the content of the xml file is copied to the clipboard ??????...... Remaining full text>

Can mybatis use the DAO layer automatically generated by generatorConfig to implement complex SQL statements?

No... Don't even think about it;
The tool does not know what the business logic is. This is called a requirement.
 

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.