기록해! 정리해!

비즈니스 컴포넌트 실습 II 본문

Spring

비즈니스 컴포넌트 실습 II

zsuling 2022. 9. 22. 09:53

package com.springbook.biz.user;

import lombok.Data;

@Data
public class UserVo {
  private  String id;
  private  String password;
  private  String name;
  private  String role;
  
}

<?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 
	http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:component-scan base-package="com.springbook.biz" />

<!-- 

 1. id="userService" :  UserServiceClient 의 container.getBean("userService")에서 사용
 2. property name="userDao" 는 	private  UserDao userDao 와 일치
 3. ref="userDAO" 는 밑에 있는 빈의 이름을 참조한다. <bean id="userDAO" ... 

 -->
<bean id="userService"  class="com.springbook.biz.user.impl.UserServiceImpl">
  <property name="userDao" ref="userDAO" ></property>
</bean>

<bean id="userDAO"  class="com.springbook.biz.user.impl.UserDaoImpl" /> 
</beans>
package com.springbook.biz.user;

public interface UserDao {
	public UserVo getUser(UserVo vo);
}
--------------------------------------------------------------------------

​

package com.springbook.biz.user;

public interface UserService {
	public UserVo getUser(UserVo vo);
}
========================================================================

​

package com.springbook.biz.user.impl;

import java.sql.*;

import com.springbook.biz.common.JDBCUtil;
import com.springbook.biz.user.UserDao;
import com.springbook.biz.user.UserVo;

public class UserDaoImpl implements UserDao {
	
	private JDBCUtil dbConn =null;
	private Connection conn = null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;
	 
	UserDaoImpl(){
		dbConn=JDBCUtil.getInstance();		
	}
	
	@Override
	public UserVo getUser(UserVo vo) {
		 System.out.println(" ===>  getUser(UserVo vo) 기능처리 " );
		 UserVo user = null;
		try {
			 conn = dbConn.getConnection();	
			 String SQL="select *  from  users where id=? and password=?";
			 pstmt=conn.prepareStatement(SQL);
			 pstmt.setString(1, vo.getId());
			 pstmt.setString(2, vo.getPassword());
			 rs=pstmt.executeQuery();
			 rs.next();
			 
			 user=new UserVo();
			 user.setId(rs.getString("id"));
			 user.setName(rs.getString("name"));
			 user.setPassword(rs.getString("password"));
			 user.setRole(rs.getString("role"));
			 
		}catch(Exception e) {
			e.printStackTrace();
		}		
		
		return user;
	}

}
======================================================================

​

package com.springbook.biz.user.impl;

import com.springbook.biz.user.UserDao;
import com.springbook.biz.user.UserService;
import com.springbook.biz.user.UserVo;

public class UserServiceImpl  implements UserService  {

	private  UserDao userDao;
		
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public UserVo getUser(UserVo vo) {
		// TODO Auto-generated method stub
		return userDao.getUser(vo);
	}

}
----------------------------------------------------------------------------------------------------------------------

​

package com.springbook.biz.user;

import java.util.List;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class UserServiceClient {

	public static void main(String[] args) {
		AbstractApplicationContext container=
				new GenericXmlApplicationContext("applicationContext.xml");

		UserService service =(UserService) container.getBean("userService");
		UserVo vo =new UserVo();
		vo.setId("TEST");
		vo.setPassword("TEST123");
	
		UserVo  user=service.getUser(vo);
		
		if(user != null) {
			System.out.println(user.getName() + "님 환영합니다.");
			
		}else {
			System.out.println(" 로그인 실패 !!! ");
		}
		
		container.close();		
	}

}
​
Comments