Spring Tool Suite 4
application.properties
# port
server.port=3000
# DB
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=hr
spring.datasource.password=hr
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sampleBootProg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sampleBootProg</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- 자바용 json 라이브러리(XML/YAML/CSV) data-processing 툴 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
<!-- ojdbc6 위의 경로(Repository)를 추가해 줄것-->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SampleBootProgApplication.java
package bit.com.a;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "bit.com.a")
public class SampleBootProgApplication {
public static void main(String[] args) {
SpringApplication.run(SampleBootProgApplication.class, args);
}
}
WebConfig.java
package bit.com.a;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:8090");
}
}
DatabaseConfig.java
package bit.com.a;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
public class DatabaseConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource ds)throws Exception {
System.out.println("DatabaseConfig sqlSessionFactory()");
SqlSessionFactoryBean sqlSFB = new SqlSessionFactoryBean();
sqlSFB.setDataSource(ds);
Resource[] arrRes = new PathMatchingResourcePatternResolver().getResources("classpath:sqls/*.xml");
sqlSFB.setMapperLocations(arrRes);
sqlSFB.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return (SqlSessionFactory)sqlSFB.getObject();
}
@Bean
public SqlSessionTemplate sqlSession(SqlSessionFactory sqlSF) {
return new SqlSessionTemplate(sqlSF);
}
}
MemberController.java
package bit.com.a.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import bit.com.a.dto.MemberDto;
import bit.com.a.service.MemberService;
@RestController // Controller + responseBody
public class MemberController {
@Autowired
MemberService service;
@RequestMapping(value = "/getId", method = RequestMethod.POST)
public String getId(String id) {
System.out.println("MemberController getId");
int count = service.getId(id);
if(count > 0) {
return "NO";
}else {
return "YES";
}
}
@RequestMapping(value = "/account", method = RequestMethod.POST)
public String account(MemberDto dto) {
System.out.println("MemberController account");
boolean b = service.addmember(dto);
if(b) {
return "YES";
}
return "NO";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public MemberDto login(String id, String pwd) {
System.out.println("MemberController login");
MemberDto dto = service.login(new MemberDto(id, pwd, null, null, 0));
return dto;
}
}
MemberDao.java
package bit.com.a.dao;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import bit.com.a.dto.MemberDto;
@Mapper
@Repository
public interface MemberDao {
public int getId(String id);
public int addmember(MemberDto dto);
public MemberDto login(MemberDto dto);
}
MemberDto.java
package bit.com.a.dto;
import java.io.Serializable;
public class MemberDto implements Serializable {
private String id;
private String pwd;
private String name;
private String email;
private int auth;
public MemberDto() {
}
public MemberDto(String id, String pwd, String name, String email, int auth) {
super();
this.id = id;
this.pwd = pwd;
this.name = name;
this.email = email;
this.auth = auth;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAuth() {
return auth;
}
public void setAuth(int auth) {
this.auth = auth;
}
@Override
public String toString() {
return "MemberDto [id=" + id + ", pwd=" + pwd + ", name=" + name + ", email=" + email + ", auth=" + auth + "]";
}
}
MemberService.java
package bit.com.a.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import bit.com.a.dao.MemberDao;
import bit.com.a.dto.MemberDto;
@Service
@Transactional
public class MemberService {
@Autowired
MemberDao dao;
public int getId(String id) {
return dao.getId(id);
}
public boolean addmember(MemberDto dto) {
int len = dao.addmember(dto);
return len>0?true:false;
}
public MemberDto login(MemberDto dto) {
MemberDto mem = dao.login(dto);
return mem;
}
}
Member.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="bit.com.a.dao.MemberDao">
<select id="getId" parameterType="java.lang.String"
resultType="java.lang.Integer">
SELECT NVL(COUNT(*), 0)
FROM MEMBER
WHERE ID=#{id}
</select>
<insert id="addmember" parameterType="bit.com.a.dto.MemberDto">
INSERT INTO MEMBER(ID, PWD, NAME, EMAIL, AUTH)
VALUES(#{id}, #{pwd}, #{name}, #{email}, 3)
</insert>
<select id="login" parameterType="bit.com.a.dto.MemberDto"
resultType="bit.com.a.dto.MemberDto">
SELECT ID, NAME, EMAIL, AUTH
FROM MEMBER
WHERE ID=#{id} AND PWD=#{pwd}
</select>
</mapper>
Eclipse
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
location.href = "login.html";
</script>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
#app{
margin: auto;
margin-top: 60px;
width: 60%;
border: 3px solid #8ac007;
padding: 10px;
}
</style>
</head>
<body>
<h1>login</h1>
<div id="app">
<table border="1">
<tr>
<td>ID</td>
<td>
<input type="text" id="id" placeholder="아이디입력" size="20">
</td>
</tr>
<tr>
<td>PW</td>
<td>
<input type="text" id="pwd" placeholder="패스워드입력" size="20">
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" id="login">login</button>
<button type="button" id="account">account</button>
</td>
</tr>
</table>
</div>
<script type="text/javascript">
$("#login").click(function(){
});
$("#account").click(function(){
location.href = "account.html";
});
</script>
</body>
</html>
account.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
#app{
margin: auto;
margin-top: 60px;
width: 60%;
border: 3px solid #8ac007;
padding: 10px;
}
</style>
</head>
<body>
<h1>account</h1>
<div id="app">
<form id="frm">
<table border="1">
<tr>
<td>ID</td>
<td>
<input type="text" id="id" name="id" placeholder="아이디입력" size="20">
<p id="idcheck"></p>
<button type="button" id="idcheckbtn">중복확인</button>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="text" id="pwd" name="pwd" placeholder="패스워드입력" size="20">
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" id="name" name="name" placeholder="이름입력" size="20">
</td>
</tr>
<tr>
<td>email</td>
<td>
<input type="text" id="email" name="email" placeholder="이메일입력" size="20">
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" id="account">회원가입</button>
</td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
$("#idcheckbtn").click(function(){
// alert('click');
$.ajax({
url:"http://localhost:3000/getId",
type:"post",
data:{ "id":$("#id").val() },
success:function( data ){
// alert('success');
if(data.trim() == "YES"){
$("#idcheck").html("사용할 수 있는 아이디입니다");
}else{
$("#idcheck").html("사용중인 아이디입니다");
$("#id").val("");
}
},
error:function(){
alert('error');
}
});
});
$("#account").click(function(){
var params = $("#frm").serialize();
$.ajax({
url:"http://localhost:3000/account",
type:"post",
// data:{ id:$("#id").val()... }
data:params,
success:function( data ){
alert("success");
if(data.trim() == "YES"){
alert("회원가입에 성공했습니다");
location.href = "login.html";
}else{
alert("회원가입되지 않았습니다");
}
},
error:function(){
alert("error");
}
});
});
</script>
</body>
</html>
'Spring Boot' 카테고리의 다른 글
파일 업로드 다운로드 (0) | 2020.09.28 |
---|---|
페이징! (0) | 2020.09.28 |