Aug23

【原创】MyBatis Generator使用小记

Author: leeon  Click: 15954   Comments: 1 Category: java  Tag: mybatis,generator

最近在新的项目中使用Mybatis提供的generator工具来自动生成代码,为了提高效率,我是用mybatis提供的eclipse插件来生成代码,在使用过程中遇到一些问题,在此记录下来:

1. 默认mybatis generator会在生成普通代码同时附加example的示例代码,通常我们不需要这些东西,生成出来清理也麻烦,可以在generator的xml配置文件的table配置项中添加

enableCountByExample="false"

enableUpdateByExample="false" enableDeleteByExample="false"

enableSelectByExample="false" selectByExampleQueryId="false"

来关闭example代码的输出。

2. 工具在eclipse中使用有可能会提示jdbc找不到,这时候在配置文件中添加classPathEntry配置项即可,将mysql的jdbc包引用进来,类似如下配置:

[code="xml"]
<classPathEntry location="C:\Users\leo\.m2\repository\mysql\mysql-connector-java\5.1.26\mysql-connector-java-5.1.26.jar"/>
[/code]

3. 配置文件中commentGenerator配置项最好不要设置为不生成注释,因为这样带来的问题是在自动生成的代码上不会存在@mbggenerated 这样的标记,这个标记是用来标注这些代码是代码生成器生成的,如果关闭了注释,我们在多次使用mybatis generator重新生成代码时会导致在已有生成的文件上追加生成重复的内容,导致代码错误,故我建议如果你只生成一次代码,以后再不用工具生成可以关闭注释,如果会重复生成就要把注释开关打开。我的配置参数是关闭时间注释:

[code="xml"]
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
[/code]

4 我们可以在生成好的代码上继续添加我们要实现的功能代码,但是在用中文注释代码的时候,我们再用generator重新生成代码,我们已有代码是不会被覆盖和修改的,但是中文注释有可能变成乱码,这是因为我们在工具配置文件上没有全局指定文件编码,只要我们在context的配置区域里面加入如下配置即可:

[code="xml"]
<property name="javaFileEncoding" value="UTF-8"/>
[/code]

5. mybatis generator生成的代码注释比较丑陋,同时也不支持在model代码中自动添加数据库字段注释,我们可以通过修改插件源代码来实现。从官网下载插件

https://github.com/mybatis/generator/tree/master/core/mybatis-generator-core

核心代码,修改/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/DefaultCommentGenerator.java

文件,其中addFieldComment 方法的参数introspectedColumn即包含了数据库字段的相关数据,调用的方法是getRemarks ,怎么修改我就不说了,你懂的。



Jun21

【原创】在Spring3中加入veloctiy模板引擎的注意事项

Author: leeon  Click: 5116   Comments: 0 Category: java  Tag: java,spring3,velocity

今天在spring3项目中学习配置velocity时遇到一个很奇怪的问题,通常eclipse的ide在编写xml中都能通过智能提示来提示一个bean类的属性,但是不管我怎么按照网上的说法这样写:

 

智能提示给与的提示只有两个属性:velocityEngine 和servletContext ,进入到org.springframework.web.servlet.view.velocity.VelocityConfigurer 这个类的源码查看,发现其是继承于父类VelocityEngineFactory,然后用eclipse跳转到的方式试图进入VelocityEngineFactory 父类查看,发现无法跳过去,这下问题就清楚了,依赖的spring3包不完整,搜索发现stackoveflow上有人回答了类似问题:http://stackoverflow.com/questions/19752563/using-apache-velocity-with-spring-framework-3-2-4

果断在maven中加入了spring-context-support的依赖配置即可:

org.springframework

spring-context-support

3.2.4.RELEASE


May14

【原创】ActiveMQ连接池在Spring配置中的注意事项

Author: leeon  Click: 7086   Comments: 0 Category: java  Tag: spring3,activemq,pool

今天在spring3中集成配置activemq时发现使用PooledConnectionFactory来编写bean怎么都无法启动,提示创建bean失败(org.springframework.beans.factory.BeanCreationExcept)

我所使用的版本是activemq-pool 5.8版,发现老外也有遇到同样的问题,说有可能是common-dbcp引用的版本不对造成的。

http://techempowers.wordpress.com/2013/09/24/activemq-beancreationexception-pooledconnectionfactory-malformedparameterizedtypeexception/

https://issues.apache.org/jira/browse/AMQ-4189

查看我的工程中没有引用到common-dbcp的包,反而common-pool的包有两个,一个1.5.5版本一个1.6版本,于是将1.5.5版本的jar引用在工程中去掉然后再测试发现bean可以创建成功了。跟踪common-pool的依赖关系发现是jedis的jar包关联有用导致不同的api包引用了不同版本的common-pool包所致,然而active-pool的包必须依赖1.6版本的才能启动。


