日期:2014-05-16 浏览次数:20450 次
好久没写jdbc的相关代码了,都忘得差不多了,一直都在用hibernate,这次学习spring,书中有关于jdbc整合的章节,并且讲了很多,于是决定再次写有关jdbc代码。整个写的非常简单,具体如下:
?
第一步:引入相关的jar包
第二步:在spring中配置好数据源(bean.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="spring.test"/> <!-- <aop:aspectj-autoproxy/> --> <!-- mysql 5.0 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/link?useUnicode=true&characterEncoding=gbk"/> <property name="username" value="root"/> <property name="password" value="hyj_lk"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <!-- 连接池的最大值 --> <property name="maxActive" value="500"/> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1"/> </bean> </beans>
?
?
第三步:创建实体bean(说明:这里其实可以不要创建这个bean,为了方便还是创建了这个bean) Sort.java
//*******************************************************************// // //** 创建人: Link // //** 描 述: sort表实体bean -- 分类(栏目)表 // // //*******************************************************************// package spring.test.bean; import java.util.Date; public class Sort { private int id; //标识(主键) private int sid; //分类ID(父ID) private String title; //标题 private String info; //描述 private String url; //url private String imgurl; //图片路径 private int isindex=0; //是否在首页显示(0否,1是 默认0) private int ishot=0; //是否为热门(0否,1是 默认0) private int isnew=0; //是否为新(0否,1是 默认0) private int sortnum=0; //排序号 private long hits=0; //点击次数 private Date puttime=new Date(); //更新日期 private int property=0; //属性:是否通过(0否,1是 默认0) public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getImgurl() { return imgurl; } public void setImgurl(String imgurl) { this.imgurl = imgurl; } public int getIsindex() { return isindex; } public void setIsindex(int isindex) { this.isindex = isindex; } public int getIshot() { return ishot; } public void setIshot(int ishot) { this.ishot = ishot; } public int getIsnew() { return isnew; } public void setIsnew(int isnew) { this.isnew = isnew; } public int getSortnum() { return sortnu