关于JpetStore的学习
大概看了一下,官方例子一共这几个包:
com/ibatis/jpetstore/domain
真正的业务实体类,应该属于business层面.例子里面这里定义了一个Account类.
com/ibatis/jpetstore/persistence
包括DaoConfig.java以及Dao.xml,DAO的初始化配置文件.
com/ibatis/jpetstore/persistence/iface
针对BO(Business Object)的接口,例如AccountDao.java里面定义了实现DAO对Account对象的一些操作方法以待实现.
com/ibatis/jpetstore/persistence/sqlmapdao
跟iface包里面的接口一一对应,例如AccountSqlMapDao.java就实现了AccountDao.java接口里面定义的所有映射或者操作方法.
com/ibatis/jpetstore/presentation
这个包里面都是FormBean,例如AccountBean.java.为Account定义了FormBean.
com/ibatis/jpetstore/service
为类操作提供的实用工具类,例如AccountService类就提供了很多对Account等操作的实用方法,这样在AccountBean里面就可以轻松使用了.
com/ibatis/struts
struts工具类.运用了反射机制.
com/ibatis/struts/httpmap
例如SessionMap或者CookiesMap等等类,估计是提供实用工具类来帮助轻松处理request,session,cookies等等.
com/ibatis/jpetstore/persistence/sqlmapdao/sql
很简单,每个实体类大致映射到数据库中的一张表,对该库表可能进行的所有sql操作都在这个包里面进行定义.包括对象和表列映射等等.
/properties
不用说了,配置文件而已.
根据Tutorial的说法:
创建一个Person以及到数据库的映射只需要做一下几件事情:
1.创建Person类:
package examples.domain;
import java.sql.Date;
public class Person {
private int id;
private String firstName;
private String lastName;
private Date birthDate;
private double weightInKilograms;
private double heightInMeters;
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
//…let’s assume we have the other getters and setters to save space…
}
2.创建Person表(需要手工建库):
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)
)
3.创建Person类到Person表的关系(Person.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map
PUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN"
"http://www.ibatis.com/dtd/sql-map.dtd">
<sql-map name="Person">
<!-- Use primitive wrapper type (e.g. Integer) as parameter and allow results to
be auto-mapped results to Person object (Java Bean) properties -->
<mapped-statement name="getPerson" result-class="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#
</mapped-statement>
<!-- Use Person object (Java Bean) properties as parameters for insert. Each of the
parameters in the #hash# symbols is a Java Beans property. -->
<mapped-statement name="insertPerson" >
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#)
</mapped-statement>
<!-- Use Person object (Java Bean) properties as parameters for update. Each of the
parameters in the #hash# symbols is a Java Beans property. -->
<mapped-statement name="updatePerson" >
UPDATE PERSON
SET (PER_ID = PER_FIRST_NAME = #firstName#,
PER_LAST_NAME = #lastName#, PER_BIRTH_DATE = #birthDate#,
PER_WEIGHT_KG = #weightInKilograms#,
PER_HEIGHT_M = #heightInMeters#)
WHERE PER_ID = #id#
</mapped-statement>
<!-- Use Person object (Java Bean) “id” properties as parameters for delete. Each of the
parameters in the #hash# symbols is a Java Beans property. -->
<mapped-statement name="deletePerson" >
DELETE PERSON
WHERE PER_ID = #id#
</mapped-statement>
</sql-map>
其中包含了对表的CRUD,以及返回对象的定义映射等等.
4.创建配置文件SqlMapConfigExample.properties以及SqlMapConfigExample.xml.
5.可以投入生产进行使用了,TestClass类:
import java.sql.SQLException;
import com.ibatis.sqlmap.client.SqlMapClient;
import examples.domain.Person;
/*
* 创建日期 2004-9-11
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
/**
* @author wyn
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class TestClass {
/**
*
*/
public TestClass() {
super();
// TODO 自动生成构造函数存根
}
public static void main(String[] args) {
SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance();
//System.out.println("fgfh");
//Integer personPk = new Integer(2);
}
}
但是本人执行本测试类失败,
com.ibatis.sqlmap.client.SqlMapException: There was an error while building the SqlMap instance.
--- The error occurred in the SQL Map Configuration file.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error. Cause: java.io.UTFDataFormatException: Invalid byte 3 of 3-byte UTF-8 sequence.
Caused by: java.io.UTFDataFormatException: Invalid byte 3 of 3-byte UTF-8 sequence.
Caused by: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error. Cause: java.io.UTFDataFormatException: Invalid byte 3 of 3-byte UTF-8 sequence.
Caused by: java.io.UTFDataFormatException: Invalid byte 3 of 3-byte UTF-8 sequence.
at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder.buildSqlMap(XmlSqlMapClientBuilder.java:203)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:49)
at MyAppSqlConfig.<clinit>(MyAppSqlConfig.java:13)
at TestClass.main(TestClass.java:31)
Result以及parameterMap:
<sqlMap id=”Product”>
<cacheModel id=”productCache” type=”LRU”>
<flushInterval hours=”24”/>
<property name=”size” value=”1000” />
</cacheModel>
<typeAlias alias=”product” type=”com.ibatis.example.Product” />
<parameterMap id=”productParam” class=”product”>
<parameter property=”id”/>
</parameterMap>
<resultMap id=”productResult” class=”product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getProduct” parameterMap=”productParam”
resultMap=”productResult” cacheModel=”product-cache”>
select * from PRODUCT where PRD_ID = ?
</select>
</sqlMap>