The "Spring Boot+mybatis" annotations are used (no XML configuration) set Automatic hump obviously conversion (mapunderscoretocamelcase), idea in Xxdao error could not autowi

Source: Internet
Author: User

Recent use of spring Boot+mybatis, using IntelliJ idea Development, to document some of the solution to the problem.

1, in the use of @mapper annotations instead of xxmapper.xml configuration files, using @select and other annotations to configure the SQL statement, how to configure the database field name to the JavaBean entity class properties of the automatic hump naming conversion.

With spring boot, it is increasingly preferred to use annotations as a way to configure instead of the XML configuration file. Annotations can also be fully used in MyBatis to avoid the use of XML as a way to configure Mapper. (Reference Springboot (vi): How to use the elegant 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)

Set up automatic hump naming transformations, where mapunderscoretocamelcase properties can be directly configured in XML.

However, the use of annotation methods, after a search for information to find a better way to set up. As follows:

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

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

Set to True to enable hump conversions. After testing, both configuration methods can be. But if configured at the same time, the former mybatis.configuration.mapUnderscoreToCamelCase higher priority.

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 mybatis-Wang Nian Blog https://my.oschina.net/wangnian/blog/667764

In addition, there is a javaconfig way to configure the org.apache.ibatis.session.SqlSessionFactory bean, put into spring's object pool. Mapunderscoretocamelcase is an attribute of org.apache.ibatis.session.Configuration that instantiates the configuration object and sets its Mapunderscoretocamelcase property to TR UE, then use this configuration object as Sqlsessionfactory configuration to make mapunderscoretocamelcase=true effective.

But just to change the value of a property, it's too cumbersome to encode a sqlsessionfactory on your own. It is more convenient to use the method configured in Application.properties.

2.mybatis Management of the @mapper DAO, in the use of @autowire automatic injection, idea has a red error "could not Autowire", but the actual run-time normal, how to remove the error.

As I understand it, this error is due to the DAO interface adding only MyBatis custom @mapper annotations, not adding spring-defined @component, @Repository, and so idea does not consider this to be a bean that is incorporated into spring management. The source of the DAO that caused the autowire to be found in idea.

Find the solution and found the answer here:

Java-idea inspects Batis mapper Bean Wrong-stack Overflow Batis-mapper-bean-wrong

It mentioned that the installation of "MyBatis plugin" plug-in can be resolved, but I tried to install the plugin and enabled, there is still a red error (the plugin has been activated, not the problem caused by license), So guess that this plugin is only valid for Xxmapper.xml configuration, and the way to @mapper annotation DAO interface is invalid (for the latter case is valid, we tried to feedback the result).

So you have to use a compromise is not the perfect way, in DAO interface add @mapper, add @repository (or @component can), such as the following code line 1th:

1 @Repository
2 @Mapper
3 public interface Userdao {
4 
5     @Select ("Select phone from user WHERE name = #{name} ")//dynamically incoming table name, you can use ... From ${tablename} ... but needs to resolve SQL injection risk
6     string Getphonebyusername (@Param ("name") string name);
7}

This method does not seem to have any side effects in use.

PS: After the first attempt to install the "MyBatis plugin" plugin, the following output is available in Idea's event window after startup:

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

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.