StudentDao
来源:互联网 发布:asp二维码生成源码 编辑:程序博客网 时间:2024/05/28 18:43
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());
}
}
}
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());
}
}
}
0 0
- StudentDao
- SubList分页-008-StudentDao接口
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDao'
- 搜索
- JDBC示例程序
- noip2014 珠心算测验 (枚举)
- 八皇后问题--递归和非递归
- UITapGestureRecognizer 的用法
- StudentDao
- 读万卷书
- php对图片上传的处理
- Android:Material Design(一) 概述
- (第四周项目1)建立单链表
- Longest Common Prefix
- [九度OnlineJudge][剑指Offer]题目1519:合并两个排序的链表
- java:代理测试
- LINUX VIM快捷键