StudentDao

package cn.test.dao.impl;  
  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.List;  
  
import org.apache.ibatis.session.RowBounds;  
import org.apache.ibatis.session.SqlSession;  
  
import cn.test.dao.GroupDao;  
import cn.test.dao.StudentDao;  
import cn.test.entity.Group;  
import cn.test.entity.Student;  
import cn.test.utils.MyBatisUtils;  
  
public class StudentDaoImpl implements StudentDao {  
  
    @Override  
    public void add(Student student) {  
        SqlSession session = MyBatisUtils.getSession();  
        try {  
            StudentDao sd = session.getMapper(StudentDao.class);  
            sd.add(student);  
            session.commit();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            MyBatisUtils.closeSession();  
        }  
    }  
  
    @Override  
    public void deleteById(int id) {  
        SqlSession session = MyBatisUtils.getSession();  
        try {  
            StudentDao sd = session.getMapper(StudentDao.class);  
            sd.deleteById(id);  
            session.commit();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            MyBatisUtils.closeSession();  
        }  
    }  
  
    @Override  
    public Student getById(int id) {  
        SqlSession session = MyBatisUtils.getSession();  
        Student stu = new Student();  
        try {  
            StudentDao sd = session.getMapper(StudentDao.class);  
            stu = sd.getById(id);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            MyBatisUtils.closeSession();  
        }  
        return stu;  
    }  
  
    @Override  
    public void update(Student student) {  
        // TODO Auto-generated method stub  
  
    }  
  
    @Override  
    public List<Student> getAllStudent() {  
        SqlSession session = MyBatisUtils.getSession();  
        List<Student> stus = new ArrayList<Student>();  
        try {  
            //RowBounds的下标是从0开始,表示第一条记录,此表示从第2条记录开始,取4条记录  
            RowBounds rb = new RowBounds(1, 4);  
            stus = session.selectList("cn.test.dao.StudentDao.getAllStudent", null, rb);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            MyBatisUtils.closeSession();  
        }  
        return stus;  
    }  
  
    @Override  
    public Student many2one(int id) {  
        SqlSession session = MyBatisUtils.getSession();  
        Student stu = new Student();  
        try {  
            StudentDao sd= session.getMapper(StudentDao.class);  
            stu = sd.many2one(2);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            MyBatisUtils.closeSession();  
        }  
        return stu;  
    }  
  
    @Override  
    public Student getStudentAndGroupUseSelect(int id) {  
        // 测试association 的 select 属性,发送两条SQL效率较低  
        return null;  
    }  
  
    @Override  
    public List<Student> getStudentBySomeCondition(Student student) {  
        SqlSession session = MyBatisUtils.getSession();  
        List<Student> stus = new ArrayList<Student>();  
        try {  
            StudentDao sd = session.getMapper(StudentDao.class);  
            stus = sd.getStudentBySomeCondition(student);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            MyBatisUtils.closeSession();  
        }  
        return stus;  
    }  
      
    public static void main(String[] args) {  
        StudentDao sd = new StudentDaoImpl();  
        GroupDao gd = new GroupDaoImpl();  
          
        /*测试添加学生 
        Student stu = new Student(); 
        Group group = gd.getByIdResultMap(1); 
        stu.setName("OK"); 
        stu.setBirth(new Date()); 
        stu.setGroup(group); 
        sd.add(stu); 
        */  
          
        /*根据id删除学生信息 
        sd.deleteById(1); 
        */  
          
        /*根据id查询学生信息 
        Student stu = sd.getById(2); 
        System.out.println(stu.getName()+"--"+stu.getBirth().toLocaleString()); 
        */  
          
        /*分页查询所有学生信息 
        List<Student> stus = sd.getAllStudent(); 
        for (Student student : stus) { 
            System.out.println(student.getId()+"-->"+student.getName()+"-->"+student.getBirth().toLocaleString()); 
        } 
        */  
          
        /*根据id查询学生信息与分组信息 
        Student stu = sd.many2one(2); 
        System.out.println(stu.getName()+"--"+stu.getBirth().toLocaleString()+"-->"+stu.getGroup().getName()); 
        */  
          
        /*动态SQL*/  
        Student s = new Student();  
        s.setId(5);  
        s.setName("了");  
        List<Student> stus = sd.getStudentBySomeCondition(s);  
        for (Student student : stus) {  
            System.out.println(student.getId()+"-->"+student.getName()+"-->"+student.getBirth().toLocaleString());  
        }  
          
    }  
}  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值