[JAVA] MyBatis story

MyBatis story

Roughly memorandum memo as usual

Precautions when linking with Spring

The DB connection destination uses the data source settings when MapperScan registered the bean in the DI container. Note that even if you switch the transactionManager of @Transactional, you will not go to connect to the DB of the data source set by the switched transactionManager.

In the case where the table structure is exactly the same but only the DB connection destination is different (when you prepare two data sources and want to access each individually), Both MapperScan for data source 1 and MapperScan for data source 2 are prepared, and it is necessary to specify sessionFactoryRef and nameGenerator separately for one of the data sources.

nameGenerator is required to prevent the generated bean name from being covered.

Specify the bean name when DI (when Autowired).

Introducing web application development using MyBatis

https://www.slideshare.net/tokuhirom/mybatis-web-application

eclipse plugin eclipse marketplace> Mybatipse It is almost essential because the completion will be effective when writing xml.

Easy-to-understand usage

It is somewhat sensuous to define the properties of the relay entity in the entity class and map them. .. ??

Call the definition (resultMap etc.) described in another xml

http://kamatama41.hatenablog.com/entry/20130409/1365483365

Automatically generated

Use ColumnOverride for Enum support http://blog.zaq.ne.jp/oboe2uran/article/1180/ http://tech-blog.tsukaby.com/archives/205

When using plugin, add a project of Plugin class by right-clicking generatorConfig.xml> Execution configuration> Classpath> Add project>

Specifying the character code of the output java file

<context>
    <property name="javaFileEncoding" value="UTF-8" />
~
</context>

Make fields of auto-generated entities protected

See FieldVisibilityProtectedPlugin.java and myBaitsGeneratorConfig.xml in the sample code

TypeHandler for LocalDateTime

https://github.com/javaplugs/mybatis-types

Turn off cache

http://www.mybatis.org/mybatis-3/ja/configuration.html Settings cacheEnabled

Since cache is effective by default, one transaction? Even if I throw the query that is executed once in (I forgot the detailed unit), it does not go to the DB.

MyBatisGenerator does not overwrite the java file (If you set it to overwrite, it will erase the code other than @mbggenerated)

If you put in the eclipse plugin and use it, overwriting works well.

See below for the location of the plugin http://www.mybatis.org/generator/running/runningWithEclipse.html

I want to hit raw SQL with MyBatis

http://qiita.com/kumazo@github/items/72ecdb2923b77aaa0c94

[Java] Improve performance with dynamic SQL on MyBatis

http://atuweb.net/201602_java-mybatis-dynamic-and-fast-sql/

Recommended Posts

MyBatis story
MyBatis memorandum
Range story
Disable MyBatis3 cache
Java static story
MyBatis string comparison
BigDecimal.valueOf (double) story
Downcast story (memories)
Java initializer story
Java generic story