miyazawa3830nami / example

0 stars 0 forks source link

Mybatis Generatorの導入 #3

Open dimetil810 opened 1 year ago

dimetil810 commented 1 year ago

pom.xmlのpluginsに以下を追記

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.1</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                    </execution>
                </executions>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
                    <dependency>
                                         <groupId>org.postgresql</groupId>
                                         <artifactId>postgresql</artifactId>
                                         <version>42.6.0</version>
                                         </dependency>
                </dependencies>
            </plugin>
dimetil810 commented 1 year ago

/resources直下に以下ファイルを配置

<?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>
    <context id="MySQLTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.MapperAnnotationPlugin"/>
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <!--        DB接続情報:各自の環境に合わせて編集して使う-->
        <jdbcConnection driverClass="org.postgresql"
                        connectionURL="jdbc:mysql://localhost:33306/"
                        userId="root"
                        password="developer">
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>
        <javaModelGenerator targetPackage="java.com.exsample.exsample.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="java.com.exsample.exsample.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <javaClientGenerator targetPackage="java.com.exsample.exsample.mapper" targetProject="src/main/java"
                             type="MIXEDMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="%"
               enableInsert="true"
               enableSelectByPrimaryKey="true"
               enableSelectByExample="true"
               enableUpdateByPrimaryKey="true"
               enableUpdateByExample="true"
               enableDeleteByPrimaryKey="true"
               enableDeleteByExample="true"
               enableCountByExample="true"
               selectByExampleQueryId="true"
               modelType="flat">
               <property name="useActualColumnNames" value="false"/>
        </table>
    </context>
</generatorConfiguration>
dimetil810 commented 1 year ago

以下サイトを参考にしてpostgresの接続をできるようにする https://qiita.com/charon/items/9946aababa76d586fe6c#%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%ABmybatis-generator%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B

dimetil810 commented 1 year ago

MyBatis Generatorがなぜか動かないため、一旦手動でMapperを作成するとよさそう
https://qiita.com/TaikiTkwkbysh/items/9f598ed01c363b59fd98

dimetil810 commented 1 year ago

PostgresSQLで権限を追加する

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema_name TO user_name;
dimetil810 commented 1 year ago

PostgresSQLでsequenceの権限を追加する

GRANT USAGE, SELECT ON SEQUENCE sequence_name TO user_name;