Home Database Mysql Tutorial iBatis入门--简单使用

iBatis入门--简单使用

Jun 07, 2016 pm 05:29 PM
ibatis

Ibatis:一种ldquo;半 自动化rdquo;的 ORM 实现,也就是说ibatis 不会为程序员在运行期自动生成 SQL 执行。具体的 SQL 需要程序

一、概念

Hibernate 和 Apache OJB :“一站式”ORM

Ibatis:一种“半 自动化”的 ORM 实现,也就是说ibatis 不会为程序员在运行期自动生成 SQL 执行。具体的 SQL 需要程序员编写,然后通过映 射配置文件,将 SQL 所需的参数,以及返回的结果字段映射到指定 POJO。

二、使用

构建 ibatis 基础代码
ibatis 基础代码包括:

1. ibatis 配置SqlMapConfig.xml文件 :

(1)、Settings 节点

参数描述

cacheModelEnable 是否启用SqlMapClient上的缓存机制。建议设为"true"

enhancementEnable 是否针对POJO启用字节码增强机制,避免java reflect的性能开销

errorTracingEnabled 是否启用错误日志,开发期间可以开启,方便调试

lazyLoadingEnalbed 是否启用延迟加载

maxRequests 最大并发请求数(Statement 并发数)

maxTransactions 最大并发事务数

maxSessions 最大Session数,及最大的并发SqlMapclient数,必须介于maxTransactions和maxRequest之间

(2)、transactionManager 事物管理
(3)、dataSource 数据源配置
(4)、sqlMap 用于包括SQL Map映射文件和其他的SQL Map配置文件。每个SqlMapClient对象使用的所有SQL Map映射文件都要在此声明。映射文件作为stream resource从类路径或URL读入。您必须在这里指定所有的SQL Map文件。

2、实体类POJO。这个需要和数据库的相对应。

3、映射文件map,通常放在sqlmap包下,实现具体的数据库操作语句。主要有模块配置、缓存配置、statement配置。使用最多的应该是此处的statement配置了,需要用的sql语句和参数都写在这里。

4、如何在代码中使用,这才是最关键的。

在此先要搞清楚几个比较核心的比较重要的概念:

(1)、XmlSqlMapClientBuilder,ibatis 2.0 之后版本新引入的组件, 用以替代 1.x 版本中的 XmlSqlMapBuilder。其作用是根据配置文件创建 SqlMapClient 实例。

(2)、SqlMapClient 是 ibatis 的核心组件, 提供数据操作的基础平台。

SqlMapClient 可通过 XmlSqlMapClientBuilder 创建:
String resource ="com/ibatis/sample/SqlMapConfig.xml";

Reader reader; reader = Resources.getResourceAsReader(resource);

XmlSqlMapClientBuilder xmlBuilder = new XmlSqlMapClientBuilder();

SqlMapClient sqlMap = xmlBuilder.buildSqlMap(reader);

备注:"com/ibatis/sample/SqlMapConfig.xml"指明了配置文件在 CLASSPATH 中的相对路径。 XmlSqlMapClientBuilder 通过接受一个 Reader 类型的配置文 件句柄,根据配置参数,创建 SqlMapClient 实例。 SqlMapClient 提供了众多数据操作方法。

5、SqlMapClient 基本操作示例

见Ibatis官方手册

6、学习体会

大三用java做数据库课程设计的时候将所有的数据库操作都写在一个类里,程序的耦合性很高,扩展性也不好。Ibatis是我第一次接触的,感觉学起来很容易上手,自己也学着在Demo上试了一下,做了个简单的登陆界面。学习ibatis的过程最重要的是理解他的工作原理和映射关系,还有就是结合webx开发,哪些文件应该放置在哪层(通常是DAO层),弄清楚这些问题,在以后的开发中肯定也是很有帮助的。

实践中遇到的问题:

