iBatis简单入门教程(3)

package com.iflytek.dao;

import Java.util.List;

import com.iflytek.entity.Student;

/**
 * @author xudongwang 2011-12-31
 *
 *        Email:xdwangiflytek@gmail.com
 *
 */
public interface StudentDao {

/**
  * 添加学生信息
  *
  * @param student
  *            学生实体
  * @return 返回是否添加成功
  */
 public boolean addStudent(Student student);

/**
  * 根据学生id删除学生信息
  *
  * @param id
  *            学生id
  * @return 删除是否成功
  */
 public boolean deleteStudentById(int id);

/**
  * 更新学生信息
  *
  * @param student
  *            学生实体
  * @return 更新是否成功
  */
 public boolean updateStudent(Student student);

/**
  * 查询全部学生信息
  *
  * @return 返回学生列表
  */
 public List<Student> selectAllStudent();

/**
  * 根据学生姓名模糊查询学生信息
  *
  * @param name
  *            学生姓名
  * @return 学生信息列表
  */
 public List<Student> selectStudentByName(String name);

/**
  * 根据学生id查询学生信息
  *
  * @param id
  *            学生id
  * @return 学生对象
  */
 public Student selectStudentById(int id);

}

StudentDaoImpl

package com.iflytek.daoimpl;

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

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.iflytek.dao.StudentDao;
import com.iflytek.entity.Student;

/**
 * @author xudongwang 2011-12-31
 *
 *        Email:xdwangiflytek@gmail.com
 *
 */
public class StudentDaoImpl implements StudentDao {

 private static SqlMapClient sqlMapClient = null;

 // 读取配置文件
 static {
  try {
   Reader reader = Resources
     .getResourceAsReader("com/iflytek/entity/SqlMapConfig.xml");
   sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public boolean addStudent(Student student) {
  Object object = null;
  boolean flag = false;
  try {
   object = sqlMapClient.insert("addStudent", student);
   System.out.println("添加学生信息的返回值:" + object);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  if (object != null) {
   flag = true;
  }
  return flag;
 }

 public boolean deleteStudentById(int id) {
  boolean flag = false;
  Object object = null;
  try {
   object = sqlMapClient.delete("deleteStudentById", id);
   System.out.println("删除学生信息的返回值:" + object + ",这里返回的是影响的行数");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  if (object != null) {
   flag = true;

  }
  return flag;

 }

 public boolean updateStudent(Student student) {
  boolean flag = false;
  Object object = false;
  try {
   object = sqlMapClient.update("updateStudent", student);
   System.out.println("更新学生信息的返回值:" + object + ",返回影响的行数");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  if (object != null) {
   flag = true;
  }
  return flag;
 }

 public List<Student> selectAllStudent() {
  List<Student> students = null;
  try {
   students = sqlMapClient.queryForList("selectAllStudent");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return students;
 }

 public List<Student> selectStudentByName(String name) {
  List<Student> students = null;
  try {
   students = sqlMapClient.queryForList("selectStudentByName",name);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return students;
 }

 public Student selectStudentById(int id) {
  Student student = null;
  try {
   student = (Student) sqlMapClient.queryForObject(
     "selectStudentById", id);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return student;
 }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/18830.html