wasup
Spring Legacy MiNi Project - STS / member java 본문
반응형
1. 툴 다운로드
sts : ( https://spring.io/tools )
2. 미니 프로젝트
2-1. Spring Legacy MiNi Project
2-2. Spring Boot MiNi Project
2-3 eGov
Project Name : slp(spring legacy project) 회원관리, 게시판
개발환경
- window10
- tomcat8.5
- mysql 5.5
- java version 1.8.0_40
- sts3
- jdbc 3.2.3
- myBatis 3.2.8
개발언어
- java, spring, jsp
- jsjq
- html5, css3
STS3(Spring Tool Suite3)-spring legacy project
: 이전부터 사용되어온 주 시스템(core, bases)
: Spring Legacy Project는 STS3까지 지원
: STS4부터는 Spring Boot가 기본
: STS4에서 Spring Legacy Project를 사용하기 위해서는 지원 플러그인을 따로 설치해서 사용
참고 링크 - ( https://woopi1087.tistory.com/26 )
참고 링크 - ( https://developsd.tistory.com/68 )
SQL
create database community;
use community;
drop table member;
create table member(
num int not null primary key auto_increment,
id varchar(20) not null,
pw varchar(15) not null,
name varchar(30) not null,
email varchar(30) not null,
tel varchar(14) not null,
addr_code varchar(10) not null,
addr varchar(50) not null,
addr_detail varchar(30) not null,
reg_date date not null
);
desc member;
-- id 아이디
-- pw 비밀번호
-- name 성명
-- email 이메일
-- tel 전화번호
-- addr_code 우편번호
-- addr 주소
-- addr_detail 상세주소
-- reg_date 가입일
--------------------------------
drop table board;
create table board(
num int not null primary key auto_increment,
writer varchar(30) not null,
subject varchar(40) not null,
content text not null,
pw varchar(15) not null,
reg_date datetime,
read_count int default 0,
re_group int not null,
re_step int not null,
re_depth int not null,
ip varchar(20) not null
);
desc board;
-- num 번호
-- writer 작성자
-- subject 제목
-- content 내용
-- pw 비밀번호
-- reg_date 작성일
-- hit 조회수
-- re_group 글 그룹(숫자)
-- re_step 글 순서(숫자)
-- re_depth 글 깊이
-- ip 아이피
cmd창에서 db확인
>cd\
>cd mysql5\bin
>mysql -u root -p
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 103
Server version: 5.5.62 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use community
Database changed
mysql> desc member;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| num | int(11) | NO | PRI | NULL | auto_increment |
| id | varchar(20) | NO | | NULL | |
| pw | varchar(15) | NO | | NULL | |
| name | varchar(30) | NO | | NULL | |
| email | varchar(30) | NO | | NULL | |
| tel | varchar(14) | NO | | NULL | |
| addr_code | varchar(10) | NO | | NULL | |
| addr | varchar(50) | NO | | NULL | |
| addr_detail | varchar(30) | NO | | NULL | |
| reg_date | date | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
기본 구조
pom.xml 추가
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<!--spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
org.springframework.web.servlet.DispatcherServlet
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://JAVA.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 한글처리 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
추가한내용
<!-- 한글처리 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/community"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean >
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value= "classpath:/mybatis/mybatis-config.xml">
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
src/main/resources/mybatis/mybatis-config.xml
mybatis-config.xml : DAO비즈니스 로직
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<typeAlias alias="memberDomain" type="com.mvc.pj.domain.MemberDTO"/>
<typeAlias alias="boardBasic" type="com.mvc.pj.domain.BoardDTO"/>
</typeAliases>
<mappers>
<mapper resource="/mybatis/member.xml"/>
</mappers>
</configuration>
src/main/resources/mybatis/member.xml
member.xml : DAO비즈니스 로직
<?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">
src/main/java/com.mvc.pj.domain.MemerDTO.java : DTO, VO : 모델 빈
package com.mvc.pj.domain;
import java.util.Date;
//모델빈
public class MemberDTO {
//전역변수:필드:프로퍼티:property
private int num;
private String id;
private String pw;
private String name;
private String email;
private String tel;
private String addr_code;
private String addr;
private String addr_detail;
private Date reg_date;
public MemberDTO() {}
}
src/main/resources/mybatis/member.xml 추가작성
: DAO 기능
<?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="member">
<!-- id중복체크, 내정보 수정 -->
<select id="selectOne" parameterType="String"
resultType="memberDomain">
select * from member
where id=#{id}
</select>
<!-- 회원가입 -->
<insert id="insertMember"
parameterType="com.mvc.pj.domain.MemberDTO">
insert into member(id,pw,name, email,tel,
addr_code,addr,addr_detail,reg_date)
values(#{id},#{pw},#{name},#{email},#{tel},#{addr_code},#{addr},#{addr_detail},NOW())
</insert>
<!-- 로그인,map사용 -->
<select id="selectLogin" parameterType="map"
resultType="com.mvc.pj.domain.MemberDTO">
select * from member
where id=#{id} and pw=#{pw}
</select>
<!-- 내 정보 수정 -->
<update id="memberUpdate"
parameterType="com.mvc.pj.domain.MemberDTO">
update member
set
pw=#{pw},name=#{name},email=#{email},tel=#{tel},addr_code=#{addr_code},
addr=#{addr},addr_detail=#{addr_detail}
where id=#{id}
</update>
<!-- 탈퇴 -->
<delete id="memberDelete" parameterType="String">
delete from member
where id=#{id}
</delete>
</mapper>
*memberDomain= mybatis-config.xml에서 지정한 별칭
*memberDomain로 사용해도되고 com.mvc.pj.domain.MemberDTO으로 사용해도 됨.
*본래는 가입날짜, 수정날짜, 접속날짜 등도 표시가 가능해야함.
com.mvc.pj.controller.MemberController.java
MemberController 틀
@Controller
public class MemberController {
@Autowired
private SqlSession sqlSession; // 클래스는 대문자 변수는 소문자 시작
@RequestMapping("main")
public String mm() {
return "main";// view return // views/main.jsp
}
// 회원가입FORM
// 아이디 중복체크
// 회원가입
// Login Form
// LogOut
// 내 정보 수정 폼
// 내 정보 수정 DB처리
//회원탈퇴
}// class-end
회원가입 폼
// 회원가입FORM
@RequestMapping("insertForm")
public String insertForm() {
return "/member/insertForm";// views/member/insertForm.jsp
}
아이디 중복체크
// 아이디 중복체크
@RequestMapping(value = "confirmID", method = RequestMethod.POST)
public String idCheck(String id, Model model) {
int check = -1;
MemberDTO memberDTO = sqlSession.selectOne("member.selectOne", id);
if (memberDTO == null) {
check = 1;// 사용가능한 아이디
} else {
check = -1;// 사용중인 아이디
}
model.addAttribute("check", check);
return "member/confirmID";// view return confirmID.jsp
}
회원가입
// 회원가입
@RequestMapping(value = "insertPro", method = RequestMethod.POST)
public String insertPro(@ModelAttribute("memberDTO") MemberDTO memberDTO, HttpServletRequest request)
throws IOException, NamingException {
// email받기
String email1 = request.getParameter("email1");
String email2 = request.getParameter("email2");
String email = email1 + email2;// 합치기
memberDTO.setEmail(email);// set
// tel받기
String tel1 = request.getParameter("tel1");
String tel2 = request.getParameter("tel2");
String tel3 = request.getParameter("tel3");
String tel = tel1 + tel2 + tel3;// 합치기
memberDTO.setTel(tel);// set
// sql
sqlSession.insert("member.insertMember", memberDTO); // namespace.id, DTO Parameter
return "main";
}
로그인 폼
// Login Form
@RequestMapping("loginForm")
public String loginForm() {
return "member/loginForm";// views/member/loginForm.jsp : View return
}
로그인 처리
// Login 처리 로직
@RequestMapping(value = "loginPro", method = RequestMethod.POST)
public String loginPro(String id, String pw, Model model) {
// Map, HashMap 사동가능
HashMap<String, String> map = new HashMap<String, String>();// 객체생성
map.put("id", id);
map.put("pw", pw);
MemberDTO mdto = sqlSession.selectOne("member.selectLogin", map);
if (mdto == null) {// 로그인 실패시
model.addAttribute("msg", "로그인 실패");
return "member/loginForm";// loginForm.jsp View return
}
// 로그인 성공시
model.addAttribute("mdto", mdto);
return "member/loginSucess";// loginSucess.jsp View return
}
로그아웃
// LogOut
@RequestMapping("logOut")
public String logOut() {
return "member/logOut";// logOut.jsp View return
}
내 정보 수정 폼
// 내 정보 수정 폼
@RequestMapping(value = "memUpdateForm", method = RequestMethod.POST)
public String updateForm(String id, Model model) {
MemberDTO mdto = sqlSession.selectOne("member.selectOne", id);
// email 나누기
// ex)emailaddr@naver.com
String email = mdto.getEmail();// email가져와서
int idx = email.indexOf("@");// @위치를 찾아서
String email1 = email.substring(0, idx);// 처음부터 idx까지(emailaddr)
String email2 = email.substring(idx);// idx부터 끝까지(@naver.com)
model.addAttribute("email1", email1);
model.addAttribute("email2", email2);
// tel 나누기
// ex)01012345678
String tel = mdto.getTel();// tel가져와서
String tel1 = tel.substring(0, 3);// 010(3 직전까지)
String tel2 = tel.substring(3, 7);// 1234(7 직전까지)
String tel3 = tel.substring(7);// 5678(7부터 끝까지)
model.addAttribute("tel1", tel1);
model.addAttribute("tel2", tel2);
model.addAttribute("tel3", tel3);
model.addAttribute("mdto", mdto);
return "member/updateForm"; // updateForm.jsp View return
}
내 정보 수정 처리
// 내 정보 수정 DB처리
@RequestMapping(value = "memIpdatePro", method = RequestMethod.POST)
public String memIpdatePro(@ModelAttribute("memberDTO") MemberDTO memberDTO, HttpServletRequest request)
throws IOException, NamingException {
//email1, email2 를 email로 합치기
String email1 = request.getParameter("email1");
String email2 = request.getParameter("email2");
String email = email1 + email2;//합치기
memberDTO.setEmail(email);//set
//tel들을 받아서 하나로 합치기
String tel1 = request.getParameter("tel1");
String tel2 = request.getParameter("tel2");
String tel3 = request.getParameter("tel3");
String tel = tel1+tel2+tel3;//합치기
memberDTO.setTel(tel);//set
sqlSession.update("member.memberUpdate", memberDTO);
return "main"; // main.jsp View return
}
회원 탈퇴
//회원탈퇴
@RequestMapping("deleteMember")
public String deleteMember(String id) {
sqlSession.delete("member.memberDelete", id);
return "main";//main.jsp View return
}
view -> 바로가기
반응형
'IT > Java' 카테고리의 다른 글
Spring) JDBC / jdbcTemplate / JPA / spring data JPA (0) | 2021.08.08 |
---|---|
JUnit) 단위테스트와 통합테스트 (0) | 2021.08.07 |
Spring) Note - 개념, 관계, 테스트케이스 JUnit (0) | 2021.07.31 |
Spring) 인텔리제이(IntelliJ)로 스프링 시작해보기 (0) | 2021.07.28 |
JUnit) unit test!!! (0) | 2021.07.08 |
Comments