- 浏览: 1212969 次
- 性别:
- 来自: 深圳
-
文章分类
- 全部博客 (718)
- HTML (13)
- JS基础 (23)
- JS应用 (40)
- AJAX (6)
- JSP相关 (12)
- JAVA基础 (52)
- JAVA应用 (74)
- APPLET (11)
- SWING\RCP (2)
- JAVA反射 (6)
- 设计模式 (26)
- 数据库设计 (20)
- Struts (35)
- Struts2 (12)
- Spring (22)
- Hibernate (45)
- Ibatis (18)
- mybatis (3)
- SSH (8)
- UML (5)
- WebService (3)
- XML (16)
- Log4j (7)
- WEB容器 (26)
- 数据结构 (36)
- Linux (34)
- Ruby on Rails (1)
- 其它技术 (27)
- IDE配置 (15)
- 项目实战 (2)
- Oracle (69)
- JAVA报表 (7)
- Android学习 (2)
- 博客链接 (1)
- 网络基础 (1)
- WEB集群 (1)
- .Net开发 (11)
- PB (4)
- 系统构建 (15)
最新评论
-
jnjeC:
牛逼啊哥们,讲得太好了
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包 -
九尾狐的yi巴:
很好 感谢!
Itext中文处理(更新版) -
luweifeng1983:
有用的,重启一下嘛。
设置eclipse外部修改文件后自动刷新 -
Master-Gao:
设置了也不管用,怎么破呢?
设置eclipse外部修改文件后自动刷新 -
aigo_h:
锋子还有时间写博客,还是很闲哈!
Add directory entries问题
http://www.builder.com.cn/2007/0730/438668.shtml
这里把我学习ibatis时候,一个实现的例子发上来,供参考。
工程目录结构如下:
//1、SQL MAP配置文件
SqlMapConfig.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>
<properties resource="com/study/xiaofeng/maps/SqlMapConfig.properties"/>
<settings cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="$" />
<property name="JDBC.ConnectionURL" value="$" />
<property name="JDBC.Username" value="$" />
<property name="JDBC.Password" value="$" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery" value="select 1 from sample" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<!--
<transactionManager type="JTA" >
<property name="UserTransaction"
value="java:comp/env/jdbc/framework"/>
<dataSource type="JNDI">
<property name="DataSource"
value="java:comp/env/jdbc/ibatistest"/>
</dataSource>
</transactionManager>
<transactionManager type="JDBC" >
<dataSource type="JNDI">
<property name="DataSource"
value="java:comp/env/jdbc/ibatistest"/>
</dataSource>
</transactionManager>
-->
<sqlMap resource="com/study/xiaofeng/maps/person.xml" />
</sqlMapConfig>
//2、SQL Map配置文件拥有唯一的<properties>元素,这样做后,在属性文件中定义的属性可以作为变量在SQL Map配置文件及其包含的所有SQL Map映射文件中引用
SqlMapConfig.xml
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ibatistest;SelectMethod=Cursor;
username=xiaofeng
password=xiaofeng
//3、SQL Map XML映射
person.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="Student">
<typeAlias alias="student" type="com.study.xiaofeng.Student"/>
<typeAlias alias="course" type="com.study.xiaofeng.Course"/>
<typeAlias alias="intro" type="com.study.xiaofeng.Intro"/>
<typeAlias alias="sc" type="com.study.xiaofeng.SC"/>
<resultMap id="get-student-result" class="student" >
<result property="sno" column="Sno"/>
<result property="sname" column="Sname"/>
<result property="ssex" column="Ssex"/>
<result property="sage" column="Sage"/>
<result property="sdept" column="Sdept"/>
<result property="sc" column="Sno" select="getSc"/>
<result property="intro" column="Sno" select="getIntro"/>
</resultMap>
<resultMap id="get-course-result" class="course">
<result property="cno" column="Cno"/>
<result property="cname" column="cname"/>
<result property="ccredit" column="Ccredit"/>
</resultMap>
<resultMap class="intro" id="get-intro-result">
<result property="sno" column="Sno"/>
<result property="idescription" column="Idescription"/>
</resultMap>
<!--
<resultMap id="get-sc-result" class="sc" >
<result property="sno" column="Sno"/>
<result property="cno" column="Cno"/>
<result property="grade" column="Grade"/>
<result property="course" column="Cno" select="getCourse"/>
</resultMap>
-->
<select id="getStudent" parameterClass="String" resultMap="get-student-result">
select * from STUDENT
WHERE
Sname=#value#
</select>
<select id="getCourse" parameterClass="Integer" resultMap="get-course-result">
select * from COURSE WHERE Cno=#value#
</select>
<select id="getIntro" parameterClass="Integer" resultMap="get-intro-result">
select *from INTRO WHERE Sno=#value#
</select>
<select id="getSc" parameterClass="Integer" resultClass="SC">
select Cno,Grade
from SC
WHERE
Sno=#sno#
</select>
<insert id="insertStudent" parameterClass="student">
INSERT INTO
STUDENT (Sno,Sname,Ssex,Sage,Sdept)
VALUES (#sno#,#sname#,#ssex#,#sage#,#sdept#)
</insert>
<update id="updateStudent" parameterClass="student">
UPDATE STUDENT
SET Sname= #sname#,
Ssex= #ssex#,
Sage=#sage#,
Sdept=#sdept#
WHERE Sno = #sno#
</update>
<delete id="deleteStudent" parameterClass="student">
DELETE STUDENT
WHERE Sno = #sno#
</delete>
</sqlMap>
//4、AppSqlConfig.java
package com.study.xiaofeng;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.io.Reader;
import com.ibatis.common.resources.*;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class AppSqlConfig {
private static final SqlMapClient sqlMap;
static {
try {
String resource ="com/study/xiaofeng/maps/SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException ("Error initializing MyAppSqlConfig class. Cause: "+e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
//test.java
package com.study.xiaofeng;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.*;
import java.util.List;
//JTA
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
public class Test {
public static void update(int no,String name,String sex,int age,String dept){
com.ibatis.sqlmap.client.SqlMapClient client = null;
try{
client=new AppSqlConfig().getSqlMapInstance();
client.startTransaction();
Student student=new Student();
student.setSno(no);
student.setSname(name);
student.setSsex(sex);
student.setSage(age);
student.setSdept(dept);
client.update("updateStudent",student);
client.commitTransaction();
}catch(SQLException e)finally {
try catch (SQLException e)
}
}
public static void insertStudent(int no,String name,String sex,int age,String dept){
com.ibatis.sqlmap.client.SqlMapClient client = null;
try{
client=new AppSqlConfig().getSqlMapInstance();
client.startTransaction();
Student student=new Student();
student.setSno(no);
student.setSname(name);
student.setSsex(sex);
student.setSage(age);
student.setSdept(dept);
client.insert("insertStudent",student);
client.commitTransaction();
}catch(SQLException e)finally {
try catch (SQLException e)
}
}
//一个对象直接作为属性,实现关联
public static Student getStudent(){
com.ibatis.sqlmap.client.SqlMapClient client = null;
Student student=null;
try{
client=new AppSqlConfig().getSqlMapInstance();
client.startTransaction();
student = (Student)client.queryForObject("getStudent","xiaofeng");
client.commitTransaction();
}catch(SQLException e)finally{
try catch (SQLException e)
}
return student;
}
//多个对象放到List中作为一个属性 实现关联,但是得嵌套查询。 测试一对多的关联查询
//也可以实现多对多
public static void reStudent(String name){
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
sqlMap=new AppSqlConfig().getSqlMapInstance();
try{
sqlMap.startTransaction();
List studentList=sqlMap.queryForList("getStudent",name);
for(int i=0;i<studentList.size();i++){
Student student=(Student)studentList.get(i);
System.out.println("姓名(表1):"+student.getSname());
for(int k=0;k<student.getSc().size();k++){
SC sc=(SC)student.getSc().get(k);
Course course=(Course)sqlMap.queryForObject("getCourse", sc.getCno());
System.out.print("课程号(表2):"+sc.getCno());
System.out.print("------课程名(表3):"+course.getCname().trim()+"------分数(表2): "+sc.getGrade()+"n");
}
}
sqlMap.commitTransaction();
}catch(SQLException e)finally{
try catch (SQLException e)
}
}
public static void main(String args[]){
// update(2004131301,"xiaofeng","男",23,"信息");
reStudent("name2");
Student student=getStudent();
System.out.println("表4 描述:"+student.getIntro().getIdescription());
// insertStudent(2004131305,"xiaofeng5","男",23,"信息"); ;
}
}
//Course.java
package com.study.xiaofeng;
import java.io.Serializable;
public class Course implements Serializable{
private int cno;
private String cname;
private int ccredit;
public int getCno(){
return this.cno;
}
public void setCno(int no)
public String getCname(){
return this.cname;
}
public void setCname(String name)
public int getCcredit(){
return this.ccredit;
}
public void setCcredit(int credit)
}
//Intro.java
package com.study.xiaofeng;
import java.io.Serializable;
public class Intro implements Serializable{
private int sno;
private String idescription;
public int getSno(){
return this.sno;
}
public void setSno(int sno)
public String getIdescription(){
return this.idescription;
}
public void setIdescription(String description)
}
//Sc.java
package com.study.xiaofeng;
import java.io.Serializable;
public class SC implements Serializable{
private int sno;
private int cno;
private int grade;
private Course course;
public int getSno(){
return this.sno;
}
public void setSno(int no)
public int getCno(){
return this.cno;
}
public void setCno(int no)
public int getGrade(){
return this.grade;
}
public void setGrade(int grade)
public Course getCourse(){
return this.course;
}
public void setCourse(Course course)
}
//Student.java
package com.study.xiaofeng;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable{
private int sno;
private String sname;
private String ssex;
private int sage;
private String sdept;
private List sc;
private Intro intro;
public int getSno(){
return this.sno;
}
public void setSno(int no)
public String getSname(){
return this.sname;
}
public void setSname(String name)
public String getSsex(){
return this.ssex;
}
public void setSsex(String sex)
public int getSage(){
return this.sage;
}
public void setSage(int age)
public String getSdept(){
return this.sdept;
}
public void setSdept(String dept)
public List getSc(){
return this.sc;
}
public void setSc(List sc)
public Intro getIntro(){
return this.intro;
}
public void setIntro(Intro intro)
}
//运行结果如下:
发表评论
-
IBATIS resultClass为hashMap时的缓存问题
2012-01-16 15:25 1315http://hi.baidu.com/sgqiang5566 ... -
Ibatis读写CLOB数据
2012-01-10 11:20 1264Ibatis读写CLOB数据 注意:需要更新ORACLE数据 ... -
IBATIS调用存储过程
2011-12-16 10:30 869http://www.iteye.com/topic/8422 ... -
Ibatis select in 的处理
2011-11-16 19:48 1790如下: 在ibatis中如何使用in条件select * f ... -
使用ibator快速生成代码
2011-11-08 18:38 599相关链接: ibator使用说明:http://ibatis ... -
spring 3 和mybatis 3集成,并用junit4进行测试
2011-11-04 14:01 1441转:spring 3 和mybatis 3集成,并用junit ... -
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource的解决方法
2011-11-03 16:17 2698用Myeclipse开发struts时,配置struts-co ... -
Ibatis在项目中怎么使用的
2011-11-03 10:44 896Spring中配置: <!--定义dat ... -
ibatis快速入门及与spring整合事务管理
2011-11-03 10:18 1582ibatis学习(一)--ibatis ... -
iBATIS是什么?
2009-08-05 22:40 920http://developer.51cto.com/art/ ... -
iBATIS动态查询的实现浅析
2009-08-05 22:46 1071http://developer.51cto.com/art/ ... -
IBatis 怎样直接执行SQL语句
2009-08-05 22:50 1173http://blog.tanggaowei.com/2009 ... -
选择Hibernate还是iBATIS
2009-08-05 22:54 1331http://robbin.iteye.com/blog/24 ... -
我为什么选择 iBatis 而不是 Hibernate(对于正在选型的人的建议)
2009-08-05 22:56 851http://jiming.iteye.com/blog/41 ... -
ibatis教程入门示例
2009-08-05 23:07 2794http://www.javah.net/ibatis/200 ... -
iBatis学习方法及入门总结
2009-08-05 23:09 1574http://www.javah.net/ibatis/200 ... -
iBATIS SQL Maps 入门教程
2009-08-05 23:29 967http://nchc.dl.sourceforge.net/ ...
相关推荐
Ibatis和Spring整合例子,实现增删改查功能.
手把手分步骤教你快速学会ibatis实现增删查改的一个例子,包含了需要的两个jar文件,直接导入myeclipse中即可应用
自己写的一个详细的Ibatis案例,适合初学者,内附mysql数据库,导入数据和项目以后可直接运行,有详细的步骤解释...
ajax和ibatis的综合应用例子,内带了Ibaties的配置文件,是实现的一个读取XML文件的例子
iBatis工程的简单例子,在eclipse上实现的,数据库是mysql数据库,里面的字段为CREATE DATABASE MYDB; use MYDB; Drop TABLE IF EXISTS `MYDB`.`student`; Create TABLE `MYDB`.`student` ( `name` varchar(40) NOT...
使用Ibatis,Spring,Structs整合做的一个小例子,实现增,删,查的功能。
经过一段时间的摸索,写了一个例子,可以完整的实现Ibatis+spring+Ecside的整合,很小,很方便。 不过只是实现了单条件的查询,更复杂的正在做,不久可以和大家分享。 由于设计内部的数据删除了spring的配置文件。
iBatisNet C# WinForm iBatisNet实现增删改的一个简单例子,数据库是sqlservice,大家可以下载来看看。
核心提示:实例实现了6个基本功能: 1.向数据库student表插入一条数据 2.删除student表的所有数据 3.删除student表指定ID的数据 4.更新student表指定ID的数据 5.查询student表的所有数据 6.查询student表的指定ID...
上一个版本是结合当前流行的主流框架,今天这个抛开了mybatis这个持久层框架,选择了ibatis作为我的database连接层,这个例子实现了添加一条数据的功能。其实mybatis和ibatis的区别不是很大,配置也只是一点小区别。...
iBatis实现对单表的增、删、修、查。
Spring+DWR+ibatis+jQuery+easyUI的框架例子. 完全开放源代码,免费学习与使用。 可以完成基本的后台主界面,报表查询,数据查询,增加,修改等功能。 如果你要做一些报表,后台功能利用这个a框架就可以很方便实现。
使用ibatis实现简单的增删改查操作,并且有详细介绍过程,和Eclipse工程
利用spring2.5 + ibatis 2.3 + struts2.1.8实现三个框架的整合。
参考一下链接,修改了下,做了个例子 http://shiningwu.javaeye.com/blog/184117 差别在于action没有继承ModelDriven,并且把所有需要的jar包都打包了,实现了登陆,修改,删除,一览功能 工具为mysql + tomcat6
利用ssi框架集成增删改查及存储过程处理,利用Junit测试框架进行单元测试。非常全面的例子。
在这个应用程序中,当需要和数据源进行交互的时候则使用这个接口,并且编写一个单独的类来实现这个接口在逻辑上对应这个特定的数据存储。 比如考虑在iBatis: SQL Maps中的应用例子。这是一个Struts应用允许对一个...
单纯使用mavan,ibtis实现的工程,附带数据库批量生成SQL文件。
对与IBATIS数据结合使用。包括客户端与服务器端,此例子在netbeans6.5下面可以正常运行 整个工程文件太大,这里只能上传源代码,如果需要整个工程的可以发邮件给我 xuzhenqinandy@163.com 或加QQ一起交流...
自己做的一个web例子,实现了用户的增、删、改、查等功能 用到的spingmvc+velocity+ibatis+mysql 自己创建完数据库后完全可以运行