1、创建IStudentDao接口,并定义save方法,add方法,update方法;
1. package com.aiit.dao;
2.
3.public interface IStudentDao {
4. void save();
5.
6. void add();
7.
8. void update();
9.}
2、创建IStudentDao对应的实现类StudentDaoImpl,重写里面所有方法;
1.package com.aiit.dao.impl;
2.
3.import com.aiit.dao.IStudentDao;
4.
5.public class StudentDaoImpl implements IStudentDao {
6. @Override
7. public void save() {
8. System.out.println("这是StudentDaoImpl中的save方法!");
9. }
10.
11. @Override
12. public void add() {
13. System.out.println("这是StudentDaoImpl中的add方法!");
14. }
15.
16. @Override
17. public void update() {
18. System.out.println("这是StudentDaoImpl中的update方法!");
19. }
20.}
3、创建IStudentService接口,并定义save方法,add方法,update方法;
1.package com.aiit.service;
2.
3.public interface IStudentService {
4.
5. void save();
6.
7. void add();
8.
9. void update();
10.
11.}
4、创建IStudentService对应的实现类StudentServiceImpl,重写里面所有方法;
1.package com.aiit.service.impl;
2.
3.import com.aiit.dao.IStudentDao;
4.import com.aiit.service.IStudentService;
5.
6.public class StudentServiceImpl implements IStudentService {
7.
8. private IStudentDao studentDao;
9.
10. public IStudentDao getStudentDao() {
11. return studentDao;
12. }
13.
14. public void setStudentDao(IStudentDao studentDao) {
15. this.studentDao = studentDao;
16. }
17.
18. @Override
19. public void save() {
20. studentDao.save();
21. System.out.println("这是StudentServiceImpl中的save方法!");
22. }
23.
24. @Override
25. public void add() {
26. studentDao.add();
27. System.out.println("这是StudentServiceImpl中的add方法!");
28. }
29.
30. @Override
31. public void update() {
32. studentDao.update();
33. System.out.println("这是StudentServiceImpl中的update方法!");
34. }
35.
36.
37.
38.}
5、注入StudentDao对象(通过set方法注入);
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:context="http://www.springframework.org/schema/context"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans
6. http://www.springframework.org/schema/beans/spring-beans.xsd
7. http://www.springframework.org/schema/context
8. http://www.springframework.org/schema/context/spring-context.xsd">
9.
10. <bean id="studentDao" class="com.aiit.dao.impl.StudentDaoImpl"/>
11.
12. <bean id="studentService" class="com.aiit.service.impl.StudentServiceImpl" >
13. <property name="studentDao" ref="studentDao"/>
14. </bean>
15.
16.
17.</beans>
6、创建测试类,调用StudentServiceImpl里面的方法;
1.package com.aiit;
2.
3.import com.aiit.service.IStudentService;
4.import org.junit.Test;
5.import org.springframework.context.ApplicationContext;
6.import org.springframework.context.support.ClassPathXmlApplicationContext;
7.
8.public class MyTest {
9.
10. @Test
11. public void test1(){
12. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
13. IStudentService studentService = (IStudentService) context.getBean("studentService");
14. studentService.save();
15. studentService.add();
16. studentService.update();
17. }
18.
19.}
结果: