本节将利用orm轻量级框架ibatis实现一个简单的账户的管理程序,其结构如下:
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!");
}
}

- 大小: 4 KB
分享到:
相关推荐
知识点1:iBATIS-SqlMaps-2 简介 iBATIS-SqlMaps-2 是一个基于 Java 的持久层框架,旨在简化 Java 应用程序中的数据库交互。它提供了一种灵活的方式来映射 Java 对象到数据库表,允许开发者使用 SQL 语句来操作...
Ibatis 会自动处理查询结果,如果返回多条记录,只会返回第一条。确保 SQL 语句的条件能够唯一确定一条记录。 6. **数据集合查询** 对于查询结果集的情况,使用 `selectList` 方法。它会返回一个 List 对象,每个...
**IBATIS入门(第二节)** 在本节中,我们将深入探讨开源的Java持久层框架——IBATIS,它提供了一种将SQL语句与Java代码分离的方式,从而简化了数据库操作。IBATIS这个名字来源于“Java中的iBATIS”,其中“i”是...
iBatis是一个优秀的Java持久层框架,它将SQL语句与Java代码分离,提供了一种灵活的方式来处理数据库操作。在本教程中,我们将通过“iBatis-JPetStore-5.0”项目来深入理解iBatis的核心概念和功能。 **1. iBatis概述...
**ibatis入门教程与开发指南** Ibatis,全称MyBatis-iBATIS,是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。Ibatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。Ibatis可以使用简单...
Ibatis 是一个优秀的轻量级 Java ORM(对象关系映射)框架,它允许开发者将 SQL 查询与 Java 代码分离,使得数据库操作更加灵活和易于维护。本资源包含了 Ibatis 的 2.3.4 版本开发包,以及官方的权威开发指南和入门...
通过以上步骤,我们可以完成一个简单的iBatis入门示例。值得注意的是,虽然iBatis提供了强大的SQL控制能力,但在实际项目中,我们也应该关注代码的可读性和可维护性,合理地利用框架特性,以提高开发效率和代码质量...
**iBatis入门** iBatis 是一个优秀的持久层框架,它允许开发者将SQL语句直接写在配置文件中,从而避免了Java代码与数据库之间的繁杂交互。它结合了Java的灵活性和SQL的高效查询能力,使得开发更加便捷。本篇文章将...
【IBATIS 入门】 IBATIS 是一个流行的数据访问框架,它在 Java 开发领域中扮演着重要角色,特别适用于那些需要对 SQL 有更多控制权的项目。与 Hibernate 和 Apache OJB 等全面的 ORM(对象关系映射)解决方案不同,...
这个"第一个ibatis例子"是一个典型的入门教程,旨在帮助初学者快速理解并掌握Ibatis的基本用法。 首先,Ibatis的核心组件包括XML配置文件、SqlMapConfig.xml、Mapper接口以及Mapper XML文件。在入门例子中,我们...
【标题】"ibatis经典入门"揭示了我们即将探讨的主题——如何入门并掌握iBATIS这一数据层框架。iBATIS作为一个轻量级的解决方案,它简化了Java应用程序中的数据库操作,提供了SQL映射功能,使得开发人员可以更加灵活...
通常,这样的教程会涵盖安装Ibatis、创建第一个映射文件、调用SQL语句、处理结果集等内容。此外,它可能还会介绍如何使用Ibatis的API来执行插入、更新、删除等操作,并解释如何处理单个结果、多个结果或无结果的情况...
搭建iBatis开发环境是使用这个框架的第一步,通常涉及以下步骤: 1. **导入依赖**:首先需要将必要的jar包引入到项目中,包括iBatis的核心库(如ibatis-2.3.0.677.jar)以及相应的数据库驱动,例如对于MySQL,需要...
- 第一个例子:通过一个简单的CRUD操作,展示iBATIS的基本用法。 - 数据源配置:讲解如何配置数据连接字符串,连接池等。 - CRUD操作:详细解释如何使用iBATIS执行增删改查操作。 - 映射实体类:介绍如何将...
入门教程通常从简单的Hello World示例开始,逐步引导读者了解如何创建和配置iBatis项目,编写第一条SQL,处理查询结果,以及如何进行异常处理。同时,还会讲解如何在实际项目中结合Spring等框架进行集成使用。 **...
《Ibatis入门详解:从简单案例到深入理解》 Ibatis,作为一个轻量级的Java持久层框架,因其灵活性和易用性而被广大开发者所喜爱。本教程将基于提供的"ibatis1.rar"压缩包文件,带领大家一步步走进Ibatis的世界,...
MyBatis的前身就是iBatis , MyBatis使用XML描述符或注释将对象与存储过程或SQL语句耦合,将关系数据库与面向对象应用程序结合使用变得更加容易。支持定制化SQL、存储过程以及高级映射。
MyBatis的前身就是iBatis , MyBatis使用XML描述符或注释将对象与存储过程或SQL语句耦合,将关系数据库与面向对象应用程序结合使用变得更加容易。支持定制化SQL、存储过程以及高级映射。