beans-jdbc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"></property>
<property name="url" value="jdbc:db2://192.168.25.230:50000/JSAMPLE"></property>
<property name="username" value="zyl"></property>
<property name="password" value="123"></property>
</bean>
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="usersDao" class="jdbc.dao.IUsersDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- TransactionProxyFactoryBean -->
<bean id="transProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<property name="target" ref="usersDao"></property>
<property name="proxyTargetClass" value="true"></property>
<property name="transactionManager" ref="tx"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
</beans>
-----------------------
package jdbc.dao;
import jdbc.UsersPO;
public interface IUsersDAO {
public void saveUsers(UsersPO usersPO);
}
------------------------
package jdbc.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jdbc.UsersPO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class IUsersDAOImpl extends JdbcDaoSupport implements IUsersDAO {
public IUsersDAOImpl() {
// BasicDataSource bds = new BasicDataSource();
//
// bds.setDriverClassName("com.ibm.db2.jcc.DB2Driver");
// bds.setUrl("jdbc:db2://192.168.25.230:50000/JSAMPLE");
// bds.setUsername("zyl");
// bds.setPassword("123");
//
// jdbcTemplate = new JdbcTemplate(bds);
//
// tm = new DataSourceTransactionManager(bds);
// td = new DefaultTransactionDefinition(
// TransactionDefinition.PROPAGATION_REQUIRED);
// td.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
}
// 查询方法
public void findAll() {
List<UsersPO> result = (List<UsersPO>) getJdbcTemplate().query(
"select usersid,usersname,password from users",
new ResultSetExtractor() {
// 查询结果集
public Object extractData(ResultSet rs)
throws SQLException, DataAccessException {
List<UsersPO> usersList = new ArrayList<UsersPO>();
UsersPO usersPO = null;
while (rs.next()) {
usersPO = new UsersPO(rs.getLong(1), rs
.getString(2), rs.getString(3), null);
usersList.add(usersPO);
}
return usersList;
}
});
for (UsersPO usersPO : result) {
System.out.println(usersPO);
}
}
public void saveUsers(UsersPO usersPO) {
getJdbcTemplate()
.execute("insert into users (usersname,password) values('"
+ usersPO.getUsersName() + "','"
+ usersPO.getUsersPassword() + "')");
}
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"beans-jdbc.xml");
IUsersDAOImpl daoImpl = (IUsersDAOImpl) appContext.getBean("transProxy");
daoImpl.saveUsers(new UsersPO("zyl", "12311", null));
daoImpl.findAll();
}
}
---------------------------
package jdbc;
import java.io.Serializable;
import java.util.Date;
public class UsersPO implements Serializable {
private Long usersID;
private String usersName;
private String usersPassword;
private Date regDate;
public UsersPO() {
super();
// TODO Auto-generated constructor stub
}
public UsersPO(Long usersID, String usersName, String usersPassword,
Date regDate) {
super();
this.usersID = usersID;
this.usersName = usersName;
this.usersPassword = usersPassword;
this.regDate = regDate;
}
public UsersPO(String usersName, String usersPassword, Date regDate) {
super();
this.usersName = usersName;
this.usersPassword = usersPassword;
this.regDate = regDate;
}
public Date getRegDate() {
return regDate;
}
public Long getUsersID() {
return usersID;
}
public String getUsersName() {
return usersName;
}
public String getUsersPassword() {
return usersPassword;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public void setUsersID(Long usersID) {
this.usersID = usersID;
}
public void setUsersName(String usersName) {
this.usersName = usersName;
}
public void setUsersPassword(String usersPassword) {
this.usersPassword = usersPassword;
}
@Override
public String toString() {
return "UsersPO [regDate=" + regDate + ", usersID=" + usersID
+ ", usersName=" + usersName + ", usersPassword="
+ usersPassword + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((usersID == null) ? 0 : usersID.hashCode());
result = prime * result
+ ((usersName == null) ? 0 : usersName.hashCode());
result = prime * result
+ ((usersPassword == null) ? 0 : usersPassword.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UsersPO other = (UsersPO) obj;
if (usersID == null) {
if (other.usersID != null)
return false;
} else if (!usersID.equals(other.usersID))
return false;
if (usersName == null) {
if (other.usersName != null)
return false;
} else if (!usersName.equals(other.usersName))
return false;
if (usersPassword == null) {
if (other.usersPassword != null)
return false;
} else if (!usersPassword.equals(other.usersPassword))
return false;
return true;
}
}
--------------------------
CONNECT TO JSAMPLE USER "zyl" USING ;
CREATE TABLE ZYL.USERS ( USERSID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO CACHE ) , USERSNAME VARCHAR (10) , PASSWORD VARCHAR (10) ) ;
CONNECT RESET;
CONNECT TO JSAMPLE USER "zyl" USING ;
ALTER TABLE ZYL.USERS ADD COLUMN REGDATE DATE ;
CONNECT RESET;
------------------------------------
USERSID INTEGER 4 否
USERSNAME VARCHAR 10 是
PASSWORD VARCHAR 10 是
REGDATE DATE 4 是
--------------------------
IUsersDAOImpl输出结果:
UsersPO [regDate=null, usersID=3, usersName=zyl, usersPassword=12311]