LoginSignup
1
2

More than 3 years have passed since last update.

【第二回】SpringとMySqlとMyBatisの連動方法

Last updated at Posted at 2020-09-14

はじめに

DreamHanksの松下です。今回はSpringとMySqlとMyBatisの連動方法を解説していきます。

連載記事まとめ

xmlファイルの設定

新規でプロジェクトを作った際にデフォルトで生成される3つのxmlファイルをカスタマイズしていきます。

◆pom.xmlの設定

下記のdependency(依存するライブラリ)をデフォルトで生成されたpom.xmlの<dependencies>タグの中にコピペしてください。

pom.xmlの詳しい内容は下記の記事で解説しています。
Mavenの解説

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

        <!-- MyBatis 3.4.1 -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>

        <!-- MyBatis-Spring -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!-- Spring-jdbc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- Mybatis log -->
        <!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 -->
        <dependency>
            <groupId>org.bgee.log4jdbc-log4j2</groupId>
            <artifactId>log4jdbc-log4j2-jdbc4</artifactId>
            <version>1.16</version>
        </dependency>

上記pomの解説

<!-- MySQL -->      Mysql(DB)と接続するためのdependency
<!-- MyBatis 3.4.1 -->   MyBatisを使うためのdependency
<!-- MyBatis-Spring -->   MyBatisとSpringフレームワークを連動するためのdependency
<!-- Spring-jdbc -->    SpringJdbcで使うためのdependency
<!-- Mybatis log -->    MyBatisのログを出すためのdependency

◆root-context.xmlの設定

デフォルトの状態
root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>

設定後の状態
root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    <context:component-scan
        base-package="com.dreamhanks" />
    <context:component-scan
        base-package="com.dreamhanks.service" />



    <!-- MySQL dataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
        <property name="url"
            value="jdbc:log4jdbc:mysql://localhost:3306/work_db?useSSL=false&amp;serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="Daiki8863"></property>
    </bean>

    <!-- mybatis SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation"
            value="classpath:/mybatis-config.xml"></property>
        <property name="mapperLocations"
            value="classpath:mappers/**/*Mapper.xml"></property>
    </bean>

    <!-- mybatis -->
    <bean id="sqlSession"
        class="org.mybatis.spring.SqlSessionTemplate"
        destroy-method="clearCache">
        <constructor-arg name="sqlSessionFactory"
            ref="sqlSessionFactory"></constructor-arg>
    </bean>
</beans>

 ①設定後の状態のroot-context.xmlをコピペしてください。

 ②<!-- MySQL dataSource -->の設定
  ・下記のvalue値を自分のmysqlの環境に合わせる

       <property name="url"
            value="jdbc:log4jdbc:mysql://localhost:3306/work_db?useSSL=false&amp;serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="Daiki8863"></property>

   特にみるべきところ
    ・work_db → カレントスキーマ
    ・username, passwordを自分のmysqlの環境に合わせる。

 ③パッケージ名などは自分の環境にあわせること

root-context.xmlの詳しい内容は「xmlファイルの解説」という記事内で解説しています。
【Webアプリ開発】root-context.xmlの解説

    <context:component-scan
        base-package="com.dreamhanks" />
    <context:component-scan
        base-package="com.dreamhanks.service" />

◆servlet-context.xmlの設定

servlet-context.xml
  <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.dreamhanks.workmanager" />



</beans:beans>

 ①特に設定すべき個所はありませんが、下記の内容が自分の環境と合っているかを確認してください

  <context:component-scan base-package="com.dreamhanks.workmanager" />

servlet-context.xmlの詳しい内容は「xmlファイルの解説」という記事内で解説しています。
servlet-context.xmlの解説

おわりに

次回はMyBatis Generatorのインストール方法について解説していきます。
【第三回】MyBatis Generatorのインストール方法

最新内容については下記のリンク(DreamHanksのブログ)で確認することができます。
DreamHanksブログ(Javaウェブアプリ開発)

お読みくださいまして、ありがとうございました。
  

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2