无法链接数据库,无法下载数据库驱动,把jdbc.mysql.mysql-connector包复制到tomcat下面的lib目录中即可。当然要保证你的xml配置是正确的,我连接本地Mysql的时候配置如下:

        destroy-method="close">
       
       
       
       

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iBatis vs. MyBatis: Which one is better for you? iBatis vs. MyBatis: Which one is better for you? Feb 19, 2024 pm 04:38 PM

iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

iBatis and MyBatis: Comparison and Advantage Analysis iBatis and MyBatis: Comparison and Advantage Analysis Feb 18, 2024 pm 01:53 PM

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

What is the difference between ibatis and mybatis What is the difference between ibatis and mybatis Jan 10, 2024 am 11:25 AM

The difference between ibatis and mybatis: 1. Different basic information; 2. Different development time; 3. Function and ease of use; 4. Configuration file; 5. Input parameter type and output parameter type; 6. Return result set acceptance method; 7 , Grammatical differences; 8. Database dialect support; 9. Plug-in support; 10. Community activity; 11. Globalization support. Detailed introduction: 1. The basic information is different. iBatis provides a persistence layer framework, including SQL Maps and Data Access Objects, etc.

iBatis and MyBatis: Comparative evaluation of history and current situation iBatis and MyBatis: Comparative evaluation of history and current situation Feb 19, 2024 am 10:42 AM

iBatis and MyBatis: Evaluation and Comparison from History to Current Status Introduction: With the rapid development of the software development field, higher and higher requirements have been put forward for the database access framework. iBatis and MyBatis are two Java persistence layer frameworks that have attracted much attention. They both provide a simple and flexible way to access relational databases. This article will provide a historical review of these two frameworks and evaluate and compare their current status. 1. Historical review iBatisiBatis was developed by Clinton

Comparison of similarities and differences between iBatis and MyBatis: comparison of mainstream ORM frameworks Comparison of similarities and differences between iBatis and MyBatis: comparison of mainstream ORM frameworks Feb 19, 2024 pm 07:08 PM

iBatis and MyBatis are two mainstream ORM (Object-Relational Mapping) frameworks. They have many similarities in design and use, but also have some subtle differences. This article will compare the similarities and differences between iBatis and MyBatis in detail, and illustrate their characteristics through specific code examples. 1. The history and background of iBatis and MyBatis iBatis is Apache Software Foundat

How to solve ibatis mysql garbled characters How to solve ibatis mysql garbled characters Feb 16, 2023 am 10:40 AM

Solution to ibatis mysql garbled code: 1. Check the character set of mysql and set it to utf8; 2. Modify the configuration to "<property name="url"><value><![CDATA[jdbc:mysql://localhost :3306/yourDbNameuseUnicode=TRUE&characterEncoding=utf8]]></value>”.

iBatis vs. MyBatis: Comparison and Choice of Two Java Persistence Frameworks iBatis vs. MyBatis: Comparison and Choice of Two Java Persistence Frameworks Feb 22, 2024 pm 07:09 PM

iBatis and MyBatis: Comparison and Selection of Two Java Persistence Frameworks Introduction: In Java development, choosing a suitable persistence framework is the key to improving development efficiency and performance. Among the many frameworks, iBatis and MyBatis are two frameworks that are loved by developers. They all provide a concise, flexible and efficient way to operate the database. This article will compare iBatis and MyBatis from the following aspects to help developers choose a persistence framework suitable for their projects.

Java JPA vs. other persistence frameworks: Which one is better for you? Java JPA vs. other persistence frameworks: Which one is better for you? Feb 19, 2024 pm 08:21 PM

JPA (JavaPersistence API) is a standard Java API that provides a set of interfaces for accessing and persisting data objects. JPA is the result of the JPA working group, which consisted of SunMicrosystems (now Oracle) and other companies. JPA was released in December 2006 and has become the standard implementation of the JavaPersistenceAPI specification. The JPA framework provides the following key features: Object-Relational Mapping (ORM) Transaction Management Query JPAORM maps database tables to Java objects so that you can use standard Java APIs to manipulate database data. JPA transaction management allows you to

See all articles