MyBatis の実践的(?)利用例
早く Hibernate の話を書きたいんですがグッとこらえてもう一つ MyBatis の話を。前回の Customers に続き CustomerSearch に MyBatis を導入します。
元の CustomerSearch から流用するもの
web.xml, velocity-toolbox.xml, velocity.properties, Customer.java, index.jsp(ダミーページ), search.vm, search.css
struts-config.xml の修正
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="searchform" type="org.apache.struts.action.DynaActionForm"> <form-property name="id" type="java.lang.Integer" /> <form-property name="name" type="java.lang.String" /> <form-property name="phone" type="java.lang.String" /> </form-bean> </form-beans> <action-mappings> <action path="/search" name="searchform" type="jp.mydns.akanekodou.SearchAction" input="/search.vm" /> </action-mappings> </struts-config>
自前で用意した SearchActionForm.java を破棄して DynaActionForm に切り替えます。
SearchAction.java の修正
package jp.mydns.akanekodou; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import jp.mydns.akanekodou.dao.CustomerDAO; import jp.mydns.akanekodou.model.Customer; public class SearchAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dform = (DynaActionForm)form; int id = (int)dform.get("id"); Customer cust = new CustomerDAO().find(id); dform.set("name", cust.getName()); dform.set("phone", cust.getPhone()); return mapping.getInputForward(); } }
DynaActionForm への切り替えに伴う修正。
CustomerMapper.java の作成と customer-mapper.xml の作成
CustomerMapper.java
package jp.mydns.akanekodou.dao.mapper; import jp.mydns.akanekodou.model.Customer; public interface CustomerMapper { Customer selectOne(int id); }
customer-mapper.xml (jp.mydns.akanekodou.dao.mapper 内に作成、コピー & ペーストして必要な部分を修正すると楽)
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="jp.mydns.akanekodou.dao.mapper.CustomerMapper"> <resultMap id="customerResultMap" type="Customer"> <id property="id" column="顧客ID" /> <result property="name" column="顧客名" /> <result property="phone" column="連絡先" /> </resultMap> <select id="selectOne" parameterType="int" resultMap="customerResultMap"> SELECT * FROM 顧客マスタ WHERE 顧客ID = #{id} </select> </mapper>
CustomerDAO.java の作成
コンストラクタまでは Customers のそれと同じなのでコピー & ペーストで。
package jp.mydns.akanekodou.dao; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import jp.mydns.akanekodou.dao.mapper.CustomerMapper; import jp.mydns.akanekodou.model.Customer; public class CustomerDAO { private SqlSessionFactory sessionFactory; public CustomerDAO() { sessionFactory = DaoUtil.getSqlSessionFactory(); } public Customer find(int id) { SqlSession session = sessionFactory.openSession(); CustomerMapper mapper = session.getMapper(CustomerMapper.class); Customer cust = mapper.selectOne(id); session.close(); return cust; } }