Ibatisの簡単な応用
7081 ワード
JArパッケージ:
ibatis-2.3.0.677をダウンロードします.jar
mysqlドライバパッケージ
プロファイル:
jdbcプロファイル:db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
データベース表:
DROP TABLE IF EXISTS `test`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
主構成:SqlMapConfig.xml(hibernateのhibernate.cfg.xmlのように)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="db.properties"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property value="${driver}" name="JDBC.Driver"/>
<property value="${url}" name="JDBC.ConnectionURL"/>
<property value="${username}" name="JDBC.Username"/>
<property value="${password}" name="JDBC.Password"/>
</dataSource>
</transactionManager>
<!-- '/' -->
<sqlMap resource="Test.xml"/>
</sqlMapConfig>
クラス構成:Test.xml(hibernateのクラス名.hbm.xmlのようにオブジェクトマッピングですが、ここでは使用するsqlを構成します)
<?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>
<!--alias type alias -->
<typeAlias alias="Test" type="Test"/>
<!-- id , , spring bean id
resultClass list list -->
<select id="selectAll" resultClass="Test">
select * from test
</select>
<!-- parameterClass #param# -->
<select parameterClass="int" id="selectTestById" resultClass="Test">
select * from test where id=#id#
</select>
<!-- parameterClass -->
<insert id="add" parameterClass="test">
insert into test(id,name) values(#id#,#name#)
</insert>
<!-- set , -->
<update id="update" parameterClass="Test">
update test set name=#name# where id=#id#
</update>
<!-- $param$ ,% -->
<select id="fuzzyRetrieve" parameterClass="String" resultClass="Test">
select * from test where name like '%$name$%'
</select>
<!-- %param%, '$param$'
<select id="fuzzyRetrieve" parameterClass="String" resultClass="Test">
select * from test where name like '$name$'
</select>
-->
<delete id="delete" parameterClass="int">
delete from test where id=#id#
</delete>
</sqlMap>
javabean:
public class Test {
private int id = 0;
private String name = null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
インタフェース:
import java.util.List;
public interface TestDao {
public void add(Test test) throws Exception;
public void delete(int id) throws Exception;
public void update(Test test) throws Exception;
public List<Test>query() throws Exception;
public List<Test> query(String name) throws Exception;
public Test query(int id) throws Exception;
public void addBySequence(Test test) throws Exception;
}
インタフェースの実装:
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class ITestDaoImpl implements TestDao {
private static SqlMapClient sqlMapClient = null;
static{
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void add(Test test) throws Exception {
sqlMapClient.insert("add",test);
}
public void delete(int id) throws Exception {
sqlMapClient.delete("delete",id);
}
@SuppressWarnings("unchecked")
public List<Test> query() throws Exception {
List<Test>list = sqlMapClient.queryForList("selectAll");
return list;
}
@SuppressWarnings("unchecked")
public List<Test> query(String name) throws Exception {
List<Test>list = sqlMapClient.queryForList("fuzzyRetrieve",name);
return list;
}
public Test query(int id) throws Exception {
Test test = null;
test = (Test)sqlMapClient.queryForObject("selectTestById",id);
return test;
}
public void update(Test test) throws Exception {
sqlMapClient.update("update", test);
}
以上で構成が完了し、直接mian関数テストを使用してもjunitテストを使用してもよい
ibatisの欠点
sqlは自分で書く必要があります
パラメータ数は1つのみ
ibatisの利点
削減コード
単純
sql文とプログラムコードの分離
プロジェクトの分業を簡素化
移植性の向上