【web开发学习笔记】ibatis学习总结
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学习总结的更多相关文章
- python3.4学习笔记(七) 学习网站博客推荐
python3.4学习笔记(七) 学习网站博客推荐 深入 Python 3http://sebug.net/paper/books/dive-into-python3/<深入 Python 3& ...
- 20155234 2016-2017-2第十周《Java学习笔记》学习总结
20155234第十周<Java学习笔记>学习总结 教材学习内容总结 网络编程 在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定 ...
- #学习笔记#e2e学习使用(二)
前言: <#学习笔记#e2e学习使用(一)>主要记录了Vue项目的创建到e2e环境的搭建,以及期间遇到的各种问题和解决方法.本文建立在基础测试环境搭建完毕能正确运行的情况下,编写测试代码, ...
- #学习笔记#e2e学习使用(一)
本文仅限于记录本人学习的过程,以及怎么踩的坑,是如何解决的.逻辑肯定是混乱的,有用之处会抽出共通另行发帖. 最终目标:要运用于Vue项目中,进行功能测试甚至自动化测试. 一.e2e概念 理解:end ...
- mybatis学习笔记之学习目录(1)
mybatis学习笔记之学习结构(1) 学习结构: 1.mybatis开发方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发 ...
- Vue学习笔记-rest_framework_jwt 学习
一 使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7 (Windows x86- ...
- java JDK8 学习笔记——助教学习博客汇总
java JDK8 学习笔记——助教学习博客汇总 1-6章 (by肖昱) Java学习笔记第一章——Java平台概论 Java学习笔记第二章——从JDK到IDEJava学习笔记第三章——基础语法Jav ...
- 20155234 2610-2017-2第九周《Java学习笔记》学习总结
20155234第九周<Java学习笔记>学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC(Ja ...
- Django Web开发指南笔记
Django Web开发指南笔记 语句VS表达式 python代码由表达式和语句组成,由解释器负责执行. 主要区别:表达式是一个值,它的结果一定是一个python对象:如:12,1+2,int('12 ...
- Asp.net MVC4高级编程学习笔记-视图学习第一课20171009
首先解释下:本文只是对Asp.net MVC4高级编程这本书学习记录的学习笔记,书本内容感觉挺简单的,但学习容易忘记,因此在边看的同时边作下了笔记,可能其它朋友看的话没有情境和逻辑顺序还请谅解! 一. ...
随机推荐
- POJ 3026 Borg Maze bfs+Kruskal
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
- 【python之路7】python基本数据类型(一)
一.运算符 1.算数运算符 +.-.*./.%(求余数).//(取整数部分) python2.x中,如果计算浮点数其中一个数字必须是浮点数否则按整数计算: 如python2.7中:print 9/2 ...
- 移动周报:十款最实用的Android UI设计工具
上一周可以说是一个不断Mark周,从最实用的Android UI设计工具.免费移动应用测试框架推荐,到HTML5开发框架等等,各种开发工具.框架精彩丰呈,看得小伙伴们是不亦乐乎.当然,还有不容错过的M ...
- jsp EL表达式 字符串的比较
jsp EL表达式 字符串的比较 跟JavaScript一样,直接使用两个等于号即可:== 代码如下: <c:if test="${highLight == 'visa'}" ...
- 查看Mac OSX机器上存在的所有Device
可以使用instruments -s来查看Mac OSX机器上存在的所有Device,包括模拟器创建的Device以及真实连接的iPad,iPhone等设备.
- eMMC尺寸
eMMC Physical Specifications主要有四种结构,pin角定义及功能上基本一致,主要是看应用平台的需求: AA:12mm*16mm 169Pin AB:12mm*18mm 16 ...
- 利用TEA算法进行数据加密
TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计. ...
- [ZOJ 3631] Watashi's BG
Watashi's BG Time Limit: 3 Seconds Memory Limit: 65536 KB Watashi is the couch of ZJU-ICPC Team ...
- Ehcache Java开源缓存框架
一.ehcache.xml 配置详解 单机配置: <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...
- Power Strings
Power Strings TimeLimit: 1 Second MemoryLimit: 32 Megabyte Totalsubmit: 1791 Accepted: 528 Descr ...