UserController.java
package com.aistart.controller;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import com.aistart.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
@Autowired
@Qualifier("userServiceImpl")
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String Login(Student student){
boolean login = userService.login(student);
if (login)return "yes";
else return "no";
}
}
UserMapperImpl.java
package com.aistart.mapper.impl;
import com.aistart.mapper.UserMapper;
import com.aistart.pojo.Student;
import org.springframework.stereotype.Repository;
@Repository
public class UserMapperImpl implements UserMapper {
@Override
public int selectUser(Student student) {
System.out.println("这里是查找是否有用户的mapper");
return 0;
}
}
School.java
package com.aistart.pojo;
public class School {
private String schoolName;
public School(String schoolName) {
this.schoolName = schoolName;
}
public School() {
System.out.println("school的无参被调用了");
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("School{");
sb.append("schoolName='").append(schoolName).append('\'');
sb.append('}');
return sb.toString();
}
}
Student.java
package com.aistart.pojo;
import org.springframework.stereotype.Component;
@Component
public class Student {
private String stuName;
private Integer age;
private School school;
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public Student() {
System.out.println("student无参");
}
public Student(String stuName, Integer age) {
System.out.println("全参数构造");
this.stuName = stuName;
this.age = age;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Student{");
sb.append("stuName='").append(stuName).append('\'');
sb.append(", age=").append(age);
sb.append(", school=").append(school);
sb.append('}');
return sb.toString();
}
}
User1ServiceImpl.java
package com.aistart.service.impl;
import com.aistart.mapper.UserMapper;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class User1ServiceImpl implements UserService {
@Resource
UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public boolean login(Student student) {
System.out.println("这是login的service业务");
int i = userMapper.selectUser(student);
if (i==1){
return true;
}
return false;
}
}
UserServiceImpl.java
package com.aistart.service.impl;
import com.aistart.mapper.UserMapper;
import com.aistart.mapper.impl.UserMapperImpl;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public boolean login(Student student) {
System.out.println("这是login的service业务");
int i = userMapper.selectUser(student);
if (i==1){
return true;
}
return false;
}
}
AnnotationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.aistart"></context:component-scan>
<context:annotation-config/>
</beans>
ApplicationContext.xml
<?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="alice" class="com.aistart.pojo.Student">
<constructor-arg index="0" value="Candy"/>
<constructor-arg index="1" value="19"/>
<property name="stuName" value="Alice"/>
<property name="age" value="19"/>
<property name="school" ref="ksdx"/>
</bean>
<bean id="sqxy" class="com.aistart.pojo.School">
<property name="schoolName" value="小清华"></property>
</bean>
<bean id="ksdx" class="com.aistart.pojo.School">
<property name="schoolName" value="呼呼睡觉"></property>
</bean>
<bean id="userController" class="com.aistart.controller.UserController">
<property name="userService" ref="userService"></property>
</bean>
<bean id="userService" class="com.aistart.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
<bean id="userMapper" class="com.aistart.mapper.impl.UserMapperImpl"/>
<bean id="newOne" class="com.aistart.pojo.Student" scope="prototype">
<constructor-arg name="age" value="19"></constructor-arg>
<constructor-arg name="stuName" value="candy"></constructor-arg>
</bean>
</beans>
UserControllerTest.java
package com.aistart.controller;
import com.aistart.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class UserControllerTest {
@Test
public void login() {
ApplicationContext context = new ClassPathXmlApplicationContext("AnnotationContext.xml");
UserController userController = (UserController) context.getBean("userController");
String login = userController.Login(new Student("小米",1));
System.out.println(login);
}
}
StudentTest.java
package com.aistart.pojo;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class StudentTest {
@Test
public void test(){
Student alice = new Student("Alice", 18);
Student student = new Student("小谬",1);
student.setStuName("Bob");
student.setAge(19);
}
@Test
public void beanTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student new1 =(Student) context.getBean("newOne");
Student new2 =(Student) context.getBean("newOne");
Student alice1 =(Student) context.getBean("alice");
System.out.println(new1==new2);
}
@Test
public void annoTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("AnnotationContext.xml");
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aistart</groupId>
<artifactId>Spring_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
</dependencies>
</project>