ibatis demo01

本文通过一个简单的iBATIS Java示例介绍如何使用ORM框架进行数据库操作,包括增删改查等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我记得好像是官方的例子 ibatis demo01
Account.java
package ibatis.demo01;

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;
	}
}
SimpleExample.java


package ibatis.demo01;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;

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

/**
 * This is not a best practices class. It's just an example to give you an idea of how iBATIS works. * For a more complete example, see JPetStore 5.0 at http://www.ibatis.com.
 */
public class SimpleExample
{
    /**

     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("Demo01_SqlMapConfig.xml");
            sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();
        }
        catch (IOException e)
        {
            throw new RuntimeException("Something bad happened while building the SqlMapClient instance. " + e, e);
        }
    }

    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) throws SQLException
    {
        sqlMapper.insert("insertAccount", account);
    }

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

    public static void deleteAccount(int id) throws SQLException
    {
        sqlMapper.delete("deleteAccount", id);
    }
}

ZengWenFengTest.java
package ibatis.demo01;

import java.sql.SQLException;

public class ZengWenFengTest
{
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Account item = new Account();
		item.setId(1);
		item.setFirstName("sdfs");
		item.setLastName("fsda");
		item.setEmailAddress("");
		
		try
		{
			SimpleExample.selectAllAccounts();
			SimpleExample.insertAccount(item);
		}
		catch (SQLException e)
		{
			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="ibatis.demo01.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 ACC_ID,
		ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL 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>

</sqlMap>
Demo01_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>

  <!-- Configure a built-in transaction manager.  If you're using an 
       app server, you probably want to use its transaction manager 
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@192.168.1.103:1521:winkey"/>
      <property name="JDBC.Username" value="bms"/>
      <property name="JDBC.Password" value="bms"/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the 
       classpath, as they are here (com.domain.data...) -->
  <sqlMap resource="ibatis/demo01/Account.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>
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>

  <!-- Configure a built-in transaction manager.  If you're using an 
       app server, you probably want to use its transaction manager 
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@192.168.1.103:1521:winkey"/>
      <property name="JDBC.Username" value="bms"/>
      <property name="JDBC.Password" value="bms"/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the 
       classpath, as they are here (com.domain.data...) -->
  <sqlMap resource="com/mydomain/data/Account.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>
关于规范格式,我觉得J2EE本来的文档格式就非常标准了,就是自己稍微改进了一下。例如括号怎么放,怎么写,看起来更加对称容易阅读



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

spencer_tseng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值