ibatis学习总结

ibatis数据库配置文件

<?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>
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:hsqldb:."/>
<property name="JDBC.Username" value="carp"/>
<property name="JDBC.Password" value="123"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/mydomain/data/Account.xml"/>
</sqlMapConfig>

数据库与model映射配置文件

<?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">
<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>
</sqlMap>

Model对象

package com.mydomain.domain;

public class Account {

  private int id;
private String firstName;
private String lastName;
private String emailAddress;
/*Model层,主要进行数据訪问部分*/
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;
}
}

DAO数据訪问对象

package com.mydomain.data;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account; import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException; public class SimpleExample { /**
* SqlMapClient instances are thread safe, so you only need one.In this case, we'll use a static singleton.
*/
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("com/mydomain/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// Fail fast.
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);
}
}

结论:

	1、通过数据库和Model对象映射配置文件,将数据库中表与java对象进行关联;
2、通过数据库配置文件。里面包括数据库与model对象映射配置文件。保证数据库连接。
3、通过DAO进行数据库的增删改查操作。





【web开发学习笔记】ibatis学习总结的更多相关文章

  1. python3.4学习笔记(七) 学习网站博客推荐

    python3.4学习笔记(七) 学习网站博客推荐 深入 Python 3http://sebug.net/paper/books/dive-into-python3/<深入 Python 3& ...

  2. 20155234 2016-2017-2第十周《Java学习笔记》学习总结

    20155234第十周<Java学习笔记>学习总结 教材学习内容总结 网络编程 在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定 ...

  3. #学习笔记#e2e学习使用(二)

    前言: <#学习笔记#e2e学习使用(一)>主要记录了Vue项目的创建到e2e环境的搭建,以及期间遇到的各种问题和解决方法.本文建立在基础测试环境搭建完毕能正确运行的情况下,编写测试代码, ...

  4. #学习笔记#e2e学习使用(一)

    本文仅限于记录本人学习的过程,以及怎么踩的坑,是如何解决的.逻辑肯定是混乱的,有用之处会抽出共通另行发帖. 最终目标:要运用于Vue项目中,进行功能测试甚至自动化测试. 一.e2e概念 理解:end ...

  5. mybatis学习笔记之学习目录(1)

    mybatis学习笔记之学习结构(1) 学习结构: 1.mybatis开发方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发 ...

  6. Vue学习笔记-rest_framework_jwt 学习

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  7. java JDK8 学习笔记——助教学习博客汇总

    java JDK8 学习笔记——助教学习博客汇总 1-6章 (by肖昱) Java学习笔记第一章——Java平台概论 Java学习笔记第二章——从JDK到IDEJava学习笔记第三章——基础语法Jav ...

  8. 20155234 2610-2017-2第九周《Java学习笔记》学习总结

    20155234第九周<Java学习笔记>学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC(Ja ...

  9. Django Web开发指南笔记

    Django Web开发指南笔记 语句VS表达式 python代码由表达式和语句组成,由解释器负责执行. 主要区别:表达式是一个值,它的结果一定是一个python对象:如:12,1+2,int('12 ...

  10. Asp.net MVC4高级编程学习笔记-视图学习第一课20171009

    首先解释下:本文只是对Asp.net MVC4高级编程这本书学习记录的学习笔记,书本内容感觉挺简单的,但学习容易忘记,因此在边看的同时边作下了笔记,可能其它朋友看的话没有情境和逻辑顺序还请谅解! 一. ...

随机推荐

  1. iOS打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  2. Solr4.8.0源码分析(27)之ImplicitDocRouter和CompositeIdRouter

    同样在公司工作中发现了一个现象, 1.我用/solr/admin/collections?action=CREATE&name=collection&numShards=3&r ...

  3. python 遍历字典

    dict={"a":"apple","b":"banana","o":"orange&qu ...

  4. css3加载中

    .loader { margin: 6em auto; font-size: 10px; position: relative; text-indent: -9999em; border-top: 1 ...

  5. 关于entity framework

    http://www.cnblogs.com/lsxqw2004/archive/2009/05/31/1495240.html http://www.open-open.com/lib/view/o ...

  6. Spring 自动装配

    1.自动装配有 bytype 和byName两种模式. 2.可以使用autowire属性指定自动装配的方式,byName根据bean的名称和当前bean的setter风格属性进行自动装配:byType ...

  7. Swifter初体验;按照惯例,来一个Swift版本的:iOS图片验证码?

    不多解释,上图,上代码:代码

  8. C++实现一个限制对象实例个数的类

    http://www.cnblogs.com/absolute8511/archive/2009/03/02/1649603.html

  9. Lua的function、closure和upvalue

    Lua中的函数是一阶类型值(first-class value),定义函数就象创建普通类型值一样(只不过函数类型值的数据主要是一条条指令而已),所以在函数体中仍然可以定义函数.假设函数f2定义在函数f ...

  10. lsof 拥有更多的功能

    lsof 拥有更多的功能# lsof -i 看系统中有哪些开放的端口,哪些进程.用户在使用它们,比 netstat -lptu 的输出详细. # lsof -i 4  查看IPv4类型的进程COMMA ...