LoginSignup
1
0

More than 5 years have passed since last update.

SpringMVCとMyBatis2.xの連携

Posted at

pom.xmlに追加した依存

    <!-- MyBatis -->
    <dependency>
        <groupId>org.apache.ibatis</groupId>
        <artifactId>ibatis-core</artifactId>
        <version>3.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-ibatis</artifactId>
        <version>2.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ibatis</groupId>
        <artifactId>ibatis-sqlmap</artifactId>
        <version>2.3.0</version>
    </dependency>

この記述によってibatisのライブラリが使えるようになります。

sqlMapConfig.xml

<sqlMapConfig>
    <transactionManager type="JDBC">
        <dataSource type="SIMPLE">
            <property name="JDBC.Driver" value="org.postgresql.Driver"/>
            <property name="JDBC.ConnectionURL" value="jdbc:postgresql://localhost:5433/spring"/>
            <property name="JDBC.Username" value="postgres"/>
            <property name="JDBC.Password" value="postgres"/>
        </dataSource>
    </transactionManager>

    <sqlMap resource="sample/Friendry.xml" />


</sqlMapConfig>

今回はPostgreSQLを使用しました。

Friendly.xml

<sqlMap namespace="Friendly">
    <resultMap id="ownerResult" class="sample.biz.domain.Owner">
        <result property="ownerId" column="OWNER_ID"/>
        <result property="ownerName" column="OWNER_NAME"/>
    </resultMap>

    <!-- Owner SQL -->
    <select id="findOwner" parameterClass="java.lang.String" resultMap="ownerResult">
        select OWNER_ID, OWNER_NAME from OWNER
            where OWNER_ID = #value#
    </select>

</sqlMap>

sqlMapConfigから参照されます。
resultMapタグで、ドメインクラスにSQLの列名を紐づけています。
この宣言をすることで、自動でドメインクラスのプロパティにselect分の結果を格納してくれます。
selectタグにはidをつけてあげます。
このselect分を呼び出すときに指定する名前になります。
parameterClassは#value#の位置に入る引数の型です。
resultMapに先ほど宣言したresultMapを参照させます。

SampleRun

@Controller
public class SampleRun {

    @RequestMapping("/hello")
    public String hello(Model model) {
        try {
            Reader reader = Resources.getResourceAsReader("sample/sqlMapConfig.xml");
            SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();

            Owner owner = (Owner)sqlMap.queryForObject("findOwner", "2");

            model.addAttribute("owner", owner);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return "showMessage";
    }
}

実行クラスになります。
/helloでアクセスした場合にhelloが呼ばれます。
Modelを引数に受け取ることで、model.addAttributeが使えます。
SqlMapClientをビルドして、先ほど作ったfindOwnerに"2"の値を渡してselect分を実行しています。
最後にmodelにOwnerをセットしてjspに遷移します。

Owner.java

public class Owner {
    private int ownerId;
    private String ownerName;

    public int getOwnerId() {
        return ownerId;
    }
    public void setOwnerId(int ownerId) {
        this.ownerId = ownerId;
    }
    public String getOwnerName() {
        return ownerName;
    }
    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }
}

ドメインクラスです。
こちらは普通ですね。

まとめ

今回はSqlMapClientを使用してMyBatisを使ってみました。
Springが初めて学ぶことだったので、難しかったです。
MyBatis2.xを使用したのですが、今現場で使われているのはずっと最新のMyBatisのようです。

1
0
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
0