文章目录 UserController UserMapperImpl School.java Student.java UserServiceImpl.java ApplicationContext.xml UserControllerTest.java StudentTest.java pom.xml
UserController
package com. aistart. controller ; import com. aistart. pojo. Student ;
import com. aistart. service. UserService ;
import com. aistart. service. impl. UserServiceImpl ; public class UserController { 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
package com. aistart. mapper. impl ; import com. aistart. mapper. UserMapper ;
import com. aistart. pojo. Student ; 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 ; 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) { 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 ( ) ; }
}
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 ; public class UserServiceImpl implements UserService { 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 ; }
}
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" > < 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" /> </ 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 ( "ApplicationContext.xml" ) ; UserController userController = ( UserController ) context. getBean ( "userController" ) ; String login = userController. Login( new Student ( ) ) ; 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 ( ) ; student. setStuName ( "Bob" ) ; student. setAge ( 19 ) ; } @Test public void beanTest ( ) { ApplicationContext context = new ClassPathXmlApplicationContext ( "ApplicationContext.xml" ) ; Student alice = ( Student ) context. getBean ( "alice" ) ; Student alice1 = ( Student ) context. getBean ( "alice" ) ; System . out. println ( alice1== alice) ; System . out. println ( alice) ; }
}
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>