纯粹是为了感受一下JavaEye非常好的代码格式化功能。
这样格式化得太漂亮了。
根据iBatis包中的示例程序修改。
com.mydomain.domain包中
1. Account.java
com.mydomain.data包中
2. Account.xml
3. sqlMapConfig.xml
4. Service.java
5. Test.java
6. createDB.sql - MySQL
这样格式化得太漂亮了。
根据iBatis包中的示例程序修改。
com.mydomain.domain包中
1. Account.java
package com.mydomain.domain;
public class Account {
private int id;
private String firstName;
private String lastName;
private String emailAddress;
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 String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@Override
public String toString() {
return "id: " + this.getId() + "\nfirstName: " + this.getFirstName()
+ "\nlastName: " + this.getLastName() + "\nemailAddress: "
+ this.getEmailAddress() + "\n";
}
}
com.mydomain.data包中
2. Account.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="Account">
<typeAlias alias="Account" type="com.mydomain.domain.Account" />
<resultMap class="Account" id="AccountResult">
<result property="id" column="ACC_ID" />
<result property="firstName" column="ACC_FIRST_NAME" />
<result property="lastName" column="ACC_LAST_NAME" />
<result property="emailAddress" column="ACC_EMAIL" />
</resultMap>
<select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
<select id="selectAccountById" parameterClass="int" resultClass="Account">
select
ACC_ID as id,
ACC_FIRST_NAME as firstName,
ACC_LAST_NAME as lastName,
ACC_EMAIL as emailAddress
from ACCOUNT
where ACC_ID = #id#
</select>
<insert id="insertAccount" parameterClass="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
) values (
#id#,#firstName#,#lastName#,#emailAddress#
)
</insert>
<update id="updateAccount" parameterClass="Account">
update ACCOUNT set
ACC_FIRST_NAME = #firstName#,
ACC_LAST_NAME = #lastName#,
ACC_EMAIL = #emailAddress#
where
ACC_ID = #id#
</update>
<delete id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ACC_ID = #id#
</delete>
</sqlMap>
3. sqlMapConfig.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>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/javaee" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="admin888" />
</dataSource>
</transactionManager>
<sqlMap resource="com/mydomain/data/Account.xml" />
</sqlMapConfig>
4. Service.java
package com.mydomain.data;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.mydomain.domain.Account;
public class Service {
private static SqlMapClient sqlMapper;
static {
try {
Reader reader = Resources
.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException("Something bad happend while "
+ "building the SqlMapClient instance." + e, e);
}
}
@SuppressWarnings("unchecked")
public List<Account> selectAllAccount() throws SQLException {
return sqlMapper.queryForList("selectAllAccounts");
}
public Account selectAccountById(int id) throws SQLException {
return (Account) sqlMapper.queryForObject("selectAccountById", id);
}
public void insertAccount(Account account) throws SQLException {
sqlMapper.insert("insertAccount", account);
}
public void updateAccount(Account account) throws SQLException {
sqlMapper.update("updateAccount", account);
}
public void deleteAccount(int id) throws SQLException {
sqlMapper.delete("deleteAccountById", id);
}
}
5. Test.java
package com.mydomain.data;
import java.util.Iterator;
import java.util.List;
import com.mydomain.domain.Account;
public class Test {
private static Service service = new Service();
public static void main(String[] args) throws Exception {
Test test = new Test();
// test.testSelectAccountById(2);
// test.testInsertAccount(44, "aaa", "bbb", "ccc@ddd.com");
// test.testUpdateAccount(34, "Hu", "Jintao", "hujintao@gov.cn");
test.testDeleteAccount(44);
test.testSelectAllAccount();
}
public void testSelectAllAccount() throws Exception {
List<Account> accounts = service.selectAllAccount();
Iterator<Account> it = accounts.iterator();
while (it.hasNext()) {
Account account = (Account) it.next();
System.out.println(account.toString());
}
}
public void testSelectAccountById(int id) throws Exception {
Account account = service.selectAccountById(id);
System.out.println(account.toString());
}
public void testInsertAccount(int id, String firstName, String lastName,
String emailAddress) throws Exception {
Account account = new Account();
account.setId(id);
account.setFirstName(firstName);
account.setLastName(lastName);
account.setEmailAddress(emailAddress);
service.insertAccount(account);
}
/**
* 根据id进行查询,修改其他几个属性,不修改id
* @param id
* @param firstName
* @param lastName
* @param emailAddress
* @throws Exception
*/
public void testUpdateAccount(int id, String firstName, String lastName,
String emailAddress) throws Exception {
Account account = service.selectAccountById(id);
account.setFirstName(firstName);
account.setLastName(lastName);
account.setEmailAddress(emailAddress);
service.updateAccount(account);
}
public void testDeleteAccount(int id) throws Exception {
service.deleteAccount(id);
}
}
6. createDB.sql - MySQL
create table(
ACC_ID bigint,
ACC_FIRST_NAME varchar(20),
ACC_LAST_NAME varchar(20),
ACC_EMAIL varchar(30)
);