이론
제어역전 ( IoC, Inversion of Control )
스프링 컨테이너란?
POJO ( Plain Old Java Object ) 란?
프로젝스 세팅
프로젝트 생성
help > Install new software
#port
server.port=9090
#thymeleaf cache
spring.thymeleaf.cache=false
시작
public class Member {
private int id;
private String name;
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;
}
}
core2.repository 패키지 생성
core2.repository > MemberRepository.java 인터페이스 생성
public interface MemberRepository {
// 회원 저장
Member save(Member member);
// 전체 찾기
List<Member> findAll();
}
public class MemoryMemberRepository implements MemberRepository{
private static Map<Integer, Member> store = new HashMap<Integer, Member>();
private static int sequence = 0;
@Override
public Member save(Member member) {
member.setId(++sequence);
store.put(member.getId(), member);
return member;
}
@Override
public List<Member> findAll() {
return new ArrayList<Member>(store.values()); // 스토어 안에있는 전체 벨류 리턴
}
core2.service 패키지 생성
core2.service > MemberService.java 생성
public class MemberService {
MemberRepository memberRepository = new MemoryMemberRepository();
// 회원가입
public int join(Member member) {
memberRepository.save(member);
return member.getId();
}
// 전체회원 조회
public List<Member> findMembers(){
return memberRepository.findAll();
}
}
@Controller
public class MemberController {
MemberService mServive = new MemberService();
}
...
// MemberService mServive = new MemberService();
private final MemberService memberServive;
@Autowired
public MemberController(MemberService memberService) {
this.memberServive = memberService;
}
}
// -------------------------------------------------
// 필드 주입 ( Field Injection )
// - final 키워드를 사용 할 수 없어, 순환 참조가 발생 할 수 있다. 권장하지 않는다.
@Autowired
private MemberService memberServive;
// -------------------------------------------------
// -------------------------------------------------
// Setter Injection ( 수정자 주입 )
// - public으로 노출이 되기 때문에 다른곳에서 주입 가능하다.
private MemberService memberService;
@Autowired
public void setMember(MemberService memberService) {
this.memberService = memberService;
}
// -------------------------------------------------
@Service
public class MemberService {
// MemberRepository memberRepository = new MemoryMemberRepository();
private final MemberRepository memberRepository;
@Autowired
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
}
@Repository
public class MemoryMemberRepository implements MemberRepository{
...
Controller 패키지 > HomeController.java 생성
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "home";
}
}
resoureces>templates> home.html 생성
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<h1>Hello Spring</h1>
<p>회원 기능</p>
<p>
<a href="/members/new">회원가입</a>
<a href="/members">회원목록</a>
</p>
</div>
</body>
</html>
...
@GetMapping(value = "/members/new")
public String createForm() {
return "members/createMemberForm";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/members/new" method="post">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요.">
<button type="submit">등록</button>
</form>
</body>
</html>
public class MemberFormDTO {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@PostMapping(value = "/members/new")
public String create(MemberFormDTO form) {
Member member = new Member();
member.setName(form.getName());
memberServive.join(member);
//리다이렉트 방식으로 홈 화면으로 돌리기.
return "redirect:/";
//return "forward:/:;
}
...
@GetMapping("/members")
public String list(Model model) {
List<Member> members = memberServive.findMembers();
model.addAttribute("members", members);
return "members/memberList";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<table border="1">
<thead>
<tr>
<th>#</th>
<th>이름</th>
</tr>
</thead>
<tbody>
<tr th:each="member : ${members}" >
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
...
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation group: 'com.oracle.database.jdbc', name: 'ojdbc6', version: '11.2.0.4'
...
...
#encoding
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.enabled=true
#dbms
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.username=myspring
spring.datasource.password=myspring
//@Repository
public class MemoryMemberRepository implements MemberRepository{
...
@Repository
public class JdbcMemberRepository implements MemberRepository{
private final DataSource dataSource;
public JdbcMemberRepository(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public Member save(Member member) {
String sql = "INSERT INTO MEMBER VALUES (member_seq.nextval, ?)";
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
String generatedColumes[] = {"id"};
pstm = conn.prepareStatement(sql, generatedColumes);
pstm.setString(1, member.getName());
pstm.executeUpdate();
rs = pstm.getGeneratedKeys();
if (rs.next()) {
member.setId(rs.getInt(1));
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstm.close();
conn.close();
} catch( SQLException e) {
e.printStackTrace();
}
}
return member;
}
@Override
public List<Member> findAll() {
String sql = "select * from member";
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
List<Member> members = null;
try {
conn = dataSource.getConnection();
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
members = new ArrayList<Member>();
while(rs.next()) {
Member member = new Member();
member.setId(rs.getInt("id"));
member.setName(rs.getString("name"));
members.add(member);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstm.close();
conn.close();
} catch( SQLException e) {
e.printStackTrace();
}
}
return members;
}
}
...
//implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
...
...
#jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
@Entity
public class Member {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySequence")
@SequenceGenerator(name ="mySequence", sequenceName = "member_seq", allocationSize = 1)
private int id;
//@Repository
public class JdbcMemberRepository implements MemberRepository{
...
}
@Service
@Transactional
public class MemberService {
...
}
@Repository
public class JpaMemberRepository implements MemberRepository{
private final EntityManager em;
public JpaMemberRepository(EntityManager em) {
this.em = em;
}
@Override
public Member save(Member member) {
em.persist(member);
return member;
}
@Override
public List<Member> findAll() {
return em.createQuery("select m from Member m", Member.class).getResultList();
}
}
콘솔
insert into member (name, id) values (?, ?)
select
member0_.id as id1_0_ ,
member0_.name as name2_0_
from member member0_