本实例是只使用ibaties进行的开发,后续会分享使用spring的例子。
第一步:编写sqlmap.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql:///test" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="root" />
</dataSource>
</transactionManager>
<sqlMap resource="com/warefont/pojo/Employee.xml" />
</sqlMapConfig>
第二步:编写Employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="employee">
<insert id="insert_employee" parameterClass="com.warefont.pojo.Employee">
insert into employee(name,gender,department) values
(#name#,#gender#,#department#)
</insert>
<select id="getEmployeet" resultClass="com.warefont.pojo.Employee">
select id, name, gender,department from employee
</select>
<delete id="delEmployee" parameterClass="int">
delete from employee where id=#value#
</delete>
<update id="updateEmployee" parameterClass="com.warefont.pojo.Employee">
update employee set name=#name#,gender=#gender#,department=#department#
where id=#id#
</update>
</sqlMap>
第三步:将配置文件注入到程序中
package com.warefont.util;
import java.io.IOException;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class GetSqlMapClient {
private static SqlMapClient sqlMapClient;
static {
String resource = "sqlMap.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlMapClient getSqlMapInstance() {
return sqlMapClient;
}
}
第四步:使用
package com.warefont.dao;
import java.sql.SQLException;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.warefont.pojo.Employee;
import com.warefont.util.*;
public class EmployeeDao {
public void insertEmp(Employee emp){
SqlMapClient sqlMap = GetSqlMapClient.getSqlMapInstance();
try {
sqlMap.insert("insert_employee", emp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
第五步:测试
package com.warefont.test;
import com.warefont.dao.EmployeeDao;
import com.warefont.pojo.Employee;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Employee emp = new Employee();
emp.setDepartment("dept");
emp.setGender("testGender");
emp.setName("hewei");
EmployeeDao empDao = new EmployeeDao();
empDao.insertEmp(emp);
}
}
PS:这只是一个简单的应用,但是包含了其基本的使用。
分享到:
相关推荐
其中包含iBatis精讲PDF iBatis实例 iBatis开发指南 iBatis分页 spring+iBatis处理1对多数据表实例等等若干帮助文档。希望可以帮助大家学习iBatis...
IBatis开发实例IBatis开发实例
Ibatis应用实例.docx,非常非常值得推荐下载
ibatis demo,ibatis例子,ibatis示例
复杂SQL查询
此为本人写的一个Castle+ibatis示例代码,使用了Castle中的IOC容器,Facility事务管理,ibatis由Castle进行管理.供大家下载,共同进步!
ibatis经典示例
struts2+hibernate+spring+ibatis 小实例struts2+hibernate+spring+ibatis 小实例struts2+hibernate+spring+ibatis 小实例struts2+hibernate+spring+ibatis 小实例struts2+hibernate+spring+ibatis 小实例struts2+...
ibatis 实例 配置 源码 依赖包 ibatis-2.3.4.726.jar commons-logging-api.jar commons-logging-1.1.jar commons-dbcp-1.2.2.jar commons-pool.jar
Ibatis.net学习和实例Ibatis.net学习和实例Ibatis.net学习和实例Ibatis.net学习和实例
struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例
struts2 + spring + ibatis 实例 struts2 + spring + ibatis 实例 struts2 + spring + ibatis 实例 struts2 + spring + ibatis 实例 struts2 + spring + ibatis 实例
NULL 博文链接:https://qing393260529.iteye.com/blog/459723
一个ibatis的实例,一个ibatis的实例,一个ibatis的实例
iBatis Web用法实例工程
iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目。最初侧重于密码软件的开发,现在是一个基于Java的持久层框架。
自己做的一个小例子,struts1+spring2.0+ibatis整合实例,希望大家指教。 数据库用的是mysql5,其中用到一个表emp,字段为id,name。
spring3.0加ibatis , 实例, 公司用的框架, 空项目 ,很稳定
本例子集成了ibatis的常用操作,如果掌握了本实例教程,相信应付一般的工作是没有问题的了。本实例教程还集成了数据库脚本。导入工程就可以运行。里面有详细的注释。
基于C# .NET的开发实例,采用IBatis,初学者必看。轻松学会IBatis。