项目建立
实现简单的验证登录
dao层(UserMapper)
package com.a2003.dao;
public interface UserMapper {
/*public boolean login(String name,String password);//方法一*/
public String login(String name,String password);//方法二
}
dao层(UserMapperImpl)
package com.a2003.dao;
public class UserMapperImpl implements UserMapper{
//方法一
@Override
public String login(String name, String password) {
if (name.equals("嫦娥")&&password.equals("123456")){
return "登录成功";
}else {
return "登录失败";
}
}
//方法二
/*@Override
public boolean login(String name, String password) {
if (name.equals("嫦娥")&&password.equals("123456")){
return true;
}else {
return false;
}
}*/
}
pojo层(User),构造方法
package com.a2003.pojo;
public class User {
private int id;
private String name;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
pojo层(User2)
package com.a2003.pojo;
public class User2 {
private int id;
private String name;
private String password;
@Override
public String toString() {
return "User2{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
service层(UserService)
package com.a2003.service;
public interface UserService {
/*public boolean login(String name,String password);*/
public String login(String name,String password);
}
service层(UserServiceImpl)
package com.a2003.service;
import com.a2003.dao.UserMapper;
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper){
this.userMapper = userMapper;
}
@Override
public String login(String name, String password) {
return userMapper.login(name, password);
}
/*@Override
public boolean login(String name, String password) {
return userMapper.login(name, password);
}*/
}
beans.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="user" class="com.a2003.pojo.User">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="李白"></constructor-arg>
<constructor-arg name="password" value="123456"></constructor-arg>
</bean>
<!--通过set方法实现依赖注入-->
<bean id="user2" class="com.a2003.pojo.User2">
<property name="id" value="2"></property>
<property name="name" value="change"></property>
<property name="password" value="123456"></property>
</bean>
<!--UserMapperImpl-->
<bean id="UserMapperImpl" class="com.a2003.dao.UserMapperImpl"></bean>
<!-- UserServiceImpl-->
<bean id="UserServiceImpl" class="com.a2003.service.UserServiceImpl">
<property name="userMapper" ref="UserMapperImpl"/>
</bean>
</beans>
Mytest
import com.a2003.dao.UserMapperImpl;
import com.a2003.pojo.User;
import com.a2003.pojo.User2;
import com.a2003.service.UserService;
import com.a2003.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
//通过构造方法实现依赖注入
User user = applicationContext.getBean("user",User.class);
System.out.println(user);
//通过set方法实现依赖注入
User2 user2 = applicationContext.getBean("user2",User2.class);
System.out.println(user2);
}
//方法一
/*@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = applicationContext.getBean("UserServiceImpl",UserService.class);
boolean result = userService.login("嫦娥","123456");
if (result == true){
System.out.println("登录成功!!!");
}else{
System.out.println("登录失败!!!");
}
}*/
//方法二
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = applicationContext.getBean("UserServiceImpl",UserService.class);
String result = userService.login("嫦娥","123456");
System.out.println(result);
}
@Test
public void test3() {
UserService userService = new UserServiceImpl();
((UserServiceImpl) userService).setUserMapper(new UserMapperImpl());
String result = userService.login("嫦娥", "123456");
System.out.println(result);
}
}
test测试成功
test2测试成功,红色不要管
test3测试成功