<?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="com.ibatis.model.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>
<!-- Insert Account with sequence -->
<insert id="insertAccountBySequence" parameterClass="Account">
<selectKey resultClass="int" keyProperty="id">
SELECT SEQ_ACCOUNT_PK_ID.NEXTVAL FROM DUAL
</selectKey>
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
)
values (
#id#, #firstName#, #lastName#, #emailAddress#
)
</insert>
</sqlMap>
-----------------------------------------------------------------
测试:
@Test
public void insertAccountTest() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
Account account = new Account();
account.setId(104);
account.setFirstName("ff");
account.setLastName("ff");
account.setEmailAddress("ee");
dao.insertAccount(account);
System.out.println("add ok!");
}
@Test
public void selectAllAccountsTest() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
for(Account ac : dao.selectAllAccounts()){
System.out.println(ac);
}
}
@Test
public void selectAccountByIdTest() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
Account account = dao.selectAccountById(100);
System.out.println(account);
}
@Test
public void updateAccountTest() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
Account account = new Account();
account.setId(100);
account.setFirstName("ccc");
account.setLastName("ccc");
account.setEmailAddress("ccc");
dao.updateAccount(account);
System.out.println("updated!");
}
@Test
public void deleteAccountTest() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
dao.deleteAccount(0);
System.out.println("deleted!");
}
@Test
public void insertAccountBySequenceTest() throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
Account account = new Account();
account.setId(100);
account.setFirstName("ccc");
account.setLastName("ccc");
account.setEmailAddress("ccc");
dao.insertAccountBySequence(account);
System.out.println("saved with sequence!");
}
分享到:
相关推荐
ibatis简单CRUD例子,供新手学习用。
ibatis实现CRUD操作,它比学好hibernate更简单
ibatis mybatis crud 完整代码
NULL 博文链接:https://jiaozhiguang-126-com.iteye.com/blog/1668831
ibatis mybatis 分页 crud 完整代码 如有不清楚的地方:可访问公司网站:www.meihuangkeji.com 进一步交流讨论。
完整的ibatis的CRUD+存储过程+动态查询!!!!!!!!
封装通用的Spring3+Struts2+IBatis的CRUD
前段时间自己研究了下SpringMVC,发现资源很少。经过自己的努力,整合了一套出来。
IbatisDemo实现基本的CRUD操作
体现了Ibatis的作用及优点: 1.Ibatis的优点 (1) Ibatis更容易进行数据库优化。从配置文件中优化sql语句。 (2) ibatis可以进行细度优化。可以针对一个表中一个或几个字段进行更新等操作。Hibernate会更新所有的...
关于ibatis连接MSSQL和MySQL的CRUD,Junit4测试
ibatis struts2 spring3 mybatis 分页 crud 完整代码
数据库自己建一张简单的表就行了,特别说明 只适合新手入门 只有三个java文件和三个xml配置文件 非常简单 但是包含了crud操作 非常适合新手入门,因为项目经理让我熟悉一下ibatis 我就自学写了一个,希望对你有帮助
本实例实现了用户登陆,用户信息CRUD相关操作。让你感受到了ibatis做o/r mapping的方便快捷。 下次集成dwr进来 create table users( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, userName varchar(50), password...
NULL 博文链接:https://wukunlsy.iteye.com/blog/767466
iBATIS课件 iBATIS简介 搭建环境 配置文件 读取配置 基本的CRUD操作 模糊查询 iBATIS代码生成工具iBATOR 优缺点 选择Hibernate还是iBATIS
springmvc+ibatis demo搭建实现CRUD操作简单 有静态页面话技术
struts2+spring+ibatis 项目 入门使用 CRUD
ibatis3,对数据库的crud操作,用到了ibatis3的动态sql
NULL 博文链接:https://sarin.iteye.com/blog/1663290