我可以在mybatis中从schema / sql /和sql查询创建类吗?

时间:2015-03-13 21:40:44

标签: orm schema mybatis creation

我比OO更像数据库人(即使我能做到)。所以,我有一个数据库作为sql字符串,以及很多查询(当然也包括内容)。实际上,我并不想“将其键入”到OO模型(例如class Person int id get/set(int id)等) - SQL非常快而不是那样。客户要求使用ORM。

是否有可以为我做到这一点的工具? (我宁愿编写自己的解析器来执行该过程,而不是“编写”所有的)。另外,如果mybatis无法做到,是否有其他ORM软件可以这样做? (任何ORM软件都可以,实际上,只是说mybatis是轻量级的,与sql类似)。

1 个答案:

答案 0 :(得分:1)

你可以使用Mybatis generator plugin非常简单。

如果您在项目中使用maven,那么基本的插件配置

<pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.mybatis.generator</groupId>
                                        <artifactId>mybatis-generator-maven-plugin</artifactId>
                                        <versionRange>[1.3.2,)</versionRange>
                                        <goals>
                                            <goal>generate</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/kp-mybatis-generator.xml</configurationFile>
                    <outputDirectory>${basedir}/src/main/java</outputDirectory>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

生成器xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <classPathEntry location="C:\down\ojdbc6.jar" />

    <context id="kpOracleGens" targetRuntime="MyBatis3Simple">
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
            connectionURL="jdbc:oracle:thin:@localhost:1521:KPORACLE" userId="******"
            password="******">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.kp.swasthik.db.domain"
            targetProject="MAVEN">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.kp.swasthik.db.persistence"
            targetProject="MAVEN">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.kp.swasthik.db.mapper" targetProject="MAVEN"
            implementationPackage="com.kp.swasthik.db.service">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="%KPC%" catalog="*" schema="kprasad">
            <property name="useActualColumnNames" value="true" />
        </table>
        <table tableName="FOO" catalog="*" schema="kprasad">
            <property name="useActualColumnNames" value="true" />
        </table>
    </context>
</generatorConfiguration>