mybaits3.2.8 alias Packet Scan wildcard character

Source: Internet
Author: User
Tags to domain

<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
  1. <Bean id= "sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
  2. <property name= "dataSource" ref="DataSource" />
  3. <property name= "configlocation" value="Classpath:/sqlmapconfig.xml"></ Property >
  4. <property name= "mapperlocations" value="Classpath*:/sqlmaps/**/*-sql.xml " ></Property>
  5. <property name= "typealiasespackage" value="Com.demo.domain" />
  6. </Bean>


After retrofit: Applicationcontext.xml configuration:

[HTML]View PlainCopy
  1. <Bean id= "sqlsessionfactory" class="Com.demo.core.mybatis.TQSqlSessionFactoryBean " >
  2. <property name= "dataSource" ref="DataSource" />
  3. <property name= "configlocation" value="Classpath:/sqlmapconfig.xml"></ Property >
  4. <property name= "mapperlocations" value="Classpath*:/sqlmaps/**/*-sql.xml " ></Property>
  5. <property name= "typealiasespackage" value="Com.demo.**.domain" />
  6. </Bean>


com.Demo.core.mybatis.TQSqlSessionFactoryBean class Source:

[Java]View PlainCopy
  1. Package com.demo.core.mybatis;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import Org.mybatis.spring.SqlSessionFactoryBean;
  5. Import Org.slf4j.Logger;
  6. Import Org.slf4j.LoggerFactory;
  7. Import Org.springframework.core.io.Resource;
  8. Import Org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  9. Import Org.springframework.core.io.support.ResourcePatternResolver;
  10. Import Com.demo.core.utils.StringUtil;
  11. /**
  12. * @ClassName: Tqsqlsessionfactorybean
  13. * @Description: MyBatis auto-Scan alias path (new wildcard match feature)
  14. * @author Wangxiaohu [email protected]
  15. * @date December 9, 2014 9:36:23
  16. */
  17. Public class Tqsqlsessionfactorybean extends Sqlsessionfactorybean {
  18. Logger Logger = Loggerfactory.getlogger (GetClass ());
  19. private static final String Root_path = "com" + File.separator + "Demo"
  20. + File.separator;
  21. private static final String Root_path_split = ",";
  22. private Static final string[] Path_replace_array = { "]"};
  23. public void Settypealiasespackage (String typealiasespackage) {
  24. if (! Stringutil.isstringavaliable (Typealiasespackage)) {
  25. super.settypealiasespackage (typealiasespackage);
  26. return;
  27. }
  28. Resourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();
  29. StringBuffer Typealiasespackagestringbuffer = new StringBuffer ();
  30. try {
  31. For (String location:typeAliasesPackage.split (",")) {
  32. if (! Stringutil.isstringavaliable (location)) {
  33. continue;
  34. }
  35. Location = "classpath*:"
  36. + Location.trim (). Replace (".", File.separator);
  37. Typealiasespackagestringbuffer.append (getresources (resolver,
  38. Location));
  39. }
  40. } catch (IOException e) {
  41. Logger.error (E.getmessage (), E);
  42. }
  43. if ("". Equals (Typealiasespackagestringbuffer.tostring ())) {
  44. throw New RuntimeException (
  45. "MyBatis typealiasespackage path scan Error! Please check [email protected] Configuration!  ");
  46. }
  47. Typealiasespackage = Replaceresult (
  48. Typealiasespackagestringbuffer.tostring ()). Replace (
  49. File.separator, ".");
  50. super.settypealiasespackage (typealiasespackage);
  51. }
  52. private String getresources (resourcepatternresolver resolver,
  53. String location) throws IOException {
  54. StringBuffer Resourcepathstringbuffer = new StringBuffer ();
  55. For (Resource resource:resolver.getResources) {
  56. String Description = Resource = = null? "": Resource
  57. . GetDescription ();
  58. if (! Stringutil.isstringavaliable (Resource.getdescription ())
  59. || Description.indexof (root_path) = =-1) {
  60. continue;
  61. }
  62. Resourcepathstringbuffer.append (
  63. Description.substring (Description.indexof (Root_path)))
  64. . append (Root_path_split);
  65. }
  66. return resourcepathstringbuffer.tostring ();
  67. }
  68. private String Replaceresult (String resultstr) {
  69. For (String replacestr:path_replace_array) {
  70. ResultStr = Resultstr.replace (replacestr, "");
  71. }
  72. return resultstr;
  73. }
  74. }

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

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.