`

【白金山】iBatis再使用小例子(1)

阅读更多

IBatis开发已经是过去时了,现在又要用它开发,所以从一个小例子复习一下。希望对想学iBatis的朋友有用哦!

iBatisapache的一个开源项目,一个O/R Mapping解决方案,iBatis最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis是能够满足你的要求又足够灵活的最简单的解决方案。不少公司的架构师看重的可能也是他的小巧灵活吧,也都在自己的架构中用到了它。

新建一个web项目:iBatisTEST

搭建环境:导入数据库的驱动包、iBatis自身的jar

导入配置文件:

iBatis的配置文件【SqlMapConfig.xml】(Jdbc连接的属性文件(就是键值对driverurlusernamepassword))

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMapConfig

PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"

"sql-map-config-2.dtd">

<sqlMapConfig>

<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="com.microsoft.jdbc.sqlserver.SQLServerDriver" />

在连接MSSQLSERVER时,容易出现这种问题:一个Connection对象创建了一个Statement对象,并且这个Statement对象先后执行了多条SQL语句,那么就需要该Connection对象的SelectMethod=Cursor,默认的SelectMethod=direct

<property name="JDBC.ConnectionURL" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=javatest;SelectMethod=Cursor" />

<property name="JDBC.Username" value="sa" />

<property name="JDBC.Password" value="test" />

-->

<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />

<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />

<property name="JDBC.Username" value="root" />

<property name="JDBC.Password" value="test" />

<property name="JDBC.DefaultAutoCommit" value="true" />

<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 ACCOUNT" />

<property name="Pool.PingEnabled" value="false" />

<property name="Pool.PingConnectionsOlderThan" value="1" />

<property name="Pool.PingConnectionsNotUsedFor" value="1" />

</dataSource>

</transactionManager>

<sqlMap resource="config/Student.xml" />

</sqlMapConfig>

新建一个POJOStudent(属性值和数据库表中的字段相互对应)

package cn.bai;

import java.sql.Date;

public class Student {

private int sid = 0;

private String sname = null;

private String major = null;

private Date birth = null;

private float score = 0;

@Override

public String toString() {

String content = "sid=" + sid + "\tsname="+sname+"\tmajor="+major+"\tbirth="+birth+"\tscore="+score;

return content;

}

public Date getBirth() {

return birth;

}

public void setBirth(Date birth) {

this.birth = birth;

}

public String getMajor() {

return major;

}

public void setMajor(String major) {

this.major = major;

}

public float getScore() {

return score;

}

public void setScore(float score) {

this.score = score;

}

public int getSid() {

return sid;

}

public void setSid(int sid) {

this.sid = sid;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

}

配置Student的配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap

PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics