프로젝트 생성을 하고 내용을 이어서 진행하겠습니다.
전 글과 이어지는 내용이기 때문에 중복되는 내용을 피하기 위해
https://badstorage.tistory.com/10
구조 정리가 다 완료되었으면
상위에 있는 Contoller부터 살펴봅시다.
MainController.java
package com.example.demo.Controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.Services.memberService.memberImpl;
@Controller
public class MainController {
@Resource(name="ServiceMember")
private memberImpl aa;
@RequestMapping(value="/")
public String main(Model model) {
aa.execute(model);
return "index";
}
}
MainMapper.java
package com.example.demo.DAO;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.example.demo.DTO.MemberDTO;
@Repository("daoDB")
public interface MainMapper {
public List<MemberDTO> selectMember();
}
MemberDTO.java
package com.example.demo.DTO;
public class MemberDTO {
private String name;
private String id;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MemberImpl.java
package com.example.demo.Services.memberService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.example.demo.DAO.MainMapper;
import com.example.demo.DTO.MemberDTO;
@Service("ServiceMember")
public class memberImpl implements ServiceMember{
@Resource(name="daoDB")
private MainMapper mm;
@Override
public int execute(Model model) {
List<MemberDTO> testDto = mm.selectMember();
System.out.println("값 확인 : "+mm.selectMember());
model.addAttribute("test",testDto);
return 0;
}
}
ServiceMember.java
package com.example.demo.Services.memberService;
import org.springframework.ui.Model;
public interface ServiceMember {
public int execute(Model model);
}
RetryDemo1Application.java
package com.example.demo;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@SpringBootApplication
@MapperScan(value= {"com/example/demo/DAO"})
public class RetryDemo1Application {
public static void main(String[] args) {
SpringApplication.run(RetryDemo1Application.class, args);
}
@Bean
public SqlSessionFactory sqlSEssionFacoty(DataSource dataSource) throws Exception{
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
Resource[] res = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml");
sessionFactory.setMapperLocations(res);
return sessionFactory.getObject();
}
}
Application에서 @어노테이션을 사용하여 bean을 등록한다.
설명:
MainMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.DAO.MainMapper">
<select id="selectMember" parameterType="String" resultType="com.example.demo.DTO.MemberDTO">
select * from test
</select>
</mapper>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>PW</th>
<th>Name</th>
</tr>
<c:forEach items="${test}" var="test">
<tr>
<td>${test.id }</td>
<td>${test.password }</td>
<td>${test.name }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
이제 실행해 보자
@어노테이션에 대해서는 나중에 공부도 할 겸 포스팅할 예정이다.
도움이 된 블로그
https://gangnam-americano.tistory.com/63