ibatis java 项目_Java项目搭建之ibatis学习详解

本文简单介绍了iBatis,它是Apache开源的ORM解决方案,小巧且上手快,是“半自动化”的ORM实现。还详细说明了通过iBatis搭建JAVA项目的步骤,包括JAR包依赖、配置文件设置等,并给出了相关的JAVA代码示例。

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

本文的主要内容是简单介绍了ibatis和如何通过iBatis搭建JAVA项目,包含了一个相关实例,需要的朋友可以参考下。

IBATIS简介

ibatis是 Apache的开源项目,一个ORM 解决方案,ibatis最大的特点就是小巧,上手很快。

使用 ibatis提供的ORM机制,对业务逻辑实现人员而言,面对的是纯粹的Java对象,这一层与通过Hibernate 实现ORM而言是基本一致的。

iBatis是一个基于SQL映射支持Java和·NET的持久层框架,相对Hibernate和ApacheOJB等“一站式”ORM解决方案而言,iBatis 是一种“半自动化”的ORM实现。

一、JAR包依赖

ibatis-2.3.4.726.jar

mysql-connector-java-5.0.8-bin.jar

二、SqlMap.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/test

username=root

password=root

三、SqlMapConfig.xml

/p>

"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

四、Student.xml

/p>

"http://ibatis.apache.org/dtd/sql-map-2.dtd">

select * from student

select * from student where id = #id#

insert into Student(id,name,age,address) values(#id#,#name#,#age#,#address#)

select @@identity as inserted

delete from student where id = #id#

delete from Student where id = #id#

update student set name=#name#,age=#age#,address=#address# where id = #id#

select * from student where name like '%$name$%'

select * from student where name like '%$name$%' and age >= #age#

select * from student where name like ? and age >= ?

五、JAVA代码

实体类:略

Dao:略

DaoImpl:

package com.ligang;

import java.io.IOException;

import java.io.Reader;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class StudentDaoImpl implements StudentDao {

public static SqlMapClient sqlMapClient = null;

static{

try {

Reader reader = Resources.getResourceAsReader("com/ligang/SqlMapConfig.xml");

sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

} catch (IOException e) {

e.printStackTrace();

}

}

public List findAll() {

List list = null;

try {

list = sqlMapClient.queryForList("findAll");

} catch (SQLException e) {

e.printStackTrace();

}

return list;

}

public Student findByID(String id){

Student student = null;

try {

student = (Student) sqlMapClient.queryForObject("findByID", id);

} catch (SQLException e) {

e.printStackTrace();

}

return student;

}

public void addStudent(Student student){

try {

sqlMapClient.insert("insertStudent",student);

} catch (SQLException e) {

e.printStackTrace();

}

}

public void deleteStudentByID(String id){

try {

sqlMapClient.delete("deleteStudentByID",id);

} catch (SQLException e) {

e.printStackTrace();

}

}

public void deleteStudent(Student student){

try {

sqlMapClient.delete("deleteStudent",student);

} catch (SQLException e) {

e.printStackTrace();

}

}

public void updateStudent(Student student){

try {

sqlMapClient.update("updateStudent", student);

} catch (SQLException e) {

e.printStackTrace();

}

}

public List findByCon(String name){

List stuList = new ArrayList();

try {

stuList = sqlMapClient.queryForList("selectByLike",name);

} catch (SQLException e) {

e.printStackTrace();

}

return stuList;

}

public List findByCon(Student student){

List stuList = new ArrayList();

try {

stuList = sqlMapClient.queryForList("findByCon1",student);

} catch (SQLException e) {

e.printStackTrace();

}

return stuList;

}

public List findByCon(Map map){

List stuList = new ArrayList();

try {

stuList = sqlMapClient.queryForList("findByCon2",map);

} catch (SQLException e) {

e.printStackTrace();

}

return stuList;

}

}

总结

// JBuilder API Decompiler stub source generated from class file // 2010-1-15 // -- implementation of methods is not available package com.ibatis.common.jdbc; // Imports import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Savepoint; import java.sql.Statement; import java.util.Map; public class SimplePooledConnection implements InvocationHandler { // Fields private static final String CLOSE = "close"; private static final Class[] IFACES; private int hashCode; private SimpleDataSource dataSource; private Connection realConnection; private Connection proxyConnection; private long checkoutTimestamp; private long createdTimestamp; private long lastUsedTimestamp; private int connectionTypeCode; private boolean valid; // Constructors public SimplePooledConnection(Connection connection, SimpleDataSource dataSource) { } // Methods public void invalidate() { } public boolean isValid() { return false;} public Connection getRealConnection() { return null;} public Connection getProxyConnection() { return null;} public int getRealHashCode() { return 0;} public int getConnectionTypeCode() { return 0;} public void setConnectionTypeCode(int connectionTypeCode) { } public long getCreatedTimestamp() { return 0L;} public void setCreatedTimestamp(long createdTimestamp) { } public long getLastUsedTimestamp() { return 0L;} public void setLastUsedTimestamp(long lastUsedTimestamp) { } public long getTimeElapsedSinceLastUse() { return 0L;} public long getAge() { return 0L;} public long getCheckoutTimestamp() { return 0L;} public void setCheckoutTimestamp(long timestamp) { } public long getCheckoutTime() { return 0L;} private Connection getValidConnection() { return null;} public int hashCode() { return 0;} public boolean equals(Object obj) { return false;} public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null;} public Statement createStatement() throws SQLException { return null;} public PreparedStatement prepareStatement(String sql) throws SQLException { return null;} public CallableStatement prepareCall(String sql) throws SQLException { return null;} public String nativeSQL(String sql) throws SQLException { return null;} public void setAutoCommit(boolean autoCommit) throws SQLException { } public boolean getAutoCommit() throws SQLException { return false;} public void commit() throws SQLException { } public void rollback() throws SQLException { } public void close() throws SQLException { } public boolean isClosed() throws SQLException { return false;} public DatabaseMetaData getMetaData() throws SQLException { return null;} public void setReadOnly(boolean readOnly) throws SQLException { } public boolean isReadOnly() throws SQLException { return false;} public void setCatalog(String catalog) throws SQLException { } public String getCatalog() throws SQLException { return null;} public void setTransactionIsolation(int level) throws SQLException { } public int getTransactionIsolation() throws SQLException { return 0;} public SQLWarning getWarnings() throws SQLException { return null;} public void clearWarnings() throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public Map getTypeMap() throws SQLException { return null;} public void setTypeMap(Map map) throws SQLException { } public void setHoldability(int holdability) throws SQLException { } public int getHoldability() throws SQLException { return 0;} public Savepoint setSavepoint() throws SQLException { return null;} public Savepoint setSavepoint(String name) throws SQLException { return null;} public void rollback(Savepoint savepoint) throws SQLException { } public void releaseSavepoint(Savepoint savepoint) throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return null;} }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值