ibatis入门-第一节

本节将利用orm轻量级框架ibatis实现一个简单的账户的管理程序,其结构如下:
[img]http://tomotoboy.iteye.com/upload/attachment/129428/fcfcb6d6-8e55-366f-ae05-c7cfcde20130.bmp[/img]
Account.java

package cistec.ibatis.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;
}

}


AccountDaoImpl.java

package cistec.ibatis.data;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import cistec.ibatis.domain.Account;

import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;


public class AccountDaoImpl {

/**
* SqlMapClient instances are thread safe, so you only need one.
* In this case, we'll use a static singleton. So sue me. ;-)
*/
private static SqlMapClient sqlMapper;

/**
* It's not a good idea to put code that can fail in a class initializer,
* but for sake of argument, here's how you configure an SQL Map.
*/
static {
try {

Reader reader = Resources.getResourceAsReader("cistec/ibatis/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();

}
catch (IOException e){
e.printStackTrace();
}
}

public static List selectAllAccounts () throws SQLException {
return sqlMapper.queryForList("selectAllAccounts");
}

public static Account selectAccountById (int id) throws SQLException {
return (Account) sqlMapper.queryForObject("selectAccountById", id);
}

public static void insertAccount (Account account) {
try {
sqlMapper.insert("insertAccount", account);
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void updateAccount (Account account) throws SQLException {
sqlMapper.update("updateAccount", account);
}

public static void deleteAccountById (int id) {
try {
sqlMapper.delete("deleteAccountById", id);
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void deleteAccount(Account account){
try {
sqlMapper.delete("deleteAccount",account);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


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">

<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="Account" type="cistec.ibatis.domain.Account"/>

<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly.
-->

<resultMap id="AccountResult" class="Account">
<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 with no parameters using the result map for Account class. -->
<select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>

<!-- A simpler select example without the result map. Note the
aliases to match the properties of the target result class. -->
<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 example, using the Account parameter class -->
<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 example, using the Account parameter class -->
<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 example, using an integer as the parameter class -->
<delete id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ACC_ID = #id#
</delete>

<!-- Delete Account ,using an account as the parameter class -->
<delete id="deleteAccount" parameterClass="Account">
delete from ACCOUNT where ACC_ID = #id#
</delete>

</sqlMap>


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">
<!--
基于Apache DBCP连接池组件实现的DataSource封装
-->
<dataSource type="DBCP">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/ibatis"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>

<!--
数据库连接池中可维持的最大容量
-->
<property name="Pool.MaximumActiveConnections" value="10"/>

<!--
连接池挂起的连接树
-->
<property name="Pool.MaximumIdleConnections" value="5"/>

<!--
当连接池中没有可用的连接时,允许线程等待的最长时间
-->
<property name="Pool.MaximumWait" value="14000"/>


</dataSource>
</transactionManager>
<sqlMap resource="cistec/ibatis/data/Account.xml"/>
</sqlMapConfig>


测试程式

package cistec.ibatis.data;

import java.sql.SQLException;

import cistec.ibatis.domain.Account;

public class AccountTest {

/**
* @param args
*/
public static void main(String[] args) {
Account account = new Account();
account.setId(7);
account.setFirstName("YANG");
account.setLastName("FANG");
account.setEmailAddress("MAOFAN@foxmail.com");
//AccountDaoImpl.deleteAccount(account);
AccountDaoImpl.insertAccount(account);
System.out.println("Have deleted a new account!");

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值