blic class Grade implements Serializable{
private int grade_id;
private User user;
private double grade;
.....//省略set、get方法
}
应用Spring JDBC的DAO:(省略DAO接口)
//UserJdbcDao.java:
package com.dao.jdbc;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.dao.UserDao;
import com.domain.User;
public class UserJdbcDao extends JdbcDaoSupport implements UserDao{
public void addUser(User user){
String SQL = "INSERT INTO user(user_id,user_name,user_password) VALUES(?,?,?)";
Object[] params = new Object[]{
user.getUser_id(),user.getUser_name(),user.getUser_password()
};
this.getJdbcTemplate().update(SQL, params);
}
}
//GradeJdbcDao.java:
package com.dao.jdbc;
import com.dao.GradeDao;
import com.domain.Grade;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class GradeJdbcDao extends JdbcDaoSupport implements GradeDao{
public void addGrade(Grade grade){
final String SQL = "INSERT INTO grade(user_id,grade) VALUES(?,?)";
Object[] params = new Object[]{
grade.getUser().getUser_id(),grade.getGrade()
};
this.getJdbcTemplate().update(SQL, params);
}
}
//Domain对象User.java:
package com.domain;
import java.io.Serializable;
public class User implements Serializable {
private int user_id;
private String user_name;
private String user_password;
......//省略set、get方法
}
//Domain对象Grade.java:
package com.domain;
import java.io.Serializable;
public class Grade implements Serializable{
private int grade_id;
private User user;
private double grade;
.....//省略set、get方法
}
Spring集成JOTM的JTA事务管理(3)
时间:2011-02-06 mrzhangtufu
应用Spring JDBC的DAO:(省略DAO接口)
//UserJdbcDao.java:
package com.dao.jdbc;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.dao.UserDao;
import com.domain.User;
public class UserJdbcDao extends JdbcDaoSupport implements UserDao{
public void addUser(User user){
String SQL = "INSERT INTO user(user_id,user_name,user_password) VALUES(?,?,?)";
Object[] params = new Object[]{
user.getUser_id(),user.getUser_name(),user.getUser_password()
};
this.getJdbcTemplate().update(SQL, params);
}
}
//GradeJdbcDao.java:
package com.dao.jdbc;
import com.dao.GradeDao;
import com.domain.Grade;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class GradeJdbcDao extends JdbcDaoSupport implements GradeDao{
public void addGrade(Grade grade){
final String SQL = "INSERT INTO grade(user_id,grade) VALUES(?,?)";
Object[] params = new Object[]{
grade.getUser().getUser_id(),grade.getGrade()
};
this.getJdbcTemplate().update(SQL, params);
}
}
5、应用了JTA事务管理的业务类(省略了接口),用@Transactional注解标注,以在配置文件中可以用<tx:annotatio |