iBatis初級入門
6750 ワード
簡単なiBatisアプリケーションに必要なファイル
1:POJO類例:Person.java
2:iBatis全体のプロファイル例:SqlMapConfig.xml
3:JDBCプロファイルSqlMapConfigExample.properties
4:POJO類対応プロファイル例:Person.xml
5:対応表Person.sql
6:パッケージ
7:適用
1:POJO類例:Person.java
public class Person {
private int id;
private String firstName;
private String lastName;
private Date birthDate;
private double weightInKilograms;
private double heightInMeters;
// getter,setter
}
2:iBatis全体のプロファイル例:SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- JDBC -->
<properties resource="examples/sqlmap/maps/SqlMapConfigExample.properties" />
<!-- -->
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false" />
<!-- -->
<typeAlias alias="order" type="testdomain.Order" />
<!-- transaction&dataSource -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
</dataSource>
</transactionManager>
<!-- POJO -->
<sqlMap resource="examples/sqlmap/maps/Person.xml" />
</sqlMapConfig>
3:JDBCプロファイルSqlMapConfigExample.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:oracle1
username=jsmith
password=test
4:POJO類対応プロファイル例:Person.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Person">
<!-- -->
<select id="getPerson" parameterClass="int" resultClass="examples.domain.Person">
SELECT
PER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_BIRTH_DATE as birthDate,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON WHERE
PER_ID = #value#
</select>
<!-- -->
<insert id="insertPerson" parameterClass="examples.domain.Person">
INSERT INTO PERSON
(PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG,
PER_HEIGHT_M)
VALUES
(#id#, #firstName#, #lastName#, #birthDate#,#weightInKilograms#, #heightInMeters#)
</insert>
<!-- -->
<update id="updatePerson" parameterClass="examples.domain.Person">
UPDATE PERSON SET
PER_FIRST_NAME = #firstName#,
PER_LAST_NAME = #lastName#,
PER_BIRTH_DATE = #birthDate#,
PER_WEIGHT_KG = #weightInKilograms#,
PER_HEIGHT_M = #heightInMeters#
WHERE PER_ID = #id#
</update>
<!-- -->
<delete id="deletePerson" parameterClass="examples.domain.Person">
DELETE PERSON WHERE
PER_ID = #id#
</delete>
</sqlMap>
5:対応表Person.sql
CREATE TABLE PERSON(
PER_ID NUMBER (5, 0) NOT NULL,
PER_FIRST_NAME VARCHAR (40) NOT NULL,
PER_LAST_NAME VARCHAR (40) NOT NULL,
PER_BIRTH_DATE DATETIME ,
PER_WEIGHT_KG NUMBER (4, 2) NOT NULL,
PER_HEIGHT_M NUMBER (4, 2) NOT NULL,
PRIMARY KEY (PER_ID)
)
6:パッケージ
public class MyAppSqlConfig {
private static final SqlMapClient sqlMapClient ;
static {
try {
String resource = "com/ibatis/example/sql-map-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlMapClient getSqlMapInstance() {
return sqlMapClient;
}
}
7:適用
public class Person {
public static void main(String[] args)
{
//--------- --------------
SqlMapClient sqlMapClient = MyAppSqlMapConfig.getSqlMapInstance();
Integer personPk = new Integer(5);
Person person = (Person) sqlMapClient.queryForObject ("getPerson", personPk);
//--------- -------------
person.setHeightInMeters(1.83);
person.setWeightInKilograms(86.36);
sqlMapClient.update("updatePerson", person);
//--------- ----------
sqlMapClient.delete("deletePerson", person);
//--------- ----------
Person newPerson = new Person();
newPerson.setId(11);
newPerson.setFirstName("Clinton");
newPerson.setLastName("Begin");
newPerson.setBirthDate (null);
newPerson.setHeightInMeters(1.83);
newPerson.setWeightInKilograms(86.36);
sqlMapClient.insert ("insertPerson", newPerson);
}
}