<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.generator.version>1.3.2</mybatis.generator.version>
MBG
Http://www.mybatis.org/spring/apidocs/reference/org/mybatis/spring/SqlSessionFactoryBean.html
These days build a simple framework for spring4.1.2+mybatis3.2.8.
Discover MyBatis's Sqlsessionfactorybean can configure the Typealiasespackage property to automatically alias domain.
If my domain is under a different package, then this configuration does not support wildcard scan package path? The following modifications:
Before Retrofit: Applicationcontext.xml configuration:
[HTML]View PlainCopy
- <Bean id= "sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
- <property name= "dataSource" ref="DataSource" />
- <property name= "configlocation" value="Classpath:/sqlmapconfig.xml"></ Property >
- <property name= "mapperlocations" value="Classpath*:/sqlmaps/**/*-sql.xml " ></Property>
- <property name= "typealiasespackage" value="Com.demo.domain" />
- </Bean>
After retrofit: Applicationcontext.xml configuration:
[HTML]View PlainCopy
- <Bean id= "sqlsessionfactory" class="Com.demo.core.mybatis.TQSqlSessionFactoryBean " >
- <property name= "dataSource" ref="DataSource" />
- <property name= "configlocation" value="Classpath:/sqlmapconfig.xml"></ Property >
- <property name= "mapperlocations" value="Classpath*:/sqlmaps/**/*-sql.xml " ></Property>
- <property name= "typealiasespackage" value="Com.demo.**.domain" />
- </Bean>
com.Demo.core.mybatis.TQSqlSessionFactoryBean class Source:
[Java]View PlainCopy
- Package com.demo.core.mybatis;
- Import Java.io.File;
- Import java.io.IOException;
- Import Org.mybatis.spring.SqlSessionFactoryBean;
- Import Org.slf4j.Logger;
- Import Org.slf4j.LoggerFactory;
- Import Org.springframework.core.io.Resource;
- Import Org.springframework.core.io.support.PathMatchingResourcePatternResolver;
- Import Org.springframework.core.io.support.ResourcePatternResolver;
- Import Com.demo.core.utils.StringUtil;
- /**
- * @ClassName: Tqsqlsessionfactorybean
- * @Description: MyBatis auto-Scan alias path (new wildcard match feature)
- * @author Wangxiaohu [email protected]
- * @date December 9, 2014 9:36:23
- */
- Public class Tqsqlsessionfactorybean extends Sqlsessionfactorybean {
- Logger Logger = Loggerfactory.getlogger (GetClass ());
- private static final String Root_path = "com" + File.separator + "Demo"
- + File.separator;
- private static final String Root_path_split = ",";
- private Static final string[] Path_replace_array = { "]"};
- public void Settypealiasespackage (String typealiasespackage) {
- if (! Stringutil.isstringavaliable (Typealiasespackage)) {
- super.settypealiasespackage (typealiasespackage);
- return;
- }
- Resourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();
- StringBuffer Typealiasespackagestringbuffer = new StringBuffer ();
- try {
- For (String location:typeAliasesPackage.split (",")) {
- if (! Stringutil.isstringavaliable (location)) {
- continue;
- }
- Location = "classpath*:"
- + Location.trim (). Replace (".", File.separator);
- Typealiasespackagestringbuffer.append (getresources (resolver,
- Location));
- }
- } catch (IOException e) {
- Logger.error (E.getmessage (), E);
- }
- if ("". Equals (Typealiasespackagestringbuffer.tostring ())) {
- throw New RuntimeException (
- "MyBatis typealiasespackage path scan Error! Please check [email protected] Configuration! ");
- }
- Typealiasespackage = Replaceresult (
- Typealiasespackagestringbuffer.tostring ()). Replace (
- File.separator, ".");
- super.settypealiasespackage (typealiasespackage);
- }
- private String getresources (resourcepatternresolver resolver,
- String location) throws IOException {
- StringBuffer Resourcepathstringbuffer = new StringBuffer ();
- For (Resource resource:resolver.getResources) {
- String Description = Resource = = null? "": Resource
- . GetDescription ();
- if (! Stringutil.isstringavaliable (Resource.getdescription ())
- || Description.indexof (root_path) = =-1) {
- continue;
- }
- Resourcepathstringbuffer.append (
- Description.substring (Description.indexof (Root_path)))
- . append (Root_path_split);
- }
- return resourcepathstringbuffer.tostring ();
- }
- private String Replaceresult (String resultstr) {
- For (String replacestr:path_replace_array) {
- ResultStr = Resultstr.replace (replacestr, "");
- }
- return resultstr;
- }
- }
Off Topic:
@org.apache.ibatis.type.alias (value = "user") annotations can be added to domain under the Typealiasespackage configuration path, and if this annotation is added, the alias uses the name specified by this annotation. If not configured, the default is the first letter of the class name lowercase.
http://blog.csdn.net/wsmalltiger/article/details/41825375
mybaits3.2.8 alias Packet Scan wildcard character