Programming Method using MyBatis¶
Topics
About MyBatis¶
MyBatis is an O/R mapping framework which was developed on the concept of SQL and object mappinginstead of the record and object mapping managed by the database.It will be useful when the complex combination conditions or precise query tuning are required.Please see below for the details about MyBatis.Note
MyBatisは2.x系と3.x系で実装が異なります。TERASOLUNA Global Framework では、MyBatis2.3系を利用して実装します。
Programming Method¶
SQLMap Creation¶
- When the functions of MyBatis are used, it is necessary to create the SQL map that defines the query to be executed.As an example, the xml file below is created and is placed under META-INF/mybatis/sql.Here, please set the file nae to [*-sqlmap.xml].
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="mycompany"> <!-- ① --> <typeAlias alias="MyCompany" type="my.terasoluna.app.domain.model.MyCompany"/> <!-- ② --> <resultMap id="resultMapMyCompnay" class="MyCompany"> <!-- ③ --> <result property="companyId" column="company_id" /> <result property="name" column="name" /> </resultMap> <insert id="insert" parameterClass="MyCompany"> <!-- ④ --> INSERT INTO my_company (company_id, name) VALUES(#companyId#, #name#) </insert> <update id="update" parameterClass="MyCompany"> <!-- ⑤ --> UPDATE my_company SET name = #name# WHERE company_id = #companyId# </update> <delete id="delete" parameterClass="java.lang.String"> <!-- ⑥ --> DELETE FROM my_company WHERE company_id = #value# </delete> <select id="selectOne" parameterClass="java.lang.String" resultMap="resultMapMyCompnay"> <!-- ⑦ --> SELECT * FROM my_company WHERE company_id = #value# </select> <select id="selectAll" resultMap="resultMapMyCompnay"> <!-- ⑦ --> SELECT * FROM my_company </select> <select id="selectLessId" parameterClass="java.lang.String" resultMap="resultMapMyCompnay"> <!-- ⑧ --> <![CDATA[ SELECT * FROM my_company WHERE company_id < #value# ]]> </select> </sqlMap>
- <sqlMap> tag is defined. Add arbitrary name of SQL map to the namespace attribute.Please try to put the name that is unique in the total application program.
- <typeAlias> tag is placed optionally.You can now use the class path specified in the type attribute by the omitted name specified in the alias attribute.
- <resultMap> tag is placed optionally.It will be used when the remapping is needed because of the difference in field name and column name of entity.
- In case INSERT statement is defined, use the <insert> tag.Here, in order to embed the field value of entity to the query,“MyCompany”(my.terasoluna.app.domain.model.MyCompany class) is usedas a parameterClass attribute.If the reference to parameter is required, you should specify the field name by enclosing with #.
- In case UPDATE statement is defined, use the <update> tag.
- In case DELETE statement is defined, use the <delete> tag.Since the character string is used as a condition of the main key,String is used as parameterClass, and#value# is used as a reference to variables.
- In case SELECT statement is defined, <select> tag should be used.+Search result will sepcify the ID of resultMap defined in ③,and will be returned by remapping the value.
- If the condition statement includes an inequality sign and needs an escape,query or the corresponding part should be enclosed by <![CDATA[ ]]>.
Note
Various other tags or functions are available.Please refer to the Reference for detail.
Repository Class Creation¶
- For any given entity, repository class is implemented as follows.
package my.terasoluna.app.domain.repository; import java.util.List; import jp.terasoluna.fw.dao.QueryDAO; import jp.terasoluna.fw.dao.UpdateDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import my.terasoluna.app.domain.model.MyCompany; @Repository public class MyCompanyRepositoryMyBatisImpl implements MyCompanyRepositoryMyBatis { @Autowired UpdateDAO updateDAO; // ① @Autowired QueryDAO queryDAO; // ② @Override public void insert(MyCompany entity) { updateDAO.execute("mycompany.insert", entity); // ③ } @Override public void update(MyCompany entity) { updateDAO.execute("mycompany.update", entity); // ③ } @Override public void delete(String companyId) { updateDAO.execute("mycompany.delete", companyId); // ③ } @Override public MyCompany selectOne(String companyId) { return queryDAO.executeForObject("mycompany.selectOne", companyId, MyCompany.class); // ④ } @Override public List<MyCompany> selectAll(int offset, int pageSize) { return queryDAO.executeForObjectList("mycompany.selectAll", null, offset, pageSize); // ⑤ } @Override public List<MyCompany> selectLessId(String companyId) { return queryDAO.executeForObjectList("mycompany.selectLessId", companyId); } }
- In case update-type query is used, UpdateDao should be defined.
- In case selection-type query is used, QueryDao is defined.
- In case update-type query is executed, UpdateDao#execute is executed.Query to be used is selected as the first argument.Query name should be specified as “namespace attribute of SQL map.Quesry id”.Parameter class object requested by the query should bes et to the second argument.
- In case a single record is searched, QueryDao#executeForObject is executed.Query name is specified to the first argument, condition to the second argument, and entity class of search result to the third argument.
- In case multiple records are searched, QueryDao#executeForList is executed.In case query name is fetched from the first argument and condition is fetched from the second argument,offset should be set to the third argument, and number of items obatined will be set to the fourth argument.