Use IDEA Mybatis plugin generate mapper files 2016-06-21 09:02

Mybatis is a light weight Java persistence framework(compared with Hibernate). You have to write a lot of mapper xml files and mapper interfaces. These things are duplicate works, we can generate them by IDEA Mybatis plugin. In this page I will show you how to use this plugin to generate xml mapper file and mapper interface.

  1. install Mybatis plugin

    Search Mybatis plugin in Intellij repositories and click install. IDEA Mybatis plugin

  2. create mybatis-generator-config file

    Right click resources folder choose New | mybatis-generator-config. I named this file "mybatis-generator.xml" you can choose any name you like.

    IDEA Mybatis plugin

  3. the content of mybatis-generator.xml like following.

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE generatorConfiguration PUBLIC
            "-// MyBatis Generator Configuration 1.0//EN"
            "" >
        <!-- !!!! Driver Class Path !!!! -->
        <classPathEntry location="C:\Users\Administrator\.IntelliJIdea15\config\jdbc-drivers\postgresql-9.4-1201.jdbc4.jar"/>
        <context id="context" targetRuntime="MyBatis3">
                <property name="suppressAllComments" value="false"/>
                <property name="suppressDate" value="true"/>
            <!-- !!!! Database Configurations !!!! -->
            <jdbcConnection driverClass="org.postgresql.Driver"
                            connectionURL="jdbc:postgresql://localhost:5432/users" userId="postgres" password="postgres"/>
                <property name="forceBigDecimals" value="false"/>
            <!-- !!!! Model Configurations !!!! -->
            <javaModelGenerator targetPackage="com.henryxi.mybatis.entity" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED">
                <property name="enableSubPackages" value="false"/>
                <property name="trimStrings" value="true"/>
            <!-- !!!! Mapper XML Configurations !!!! -->
            <sqlMapGenerator targetPackage="mapper" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED">
                <property name="enableSubPackages" value="false"/>
            <!-- !!!! Mapper Interface Configurations !!!! -->
            <javaClientGenerator targetPackage="com.henryxi.mybatis.mapper" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED"
                <property name="enableSubPackages" value="false"/>
            <!-- !!!! Table Configurations !!!! -->
            <table tableName="users" modelType="flat" domainObjectName="UserInfoEntity">
                <property name="ignoreQualifiersAtRuntime" value="true"/>
                <property name="modelOnly" value="false"/>
  4. This plugin won't create package you need create package and folder manually. The structure of this demo at the end of this page.

  5. Right click "mybatis-generator" and choose Run as Mybatis Generator. After generating move the mapper xml files to resource folder.

    run config

  6. the structure of this demo like following.

        │  └─com
        │      └─henryxi
        │          └─mybatis
        │              │
        │              │
        │              ├─entity
        │              │
        │              │
        │              │
        │              └─mapper
            │  mybatis-config.xml
            │  mybatis-generator.xml
  7. the code of java and mybatis config

    public class Client {
        public static void main(String[] args) throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            try {
                UserInfoEntityMapper mapper = sqlSession.getMapper(UserInfoEntityMapper.class);
                UserInfoEntity userInfoEntity = new UserInfoEntity();
                UserInfoEntityExample example = new UserInfoEntityExample();
                List<UserInfoEntity> allRecords = mapper.selectByExample(example);
            } finally {


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
            PUBLIC "-// Config 3.0//EN"
            <!-- Globally enables or disables any caches configured in any mapper under this configuration -->
            <setting name="cacheEnabled" value="true"/>
            <!-- Sets the number of seconds the driver will wait for a response from the database -->
            <setting name="defaultStatementTimeout" value="3000"/>
            <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <!-- Allows JDBC support for generated keys. A compatible driver is required.
            This setting forces generated keys to be used if set to true,
             as some drivers deny compatibility but still work -->
            <setting name="useGeneratedKeys" value="true"/>
        <!-- Continue going here -->
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="org.postgresql.Driver"/>
                    <property name="url" value="jdbc:postgresql://localhost:5432/users"/>
                    <property name="username" value="postgres"/>
                    <property name="password" value="postgres"/>
            <mapper resource="mapper/UserInfoEntityMapper.xml"/>