MyBatis の Mapper をサービスに DI する方法 (2) [開発]
今回も MyBatis の Mapper をサービスに DI するサンプルを作成します。
前回はセッターインジェクションを使いましたが、今回は Autowired アノテーションを使います。
Autowired アノテーションを使うことで Spring 設定ファイルを簡略化できます。
MyBatis と Spring Framework の環境構築方法は下記を参照してください。
10/1 ブログ (MyBatis のサンプルを作ってみる)
(1) データベースの準備
MySQLをインストールし、テスト用に以下のようなテーブルを準備しました。
(2) Springの設定ファイル (applicationContext.xml) を編集します。
前回からの変更分は赤字で記載しています。
(3) Mapper を作成します。(変更なし)
(4) HelloService インタフェースを作成します。(変更なし)
(5) HelloServiceImpl を作成します。
前回からの変更分は赤字で記載しています。
(6) 動作確認用コードを作成します。変更なし。
実行すると「おはよう」と表示されます。
前回はセッターインジェクションを使いましたが、今回は Autowired アノテーションを使います。
Autowired アノテーションを使うことで Spring 設定ファイルを簡略化できます。
MyBatis と Spring Framework の環境構築方法は下記を参照してください。
10/1 ブログ (MyBatis のサンプルを作ってみる)
(1) データベースの準備
MySQLをインストールし、テスト用に以下のようなテーブルを準備しました。
(2) Springの設定ファイル (applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/mybatis" />
<property name="username" value="mybatis" />
<property name="password" value="mybatis" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Mapper -->
<bean id="helloMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="sample.HelloMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<mybatis:scan base-package="sample" />
<!-- サービス -->
<bean id="helloService" class="sample.HelloServiceImpl" />
<property name="helloMapper" ref="helloMapper" />
</bean>
</beans>
(3) Mapper を作成します。(変更なし)
package sample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface HelloMapper {
@Select("select * from tbl_hello where id = #{id}")
Hello getById(@Param("id") int id);
}
(4) HelloService インタフェースを作成します。(変更なし)
package sample;
public interface HelloService {
public Hello hello(int msgId);
}
(5) HelloServiceImpl を作成します。
前回からの変更分は赤字で記載しています。
package sample;
public class HelloServiceImpl implements HelloService {
@Autowired
private HelloMapper helloMapper;
public void setHelloMapper(HelloMapper helloMapper) {
this.helloMapper = helloMapper;
}
@Override
public Hello hello(int msgId) {
Hello ret = this.helloMapper.getById(msgId);
return ret;
}
}
(6) 動作確認用コードを作成します。変更なし。
public class SampleMain {
public static void main(String[] args) {
String resource =
"src/main/webapp/WEB-INF/applicationContext.xml";
ApplicationContext ac =
new FileSystemXmlApplicationContext(resource);
HelloService helloService = (HelloService) ac.getBean("helloService");
Hello hello = helloService.hello(1);
System.out.println(hello.getMessage());
}
}
実行すると「おはよう」と表示されます。
オブジェクト指向のこころ (SOFTWARE PATTERNS SERIES)
- 作者: アラン・シャロウェイ
- 出版社/メーカー: 丸善出版
- 発売日: 2014/03/11
- メディア: 単行本(ソフトカバー)
コメント 0