`

iBATIS实现的一个例子

阅读更多

http://www.builder.com.cn/2007/0730/438668.shtml

这里把我学习ibatis时候,一个实现的例子发上来,供参考。

工程目录结构如下:

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实现的一个例子

分享到:
评论

相关推荐

    Ibatis和Spring整合例子,实现增删改查功能

    Ibatis和Spring整合例子,实现增删改查功能.

    ibatis实现增删查改例子

    手把手分步骤教你快速学会ibatis实现增删查改的一个例子,包含了需要的两个jar文件,直接导入myeclipse中即可应用

    一个Ibatis的详细例子

    自己写的一个详细的Ibatis案例,适合初学者,内附mysql数据库,导入数据和项目以后可直接运行,有详细的步骤解释...

    ajax和ibatis的综合应用例子

    ajax和ibatis的综合应用例子,内带了Ibaties的配置文件,是实现的一个读取XML文件的例子

    iBatis工程简单例子

    iBatis工程的简单例子,在eclipse上实现的,数据库是mysql数据库,里面的字段为CREATE DATABASE MYDB; use MYDB; Drop TABLE IF EXISTS `MYDB`.`student`; Create TABLE `MYDB`.`student` ( `name` varchar(40) NOT...

    Ibatis+Spring2.5+Structs2.0整合的一个小例子

    使用Ibatis,Spring,Structs整合做的一个小例子,实现增,删,查的功能。

    Ibatis+spring+Ecside的一个完整例子

    经过一段时间的摸索,写了一个例子,可以完整的实现Ibatis+spring+Ecside的整合,很小,很方便。 不过只是实现了单条件的查询,更复杂的正在做,不久可以和大家分享。 由于设计内部的数据删除了spring的配置文件。

    iBatisNet实现增删改的一个简单例子

    iBatisNet C# WinForm iBatisNet实现增删改的一个简单例子,数据库是sqlservice,大家可以下载来看看。

    一个很基础的ibatis的例子(含增删改查)

    核心提示:实例实现了6个基本功能: 1.向数据库student表插入一条数据 2.删除student表的所有数据 3.删除student表指定ID的数据 4.更新student表指定ID的数据 5.查询student表的所有数据 6.查询student表的指定ID...

    springmvc+spring+ibatis

    上一个版本是结合当前流行的主流框架,今天这个抛开了mybatis这个持久层框架,选择了ibatis作为我的database连接层,这个例子实现了添加一条数据的功能。其实mybatis和ibatis的区别不是很大,配置也只是一点小区别。...

    iBatis入门例子

    iBatis实现对单表的增、删、修、查。

    Spring+DWR+ibatis+jQuery+easyUI的框架例子

    Spring+DWR+ibatis+jQuery+easyUI的框架例子. 完全开放源代码,免费学习与使用。 可以完成基本的后台主界面,报表查询,数据查询,增加,修改等功能。 如果你要做一些报表,后台功能利用这个a框架就可以很方便实现。

    ibatis入门例子

    使用ibatis实现简单的增删改查操作,并且有详细介绍过程,和Eclipse工程

    SSI框架整合例子(spring+struts+ibatis)

    利用spring2.5 + ibatis 2.3 + struts2.1.8实现三个框架的整合。

    eclipse下struts2+spring+ibatis例子

    参考一下链接,修改了下,做了个例子 http://shiningwu.javaeye.com/blog/184117 差别在于action没有继承ModelDriven,并且把所有需要的jar包都打包了,实现了登陆,修改,删除,一览功能 工具为mysql + tomcat6

    ssi框架集成增删改查及存储过程(spring2.5+struts2.1+ibatis2.3)

    利用ssi框架集成增删改查及存储过程处理,利用Junit测试框架进行单元测试。非常全面的例子。

    J2EE核心:iBatis_DAO入门与进阶.doc

    在这个应用程序中,当需要和数据源进行交互的时候则使用这个接口,并且编写一个单独的类来实现这个接口在逻辑上对应这个特定的数据存储。 比如考虑在iBatis: SQL Maps中的应用例子。这是一个Struts应用允许对一个...

    ibatis实战例子

    单纯使用mavan,ibtis实现的工程,附带数据库批量生成SQL文件。

    GWT-EXT+IBATIS+SQL实现表格,树,面板

    对与IBATIS数据结合使用。包括客户端与服务器端,此例子在netbeans6.5下面可以正常运行 整个工程文件太大,这里只能上传源代码,如果需要整个工程的可以发邮件给我 xuzhenqinandy@163.com 或加QQ一起交流...

    spingmvc+velocity+ibatis+mysql开发

    自己做的一个web例子,实现了用户的增、删、改、查等功能 用到的spingmvc+velocity+ibatis+mysql 自己创建完数据库后完全可以运行

Global site tag (gtag.js) - Google Analytics