[목차]

① DB 연동

② Controller

③ Aspect


[내용]

= sprweb22_aop_login

① DB 연동

- Configuration(xml)

<configuration>
 <typeAliases>
 	<typeAlias type="pack.model.JikwonDto" alias="dto"/>
 </typeAliases>
 <!--  DB 연결을 root-context.xml에서 하도록 수정.-->
 <mappers>
  <mapper resource="pack/mybatis/DataMapper.xml" />
 </mappers>
</configuration>

 

 

 - DataMapper(xml)

<mapper namespace="dev">

 <select id="selectJikwonAll" resultType="dto">
  select jikwon_no, jikwon_name, jikwon_jik, jikwon_gen, buser_name
  from jikwon left outer join buser
  on jikwon.buser_num = buser.buser_no
 </select>
 
 <select id="selectLoginData" resultType="dto" parameterType="string">
 	select jikwon_no, jikwon_name
 	from jikwon
 	where jikwon_no=#{no}
 </select>
 
</mapper>

 

 

- MyDataSource

public class MyDataSource extends DriverManagerDataSource {
	public MyDataSource() {
		setDriverClassName("org.mariadb.jdbc.Driver");
		setUrl("jdbc:mysql://127.0.0.1:3306/test");
		setUsername("root");
		setPassword("123");
	}
}

 

 

- root-context(xml)

	<bean id="dataSource" class="pack.model.MyDataSource"/>
    
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:pack/mybatis/Configuration.xml"/>
	</bean>

 

 

② Controller

- main(jsp)

<body>
	<h2>메인</h2>
	<a href="jikwonlist">직원 보기</a>
</body>

 

 

- JikwonController

@Controller
public class JikwonController {
	@Autowired
	//@Qualifier("JikwonImpl")
	private JikwonInter jikwonInter; 
	
	@RequestMapping("jikwonlist")
	public ModelAndView jikwonShow() {
		//모델과 통신
		List<JikwonDto> list = jikwonInter.jikwonList();
		ModelAndView view = new ModelAndView("jikwonlist");
		view.addObject("list", list);
		return view;
	}
}

 

 

- JikwonInter

public interface JikwonInter {
	List<JikwonDto> jikwonList();
	JikwonDto getLoginInfo(String bun);
}

 

 

- JikwonImpl

@Repository
public class JikwonImpl extends SqlSessionDaoSupport implements JikwonInter{

	@Autowired
	public JikwonImpl(SqlSessionFactory factory) {
		setSqlSessionFactory(factory);
	}
	
	@Override
	public List<JikwonDto> jikwonList() {
		return getSqlSession().selectList("selectJikwonAll");
	}

	@Override
	public JikwonDto getLoginInfo(String bun) {
		return getSqlSession().selectOne("selectLoginData",bun);
	}
}

 

 

 - JikwonDto

public class JikwonDto {
	private String jikwon_no, jikwon_name, jikwon_jik, jikwon_gen, buser_name;
	//getter/setter
}

 

 

- jikwonlist(jsp)

<body>
	<h2>직원자료</h2>
	<% 
	if(session.getAttribute("name") != null){
		out.print("<a href='logout'>로그아웃</a>");
	}
	%>
	<table border="1">
		<tr>
			<th>사번</th><th>직원명</th><th>부서명</th><th>직급</th><th>성별</th>
		</tr>
		<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
		<c:forEach var="j" items="${list}">
		<tr>
			<td>${j.jikwon_no}</td>
			<td>${j.jikwon_name}</td>
			<td>${j.buser_name}</td>
			<td>${j.jikwon_jik}</td>
			<td>${j.jikwon_gen}</td>
		</tr>		
		</c:forEach>
	</table>
</body>

 

 

③ Aspect

- LoginClass

public class LoginClass {
	public boolean loginCheck(HttpServletRequest request, HttpServletResponse response)
					throws Exception{
		HttpSession session = request.getSession();
		
		if(session.getAttribute("name") == null) {
			response.sendRedirect("login");
			return true;
		}else {
			return false;
		}
	}
}

 

 

- MyAdvice

@Aspect
@Component
public class MyAdvice {
	@Autowired
	private LoginClass loginClass;
	
	@Around("execution(* jikwonShow*(..))")
	public Object aopProcess(ProceedingJoinPoint joinPoint) throws Throwable{
		HttpServletRequest request = null;
		HttpServletResponse response = null;
		
		for(Object obj:joinPoint.getArgs()) {
			if(obj instanceof HttpServletRequest) {
				request = (HttpServletRequest)obj;
			}
			if(obj instanceof HttpServletResponse) {
				response = (HttpServletResponse)obj;
			}
		}
		
		if(loginClass.loginCheck(request, response)) {
			return null;
		}
		
		Object object = joinPoint.proceed();
		return object;
	}
}

 

 

- login(jsp)

<body>
	<h2>로그인</h2>
	<form action="login" method="post">
	직원번호 : <input type="text" name="no"><br>
	직원이름 : <input type="text" name="name"><br>
	<input type="submit" value="로그인">
	</form>
</body>

+ Recent posts