MyBatisPlay
This module allows you to use MyBatis persistence framework with play. Included is MyBatis version 3.1.0
- Contains a fix for a classloader issue when using default mybatis cache (SerializedCache)
- It will detect changes to the xml configuration files and reinitialize the SqlSessionFactory
What’s new?
Updated MyBatis source to MyBatis 3.1.0
Sample application
There is a sample in samples-and-tests/mybatisapp folder.
cd to that directory and run:
play test
go to http://localhost:9000/@tests and run @GuestBookMapperTest@
Getting started
To install MyBatisPlay add it as a dependency in your dependencies.yml file:
require:
- play -> mybatisplay {version}
Configuration
Define these minimum settings in application.conf
Database settings
db.url=jdbc:mysql://localhost:3306/mydb
db.driver=com.mysql.jdbc.Driver
db.user=dbuser
db.pass=dbpass
MyBatis configuration location
mybatis.configuration=mybatis/configuration.xml
You can base your configuration.xml file on this template:<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pass}"/>
</dataSource>
</environment>
</environments>
</configuration>
See the included sample for a full example or the MyBatis Docs for details.
Usage
You can get access to a SqlSessionFactory anywhere in your code by simply caling IbatisSessionFactory.get()
SqlSession session = IbatisSessionFactory.get().openSession();
Known issues
When starting play app from eclipse you’ll get this stacktrace:
22:12:28,442 play.Logger.niceThrowable(Logger.java:570) - 570 ERROR ~ Error loading plugin LoadingPluginInfo{name='mybatisplay.plugins.MyBatisPlugin', index=1010, url=file:/D:/java/projects/eclipse-projects/play-mmr-ticketing/eclipse/classes/play.plugins}
java.lang.IllegalStateException: This plugin has already been initialized
at mybatisplay.plugins.MyBatisPlugin.<init>(MyBatisPlugin.java:37)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at play.plugins.PluginCollection.loadPlugins(PluginCollection.java:158)
at play.Play.init(Play.java:294)
at play.server.Server.main(Server.java:158)
This happens because eclipse copies play.plugins to the classes folder and play is trying to load the plugin twice.
Just ignore this exception.