1.简答题
根据课本例题,设计一个Student类,该类有姓名、学号等属性,设计实现Student类的dao层、service层与controller类,分别使用xml方式、annotation方式进行bean的装配
1.xml方式小案例
1.配置maven,导入所需的依赖
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
2.创建所需要的包
3.编写pojo实体类
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
private String phone;
public Student() {
}
public Student(Integer id, String name, Integer age, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
}
}
4.配置dao层
接口
public interface StudentDao {
List<Student> findAllStudent();
}
实现类
public class StudentDaoImpl implements StudentDao {
@Override
public List<Student> findAllStudent() {
List<Student> list =new ArrayList<>();
Student s1 =new Student(1,"小明",18,"13838383838");
Student s2 =new Student(2,"小红",18,"13838383838");
Student s3 =new Student(3,"小刚",18,"13838383838");
Student s4 =new Student(4,"小白",18,"13838383838");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
return list;
}
}
5.配置service
接口
public interface StudentService {
List<Student> findAllStudent();
}
实现类
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public List<Student> findAllStudent() {
return studentDao.findAllStudent();
}
}
6.配置controller
public class StudentController {
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void getAllStu(){
List<Student> allStudent = studentService.findAllStudent();
for (Student s:allStudent) {
System.out.println(s);
}
}
}
7.配置spring的配置文件配置bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentDao" class="top.chenyp.dao.impl.StudentDaoImpl"/>
<bean id="studentService" class="top.chenyp.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="studentController" class="top.chenyp.controller.StudentController">
<property name="studentService" ref="studentService"/>
</bean>
</beans>
8.通过junit的测试类测试
public class StuTest {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentController studentController = (StudentController) ac.getBean("studentController");
studentController.getAllStu();
}
}
9.运行效果
2.注解方式小案例
这个案例是在xml的基础上进行修改
1.使用配置类代替spring的xml配置
(1)删除applicationContext.xml文件
(2)创建config包,并在上面添加一个SpringConfig的类
@Configuration
@ComponentScan("top.chenyp")
public class SpringConfig {}
2.使用注解注入
dao中
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
service中
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Resource(name = "studentDao")
private StudentDao studentDao;
@Override
public List<Student> findAllStudent() {
return studentDao.findAllStudent();
}
}
controller中
@Controller
public class StudentController {
@Resource(name = "studentService")
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void getAllStu(){
List<Student> allStudent = studentService.findAllStudent();
for (Student s:allStudent) {
System.out.println(s);
}
}
}
3.使用spring-test整合junit
(1)加入maven坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.2</version>
</dependency>
(2)编写test类测试spring的配置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = top.chenyp.config.SpringConfig.class)
public class StuTest {
@Autowired
private StudentController studentController;
@Test
public void test(){
studentController.getAllStu();
}
}
4.运行结果