hibernate和spring的完整结合使用service层
2016-07-26 23:19
555 查看
1.使用service层;
2.StudetnService类;
package com.eduask.service;
import java.util.List;
import com.eduask.pojo.Student;
public interface StudentService {
//增加;
void insertStudent(Student student ) throws Exception;
//删除;
void delectStudent(int id) throws Exception;
//修改;
void updateStudent(Student student) throws Exception;
//查询根据id;
Student selectStudentById(int id) throws Exception;
//查询所有;
List<Student> selectAllStudent() throws Exception;
}
3.StudentServiceImpl类;
package com.eduask.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import com.eduask.dao.StudentDao;
import com.eduask.pojo.Student;
import com.eduask.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Resource(name="studentDaoImpl")
private StudentDao studentDao;
public void insertStudent(Student student) throws Exception {
studentDao.insertStudent(student);
}
public void delectStudent(int id) throws Exception {
studentDao.deleteStudent(id);
}
public void updateStudent(Student student) throws Exception {
studentDao.updateStudent(student);
}
public Student selectStudentById(int id) throws Exception {
Student student=studentDao.getStudentById(id);
return student;
}
public List<Student> selectAllStudent() throws Exception {
return studentDao.getStudents();
}
}
4.StudentTest类;
package com.eduask.test;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.eduask.dao.StudentDao;
import com.eduask.dao.impl.StudentDaoImpl;
import com.eduask.pojo.Student;
import com.eduask.service.StudentService;
import com.eduask.service.impl.StudentServiceImpl;
public class StudentTest {
//引入spring中的bean.xml配置文件;
private ClassPathXmlApplicationContext cx=null;
@Before
public void setUp(){
cx=new ClassPathXmlApplicationContext("spring/bean.xml");
}
//增加;
@Test
public void testInsertStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
Student student=new Student();
student.setName("linux");
student.setPwd("123456");
studentService.insertStudent(student);
}
//删除
//删除需要获得id,在StudentDaoImpl类中删除方法中调用获得id的方法;
@Test
public void testDeleteStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
studentService.delectStudent(2);
}
//修改;
//修改需要获得id;
@Test
public void testUpdateStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
Student student=new Student();
student=studentService.selectStudentById(2);
student.setPwd("11111");
studentService.updateStudent(student);
}
//查询通过id;
@Test
public void testSelectStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
System.out.println(studentService.selectStudentById(2));
}
//查询所有;
@Test
public void testSelectAllStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
List<Student> students=studentService.selectAllStudent();
for (Student student : students) {
System.out.println(student);
}
}
}
2.StudetnService类;
package com.eduask.service;
import java.util.List;
import com.eduask.pojo.Student;
public interface StudentService {
//增加;
void insertStudent(Student student ) throws Exception;
//删除;
void delectStudent(int id) throws Exception;
//修改;
void updateStudent(Student student) throws Exception;
//查询根据id;
Student selectStudentById(int id) throws Exception;
//查询所有;
List<Student> selectAllStudent() throws Exception;
}
3.StudentServiceImpl类;
package com.eduask.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import com.eduask.dao.StudentDao;
import com.eduask.pojo.Student;
import com.eduask.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Resource(name="studentDaoImpl")
private StudentDao studentDao;
public void insertStudent(Student student) throws Exception {
studentDao.insertStudent(student);
}
public void delectStudent(int id) throws Exception {
studentDao.deleteStudent(id);
}
public void updateStudent(Student student) throws Exception {
studentDao.updateStudent(student);
}
public Student selectStudentById(int id) throws Exception {
Student student=studentDao.getStudentById(id);
return student;
}
public List<Student> selectAllStudent() throws Exception {
return studentDao.getStudents();
}
}
4.StudentTest类;
package com.eduask.test;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.eduask.dao.StudentDao;
import com.eduask.dao.impl.StudentDaoImpl;
import com.eduask.pojo.Student;
import com.eduask.service.StudentService;
import com.eduask.service.impl.StudentServiceImpl;
public class StudentTest {
//引入spring中的bean.xml配置文件;
private ClassPathXmlApplicationContext cx=null;
@Before
public void setUp(){
cx=new ClassPathXmlApplicationContext("spring/bean.xml");
}
//增加;
@Test
public void testInsertStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
Student student=new Student();
student.setName("linux");
student.setPwd("123456");
studentService.insertStudent(student);
}
//删除
//删除需要获得id,在StudentDaoImpl类中删除方法中调用获得id的方法;
@Test
public void testDeleteStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
studentService.delectStudent(2);
}
//修改;
//修改需要获得id;
@Test
public void testUpdateStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
Student student=new Student();
student=studentService.selectStudentById(2);
student.setPwd("11111");
studentService.updateStudent(student);
}
//查询通过id;
@Test
public void testSelectStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
System.out.println(studentService.selectStudentById(2));
}
//查询所有;
@Test
public void testSelectAllStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
List<Student> students=studentService.selectAllStudent();
for (Student student : students) {
System.out.println(student);
}
}
}
相关文章推荐
- 崔希凡-javaWeb-笔记day07-day09(2016年7月26日23:17:27)
- Javacard的内部认证和外部认证
- 崔希凡-javaWeb-笔记day07-day09(2016年7月26日23:14:40)
- Session与Cookie的区别
- java 输入一个字符串,打印出该字符串中字符的所有排列
- 用java自带工具javac和java编译运行java程序
- Java中的ExceptionInInitializerError异常及解决方法
- struts学习笔记一
- Eclipse简介和使用技巧快捷方式
- Eclipse简介和使用技巧快捷方式
- Javacard DES/AES/RSA/Hash/Sinature算法API使用示例
- java 获得项目绝对路径
- Spring IoC和DI——学习笔记
- 查找一个数组中第二大的数的下标并输出
- java 学习笔记2
- Java 三种线性排序之计数排序
- Spring Boot入门
- MyEclipse如何全局搜索
- MyEclipse如何全局搜索
- 【职坐标】java IO流前传——File文件类