HardBirch

Spring2.5+Struts2+Ibatis整合之四

时间:09-05-17 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:0 点击: 1,292 次

该篇主要编写DAO层代码。

src下创建包com.zyg.ssi.dao,在该包下创建接口StudentDao,其代码如下:

 

import java.util.List;
import com.zyg.ssi.bean.Student;
public interface StudentDao {

/**
* 保存学生信息
* @param student
*/
public void addStudent(Student student);

/**
* 根据学号删除学生信息
* @param studentId
*/
public void delStudentById(int id);

/**
* 更新学生信息
* @param student
*/
public void updateStudentById(Student student);

/**
* 根据学号获取学生信息
* @param studentId
*/
public Student queryStudentById(int id);

/**
* 根据姓名获取学生信息
* @param studentId
*/
public List<Student> queryStudentByName(String name);

/**
* 获取全部学生信息
* @param studentId
*/
@SuppressWarnings("unchecked")
public List<Student> queryAllStudents();

}

src下创建包com.zyg.ssi.dao.ibaits,在该包下创建实现StudentDao接口的类StudentDaoImpl,其代码如下:

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.zyg.ssi.bean.Student;
import com.zyg.ssi.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {
private SqlMapClient sqlMapClient;

public void addStudent(Student student) {
// TODO Auto-generated method stub
try {
sqlMapClient.insert("insertStudent", student);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void addStudentBySequence(Student student) {
// TODO Auto-generated method stub

}

public void delStudentById(int id) {
try {
sqlMapClient.delete("delStudentById", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public List<Student> queryAllStudents() {
// TODO Auto-generated method stub
List<Student> stuList = null;
try {
stuList = sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return stuList;
}

public Student queryStudentById(int id) {
Student student=null;
try {
student = (Student)sqlMapClient.queryForObject("selectStudentById", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}

public List<Student> queryStudentByName(String name) {
// TODO Auto-generated method stub
List<Student> stuList = null;
try {
stuList = sqlMapClient.queryForList("selectStudentByName",name);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stuList;
}

public void updateStudentById(Student student) {
// TODO Auto-generated method stub
try {
sqlMapClient.update("updateStudent", student);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public SqlMapClient getSqlMapClient() {
return sqlMapClient;
}

public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
}

至此,改篇完成了DAO层代码的编写。下一篇开始编写Service层代码以及针对Service层功能的Junit测试代码。

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: Spring2.5+Struts2+Ibatis整合之四

Spring2.5+Struts2+Ibatis整合之四:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