"Spring Boot+mybatis" annotation usage (no XML configuration) set Automatic hump obviously conversion (), idea in Xxdao error could not autowire solution

Source: Internet
Author: User

Recently using the spring Boot+mybatis, using IntelliJ idea to develop and document some of the solutions to the problem.

1, in the case of using @mapper annotation instead of the Xxmapper.xml configuration file, using annotations such as @select to configure the SQL statement, how to configure the database field name to JavaBean entity class attribute life automatic hump naming conversion?

With spring boot, it is becoming more and more like annotated configuration instead of XML configuration file. Annotations can also be used entirely in mybatis, avoiding the use of XML to configure Mapper. (see Springboot (vi): How to use MyBatis http://www.ityouknow.com/springboot/2016/11/06/springboot (%E5%85%AD)-%e5%a6%82% e4%bd%95%e4%bc%98%e9%9b%85%e7%9a%84%e4%bd%bf%e7%94%a8mybatis.html)

Sets the automatic hump naming transformation, which allows you to configure the Mapunderscoretocamelcase property directly in XML.

However, when you use annotations, you find a better way to set up your data. As follows:

In the spring boot configuration file application.properties, add the configuration entry:

1 mybatis.configuration.mapunderscoretocamelcase=true2 or 3 mybatis.configuration.map-underscore-to-camel-case =true

Set to True to turn on hump conversion. After testing, both configuration methods are available. However, if configured at the same time, the former mybatis.configuration.mapUnderscoreToCamelCase has higher precedence.

Reference:

Official configuration Instructions Mybatis-spring-boot-autoconfigure–mybatis Sring-bootstarter | Reference Documentation Http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration

Springboot's MyBatis-Nian blog https://my.oschina.net/wangnian/blog/667764

Also find out that there is a javaconfig way to configure the Org.apache.ibatis.session.SqlSessionFactory bean, into the spring object pool. Mapunderscoretocamelcase is a property of org.apache.ibatis.session.Configuration that instantiates a configuration object and sets its Mapunderscoretocamelcase property to TR UE, and then use this configuration object as sqlsessionfactory to make mapunderscoretocamelcase=true effective.

But just to change the value of an attribute, it is too cumbersome to generate a sqlsessionfactory code. It is more convenient to use the method configured in Application.properties.

2.mybatis Management @mapper DAO, when using @autowire automatic injection, IDEA has red error "could not Autowire", but the actual operation is normal, how to remove the error?

According to my understanding, this error is because the DAO interface only added MyBatis custom @mapper annotations, did not add spring-defined @component, @Repository, etc., so idea does not think this is a spring-managed bean, The source of DAO that causes Autowire to be found in idea.

Find a solution and find the answers here:

Java-idea inspects Batis mapper Bean wrong-stack Overflow https://stackoverflow.com/questions/25379348/idea-inspects- Batis-mapper-bean-wrong

It is mentioned that installing the "MyBatis plugin" plugin can be solved . After I try to install it, I have the following output in the idea's event window after launch:

1 Mybatis plugin:mybatis Plugin is not activated yet. Click here to enter your license key to activate the plugin. You can also click here to purchase a license key to sponsor us making the plugin better. More features is on their. Wish Happy coding with the plugin

Seems to be a charge plug-in, need to buy license, to the official website to see the price is ¥39.99.

Then I tried to install the "free MyBatis plugin" and "Ibatis/mybatis mini-plugin" plug-ins, but did not remove the red error.

Temporarily recorded here, after the subsequent resolution to update.

"Spring Boot+mybatis" annotation usage (no XML configuration) set Automatic hump obviously conversion (), idea in Xxdao error could not autowire solution

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.