Mybatis Generator:将“自动生成的"广告素材分离出来的最佳方法是什么?和“手动编辑的文件"

问题描述:

我在一个使用 Mybatis (用于将Java持久存储到数据库)和 Mybatis Generator (用于从数据库自动生成映射器xml文件和Java接口)的项目中模式).

I am on a project that uses both Mybatis (for persisting java to database) and Mybatis Generator (to automatically generate the mapper xml files and java interfaces from a database schema).

Mybatis生成器在生成基本Crud操作所需的文件方面做得很好.

Mybatis generator does a good job at generating the files necessary for basic crud operation.

上下文

对于某些表/类,我们将需要比MyBatis生成器工具生成的填充"更多的填充"(代码查询等).

For some of the tables/classes, we will need more "stuff" (code queries, etc) than the "crud stuff" generated by the MyBatis Generator tool.

有什么办法可以做到两全其美",即使用自动生成功能还是使用自定义代码".如何分离和构造手工编辑的文件"和自动生成的文件".

Is there any way to have "best of both worlds", i.e use auto generation as as well as "custom code". How do you separate out and structure the "hand edited files" and "automatically generated files".

提案

我正在考虑以下内容,即表"Foo"

I was thinking about the following, i.e. for table "Foo"

自动生成

  • FooCrudMapper.xml
  • 界面FooCrud.java

(其中"Crud"代表创建读取更新删除")

(where "Crud" stands for "Create Read Update Delete")

手工编辑

  • FooMapper.xml
  • 界面Foo扩展了FooCrud

概念:如果更改了架构,您始终可以安全地自动生成"Crud" xml和.java文件,而无需清除任何自定义更改.

The notion: if the schema changed, you could always safely autogenerate the "Crud" xml and .java files without wiping out any of the custom changes.

问题

  • mybatis是否可以正确处理这种情况,即该映射器会正确执行自动生成的原始代码"吗?

  • Would mybatis correctly handle this scenario, i.e. would this mapper correctly execute the auto-generated 'crud code'?

FooMapper fooMapper = sqlSession.getMapper(FooMapper.class);

FooMapper fooMapper = sqlSession.getMapper(FooMapper.class);

您推荐什么方法?

*我们的数据库设计使用核心表"(元素"),其他表扩展"该表并添加额外的属性(共享键).我看了看文档,资料来源得出结论,未经任何手动编辑,我无法将Mybatis Generator与此类扩展名"结合使用:

Edit 1: * Our db design uses a 'core table' ("element") with other tables 'extending' that table and adding extra attributes (shared key) . I've looked at docs and source concluded that I cannot use Mybatis Generator in conjunction with such 'extension' without any hand editing:

即这是行不通的. -ElementMapper扩展"ElementCrudMapper" -FooMapper.xml扩展了"ElementCrudMapper"和"FooCrudMapper"

i.e. This does not work. -ElementMapper extends "ElementCrudMapper" -FooMapper.xml extends both "ElementCrudMapper" and "FooCrudMapper"

谢谢!

我可以分离出生成的文件和手工编辑的文件.

I can seperate out generated files and hand edited files.

我使用mybatis-spring和spring管理dao接口.该库允许MyBatis参与Spring事务,负责构建MyBatis映射器和SqlSessions并将它们注入其他bean,将MyBatis异常转换为Spring DataAccessExceptions,最后,它使您可以构建不依赖于MyBatis,Spring或Java的应用程序代码. MyBatis-Spring .

I use mybatis-spring and spring to manage dao interfaces. This library allows MyBatis to participate in Spring transactions, takes care of building MyBatis mappers and SqlSessions and inject them into other beans, translates MyBatis exceptions into Spring DataAccessExceptions, and finally, it lets you build your application code free of dependencies on MyBatis, Spring or MyBatis-Spring.

对于DAO接口,我编写了一个通用的MybatisBaseDao来表示由mybatis生成器生成的基本接口.

For DAO Interfaces, I write a generic MybatisBaseDao to represent base interface generated by mybatis generator.

    public interface MybatisBaseDao<T, PK extends Serializable, E> {
    int countByExample(E example);

    int deleteByExample(E example);

    int deleteByPrimaryKey(PK id);

    int insert(T record);

    int insertSelective(T record);

    List<T> selectByExample(E example);

    T selectByPrimaryKey(PK id);

    int updateByExampleSelective(@Param("record") T record, @Param("example") E example);

    int updateByExample(@Param("record") T record, @Param("example") E example);

    int updateByPrimaryKeySelective(T record);

    int updateByPrimaryKey(T record);
    }

当然,您可以根据需要自定义BaseDao.例如,我们有一个UserDao,那么您可以像这样

Of course, you can custom your BaseDao according to your demand. For example we have a UserDao, Then you can defind it like this

public interface UserDao extends MybatisBaseDao<User, Integer, UserExample>{
    List<User> selectUserByAddress(String address); // hand edited query method
}

对于mapper xml文件,我在mapper(.xml)基本文件夹中创建了两个包,以将生成的文件和手动编辑的文件分开.对于上面的UserDao,我将生成器生成的UserMapper.xml放在名为生成"的程序包中.我将所有编写映射器sql的手放在名为manual的程序包中的另一个UserMapper.xml文件中.这两个映射器文件以相同的头文件<mapper namespace="com.xxx.dao.UserDao" >开头. Mybatis可以扫描xml映射器文件以自动映射sql和相应的接口方法.

For mapper xml files, I create two packages in mapper(.xml) base folder to separate generated files and hand edited files. For UserDao above, I put UserMapper.xml generated by generator in package named 'generated'. I put all hand writing mapper sqls into another UserMapper.xml file in the package named manual. The two mapper files start with the same header <mapper namespace="com.xxx.dao.UserDao" >. Mybatis can scan the xml mapper files to map sql and corresponding interface method automatically.

对于生成的实体和示例对象,我直接将其覆盖.

希望以上方法对您有所帮助!

I hope the method above can help you!