总结: 只要按照规则编写好bean配置,一定要先检查依赖的包是否符合版本要求,同时检查是否有同一个模块包不同版本的引用

May7

【原创】Mybatis插件推荐-mybatipse

Author: leeon  Click: 11965   Comments: 0 Category: java  Tag: mybatipse,eclipse,mybatis

在eclipse下使用mybatis时,编写sql的xml没有智能提示还是一件很麻烦的事情,同时在dao层的方法要去映射到xml中的id名,跳转也是一件操作繁琐的事情,最近在eclipse官网发现了一个比较还用的mybatis插件,可以解决我们在使用mybatis时不方便的问题。

插件地址:http://marketplace.eclipse.org/content/mybatipse

XML Editor Enhancements

Auto-completion

  • Mapper namespace : Calculated from the path.
    autocomplete namespaceautocomplete namespace
  • Java class/Type alias : Propose Java classes and Type Aliases (translucent icons). Camelcase match is supported. [1] [2]
    autocomplete classautocomplete class
  • Java property : Propose properties of the parent java class. Nested properties are supported.
    autocomplete propertyautocomplete property
  • Statement ID : If corresponding mapper interface exists, propose the method name as a statement ID.
    autocomplete statement id
  • Reference to resultMap/select/sql : Propose ID of the referenced resultMap/select/sql. External reference is supported (blue icons).
    autocomplete reference
  • Parameter properties : #{} and ${} in select/insert/udpate/delte statements. Also works in some attributes.
    autocomplete propertiesautocomplete properties 2
  • javaType, jdbcType, typeHandler in #{}
    autocomplete jdbcType
  • Result elements in resultMap/collection/association : Generates elements for the parent java class properties.
    autocomplete results 1autocomplete results 2

  • [1] For better performance, classes in the application libraries (loaded as JARs) are listed only when at least one package segment is specified.

  • [2] There is a limitation for the type alias support. Basically, if type aliases are registered using Java API, they are not automatically detected. As a workaround, you can register type aliases in MyBatipse's preference. See the Configuration section.

Hyperlinks

  • To referenced resultMap/select/sql element. External reference is supported.
  • To mapper interface method.

Validation

  • Missing TypeAlias, Java class/property.
  • Missing resultMap/select/sql ID.
  • etc.
    refid errorproblem view

Java Editor Enhancements

Auto-completion

  • Parameter properties : #{} and ${} in select/insert/udpate/delte annotations. autocomplete properties in java
  • javaType, jdbcType, typeHandler in #{} : See the XML example.
  • Reference to resultMap in @ResultMap
    autocomplete resultmap annotation

Quick Assist

  • Add @Param to method parameters : Put the cursor on the method name and press cmd + 1 (or ctrl + 1).
    param-annotation1
    param-annotation2

Configuration

MyBatis Nature

  • By default, each file is validated when it's saved, but you would want to validate the XML mapper when the related Java classes are updated as well. For that purpose, MyBatipse provides a custom nature MyBatis Nature.
    To add the nature, right click the project in the package explorer and choose Configure -> Add MyBatis Nature from the menu.
    nature

Type Alias

  • Type alias are automatically detected if they are registered using XML config file (both mybatis and mybatis-spring config is supported).
    If you register type aliases in Java code, MyBatipse cannot detect them. As a workaround, you can register custom type aliases in the project setting (Open Project -> Properties menu and select MyBatipse from the left column).
    type aliases
    For example, with the above settings:
    • The first entry registers all the classes in com.example.domain package as type aliases.
    • The second entry registers the single class domain.Person.
    • The third entry registers the single class domain.SomeLongNamedBean under the alias SomeBean.
    • Note that, in any case, MyBatipse respects @Alias annotation if it exists.

Tips

  • To move MyBatis proposals to the top of the XML proposal list, quit Eclipse and open the following file in the workspace: .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.xml.ui.prefs Find the line started with xml_content_assist_default_page_sort_order= and insertnet.harawata.mybatis.proposalCategory.xml\u0000 right after the equal sign.
    xml proposal order

分类

标签

归档

最新评论

Abyss在00:04:28评论了
Linux中ramdisk,tmpfs,ramfs的介绍与性能测试
shallwe99在10:21:17评论了
【原创】如何在微信小程序开发中正确的使用vant ui组件
默一在09:04:53评论了
Berkeley DB 由浅入深【转自架构师杨建】
Memory在14:09:22评论了
【原创】最佳PHP框架选择(phalcon,yaf,laravel,thinkphp,yii)
leo在17:57:04评论了
shell中使用while循环ssh的注意事项

我看过的书

链接

其他

访问本站种子 本站平均热度:8306 c° 本站链接数:29 个 本站标签数:464 个 本站被评论次数:94 次