Apache Derby JDBC driver
//-- Person.sql --
CREATE TABLE PERSON(
PER_ID DECIMAL(5, 0) NOT NULL,
PER_FIRST_NAME VARCHAR (40) NOT NULL,
PER_LAST_NAME VARCHAR (40) NOT NULL,
PER_BIRTH_DATE TIMESTAMP ,
PER_WEIGHT_KG DECIMAL(4, 2) NOT NULL,
PER_HEIGHT_M DECIMAL(4, 2) NOT NULL,
PRIMARY KEY (PER_ID)
)
//-- SqlMapConfig.properties --
driver=org.apache.derby.jdbc.EmbeddedDriver
url=jdbc:derby:testdb;create=true
username=user
password=secret
//-- sql-map-config.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>
<properties resource="SqlMapConfig.properties" />
<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>
<sqlMap resource="Person.xml" />
</sqlMapConfig>
//-- 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" resultClass="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="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="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="Person">
DELETE FROM PERSON
WHERE PER_ID = #id#
</delete>
</sqlMap>
//-- Person.java --
import java.util.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;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public double getWeightInKilograms() {
return weightInKilograms;
}
public void setWeightInKilograms(double weightInKilograms) {
this.weightInKilograms = weightInKilograms;
}
public double getHeightInMeters() {
return heightInMeters;
}
public void setHeightInMeters(double heightInMeters) {
this.heightInMeters = heightInMeters;
}
}
//-- Main.java --
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class Main {
private static Person newPerson() {
Person person = new Person();
person.setId(1);
person.setFirstName("firstName");
person.setLastName("lastName");
person.setBirthDate(new java.util.Date());
person.setHeightInMeters(1.73);
person.setWeightInKilograms(65.0);
return person;
}
public static void main(String[] args) throws IOException, SQLException {
Reader reader = Resources.getResourceAsReader("sql-map-config.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMap.startTransaction();
try {
Connection conn = sqlMap.getCurrentConnection();
Statement st = conn.createStatement();
BufferedReader br = new BufferedReader(new InputStreamReader(
Main.class.getResourceAsStream("Person.sql")));
StringBuffer sb = new StringBuffer();
try {
String line = br.readLine();
while (line != null) {
sb.append(line).append("\n");
line = br.readLine();
}
} finally {
br.close();
try {
st.execute(sb.toString());
} catch (SQLException e) {
e.printStackTrace();
}
st.close();
}
sqlMap.insert("insertPerson", newPerson());
Person person = (Person) sqlMap.queryForObject("getPerson", new Integer(1));
System.out.println(person.getFirstName());
sqlMap.delete("deletePerson", person);
sqlMap.commitTransaction();
} finally {
sqlMap.endTransaction();
}
}
}
沒有留言:
張貼留言