JDK 이클립스에 Maria DB와 MyBatis를 연동하여
회원가입, 로그인, 로그아웃, 회원탈퇴, 회원목록 열람이 가능한 로그인 화면을 만들어 보았다.
<나의 개발환경>
tomcat 8.5 (tomcat 7.0로 진행할 시 다른 최신 버전들과 호환되지 않아 오류 발생(이것때문에 매우 고생하였다.))
자바 버전 1.8
마리아 db 10.4
mariadb-java-client 2.7.1 버전 (마리아 db와 MyBatis 그리고 이클립스를 연동하기 위해서는 JDBC를 설치해야 한다. )
프로젝트의 파일 위치들은 다음과 같다.
<pom.xml> (프로젝트 설정 파일)
<java-version></java-version>안의 버전이 1.6일 경우, 1.8로 변경
<org.springframework-version></org.springframework-version> 안의 자신의 버전 확인
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>test</artifactId>
<name>Login</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.2.7.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
|
cs |
<dependencies></dependencies> 안에 이와 같이 추가
<version></version>안의 버전은 자신의 이클립스 버전을 참고하여 작성
이렇게 pom.xml에 다음과 작성하기만 MAVEN이 사용자가 사용하기 원하는 프로그램을 자동으로 알아서 불러와준다.
(아주 편리한 기능이다.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.6.0</version>
</dependency>
|
cs |
<testMapper.xml>
DAO가 데이터베이스에 요구할 sql문을 작성
src/main/resources 안에 mappers 폴더를 만들고 폴더 안에 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<?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="BoardMapper">
<select id="login" resultType="BoardVO">
SELECT id, pw , name, phone
FROM board_by
WHERE id = #{id}
AND pw = #{pw}
</select>
<select id="viewMember" resultType="com.spring.test.domain.BoardVO">
SELECT id as id ,
name as name,
phone as phone ,
regdate as regdate
FROM board_by
WHERE id = #{id}
AND pw = #{pw}
</select>
<select id="getBoardList" resultType="BoardVO">
SELECT id,pw,name,phone,reg_date
FROM board_by
ORDER BY id DESC
</select>
<select id="getPersonalContent" resultType="BoardVO">
<![CDATA[
SELECT *
FROM board_by
WHERE id = #{id}
]]>
</select>
<select id="findIdBoard" resultType="BoardVO">
<![CDATA[
SELECT id as id, name as name
FROM board_by
WHERE name = #{name}
AND phone = #{phone};
]]>
</select>
<select id="findPwBoard" resultType="BoardVO">
<![CDATA[
SELECT pw as pw
FROM board_by
WHERE id = #{id}
AND name = #{name}
AND phone = #{phone};
]]>
</select>
<select id="insertBoard">
<![CDATA[
INSERT INTO board_by(id,pw,name,phone,reg_date)
VALUES(#{id}, #{pw}, #{name}, #{phone}, now())
]]>
</select>
<update id="updateBoard">
<![CDATA[
UPDATE board_by SET
pw = #{pw},
name = #{name},
phone = #{phone}
WHERE id = #{id}
]]>
</update>
<delete id="deleteBoard">
<![CDATA[
DELETE FROM board_by
WHERE id = #{id}
]]>
</delete>
</mapper>
|
cs |
<mybatis-config.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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>
<settings>
<!-- 자동으로 카멜케이스 규칙으로 변환 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.spring.test.domain"/>
</typeAliases>
</configuration>
|
cs |
<root-context.xml>
root-context.xml의 namespace에서 다음과 같이 체크
source에 Mapper.xml과 mybatis-config.xml의 위치를 다음과 같이 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<property name="url" value="jdbc:mariadb://마리아 db 주소:포트명/데이터베이스 이름" />
<property name="username" value="아이디" />
<property name="password" value="비밀번호" />
</bean>
<!-- mybatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml" />
<property name="mapperLocations" value="classpath:/mappers/**/*Mapper.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans>
|
cs |
<servlet-context.xml>
DispacherServlet 역활을 하는 파일
<context:component-scan base-package="com.spring.test.controller" />에 자신이 설정할 controller를 지정한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- 이 설정을 이용해 URL 매핑이 일어난다. annotation-driven에 의해 @RequestMapping을 사용할 수 있게 되고,
@RequestMapping에 지정된 URL로 브라우저의 URL이 매핑되게 된다. -->
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.spring.test.controller" />
<!-- component-scan은 @Controller, @Repository, @Service, @Component 어노테이션이 사용된
클래스를 자동으로 스캔하여 빈으로 등록한다. -->
<beans:bean name = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value = "org.mariadb.jdbc.Driver"/>
<beans:property name="url" value = "jdbc:mariadb://마리아 db 주소:포트번호/데이터베이스 명"/>
<beans:property name="username" value = "아이디"/>
<beans:property name="password" value = "비밀번호"/>
</beans:bean>
<context:component-scan base-package="com.spring.test">
<context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="regex"
expression="(service|controller|dao)\..*" />
</context:component-scan>
</beans:beans>
|
cs |
처음 작성해보는 블로그 글이라 부족한 점이 많다.
틀린 점이 있다면 댓글로 알려주세요.
'java spring' 카테고리의 다른 글
spring boot JWT 실습(1) (0) | 2021.07.31 |
---|---|
Spring boot Exception 처리 (0) | 2021.07.30 |
DAO, DTO, Service (0) | 2021.07.29 |
[Java Spring MVC] Maria DB와 MyBatis로 로그인 화면 만들기(3) (0) | 2021.02.06 |
[Java Spring MVC] Maria DB와 MyBatis로 로그인 화면 만들기(2) (1) | 2021.02.06 |