본문 바로가기

JAVA HTML JAVASCRIPT/소오스

spring - Maven 을 이용한 프로젝트 생성 및 활용(2)

728x90
반응형

MemberMapper.xml
mybatis를 이용한 것인데 이것은 쿼리를 따로 저장하여 필요한 것들을 사용할 수 있고, 
자동으로 jdbc로 변환을 해주는 기능이 있어 더 간편하게 DB처리를 할 수가 있다.

<?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.test.jjdev.service.MemberMapper"> <insert id="insertMember" parameterType="com.test.jjdev.service.Member"> INSERT INTO member(id,pw,name,age,gender) VALUES(#{id},#{pw},#{name},#{age},#{gender}) </insert> <delete id="deleteMember" parameterType="com.test.jjdev.service.Member"> DELETE FROM member WHERE id=#{id} </delete> <update id="updateMember" parameterType="com.test.jjdev.service.Member"> UPDATE member SET pw=#{pw},name=#{name},age=#{age},gender=#{gender} WHERE id=#{id} </update> <select id="selectMember" resultType="com.test.jjdev.service.Member"> SELECT id,pw,name,age,gender FROM member </select> </mapper>

MemberDao.java
mybatis의 SqlSessionTemplate을 사용하여 mapper.xml에 있는 쿼리들을 가져다가 사용

package com.test.jjdev.service; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class MemberDao { private final String NAMESPACE = "com.test.jjdev.service.MemberMapper."; @Autowired private SqlSessionTemplate sessionTemplate; //추가 public int insertMember(Member member) { return sessionTemplate.insert(NAMESPACE+"insertMember",member); } //삭제 public int deleteMember(String id) { return sessionTemplate.delete(NAMESPACE+"deleteMember",id); } //수정 public int updateMember(Member member) { return sessionTemplate.update(NAMESPACE+"updateMember",member); } //전체조회 public List<Member> selectList(){ return sessionTemplate.selectList(NAMESPACE+"selectMember"); } }

MemberService.java

package com.test.jjdev.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class MemberService { @Autowired private MemberDao memberDao; //추가 public int addMember(Member member) { return memberDao.insertMember(member); } //삭제 public int removeMember(String[] ck) { int result = 0; for(String id : ck) { memberDao.deleteMember(id); result++; } return result; } //수정 public int modifyMember(Member member) { return memberDao.updateMember(member); } //전체조회 public List<Member> getMember(){ return memberDao.selectList(); } }

MemberRestController.java

package com.test.jjdev.rest; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.test.jjdev.service.Member; import com.test.jjdev.service.MemberService; @RestController @CrossOrigin public class MemberRestController { @Autowired private MemberService memberService; //전체조회요청 @RequestMapping(value="/rest/MemberList", method = RequestMethod.GET) public List<Member> memberList(){ List<Member> list = memberService.getMember(); return list; } //수정요청 @RequestMapping(value="/rest/MemberModify", method= RequestMethod.POST) public Member memberModifyById(Member member) { int check = memberService.modifyMember(member); //dao처리가 됬는지 안됫는지에 따라 ajax에서 success와 error의 판별을 하기 위해 사용 if( 0!= check) return member; return null; } //삭제요청 @RequestMapping(value="/rest/MemberRemove", method=RequestMethod.GET) public String[] memberRemover(@RequestParam(value="ck[]", required=true) String[] ck) { int check = memberService.removeMember(ck); //dao처리가 됬는지 안됫는지에 따라 ajax에서 success와 error의 판별을 하기 위해 사용 if( 0!=check ) return ck; return null; } //추가요청 @RequestMapping(value="/rest/MemberAdd", method=RequestMethod.POST) public Member memberAdd(Member member) { System.out.println("추가요청 성공"); int check = memberService.addMember(member); //dao처리가 됬는지 안됫는지에 따라 ajax에서 success와 error의 판별을 하기 위해 사용 if( 0!=check) return member; return null; } }

[출처] spring - Maven 을 이용한 프로젝트 생성 및 활용(2)|작성자 upbreak05

 

728x90
반응형