https://kuzuro.blogspot.com
https://all-record.tistory.com
▲ 참조 블로그
프로젝트 생성 후 프로젝트 우클릭 Propertis →
Project Facets 에서 Java 1.8 변경합니다
다음으로 한글화 작업을 해주겠습니다.
src/main/WEB-INF/web.xml 에 들어가서 코드를 넣어주세요.
<!-- 문자 인코딩 시작 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 문자 인코딩 끝 -->
pom.xml 진입 후에 자바버전 변경, 스프링프레임워크 버전을 변경합니다
pom.xml에서 라이브러리를 설정합니다.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- MyBatis 3.4.1 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- MyBatis-Spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring-test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Mybatis log -->
<!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
▲ pom.xml 코드
pom.xml 설정이 끝이 나면 root-context.xml 로 이동한다
Namespaces 항목을 클릭한다
▲ 체크하기
DataSource는 MySQL과 연결을 담당한다. DataSource는 JDBC 커넥션을 처리하는 기능을 가지고 있기에 DB와 연동하는 작업에 반드시 필요하다.
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- MySQL dataSource -->
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://데이터베이스 주소:포트번호/스키마이름?useSSL=false&serverTimezone=UTC">
</property>
<property name="username" value="MySQL 계정"></property>
<property name="password" value="비밀번호"></property>
</bean>
-->
<!-- 자신의 PC(로컬)에 MySql을 설치했을 경우 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/스키마이름?useSSL=false&serverTimezone=UTC">
</property>
<property name="username" value="아이디"></property>
<property name="password" value="비밀번호"></property>
</bean>
<!-- mybatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<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" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
[출처]
https://all-record.tistory.com/175 [세상의 모든 기록]
https://kuzuro.blogspot.com/2019/08/3.html
연결 주소에는 로컬인 경우에는 127.0.0.1을 입력하고 뒤에 3306을 입력한다. 3306은 MySQL 설치시 지정한 포트인데, 변경하지 않았으면 3306이 기본이다.
root-context.xml 까지 작업을 완료하면 src/test/java 에다가 MySQLConnectionTest 와 MyBatisTest 클래스를 생성한다.
* MyBatisTest
package jae.won.package;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class MyBatisTest
{
@Inject
private SqlSessionFactory sqlFactory;
@Test
public void testFactory(){
System.out.println("\n >>>>>>>>>> sqlFactory 출력 : "+sqlFactory);
}
@Test
public void testSession() throws Exception{
try(SqlSession session = sqlFactory.openSession()){
System.out.println(" >>>>>>>>>> session 출력 : "+session+"\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
* MySQLConnection
package jae.won.package;
import java.sql.Connection;
import javax.inject.Inject;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/root-context.xml" })
public class MySQLConnectionTest {
@Inject
private DataSource ds;
@Test
public void testConnection() throws Exception {
try (Connection con = ds.getConnection()) {
System.out.println("\n >>>>>>>>>> Connection 출력 : " + con + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
다음으로 src/main/resources 에 mybatis-config.xml 을 생성합니다.
<?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>
</configuration>
다음 mapper를 추가합니다.
src/main/resources 에 mappers 폴더 생성 → ㅇㅇㅇMapper.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="namespace 입력">
</mapper>
junit 실행
콘솔창에 이렇게 나오면 성공입니다. 하지만!!! 저의 경우에는 에러가 나타났었습니다.
Unable to load authentication plugin 'caching_sha2_password
바로 이 에러입니다. 만약에 에러가 생겼을 경우 해결법을 링크에 걸어두겠습니다 → 에러해결
▼ 이제 실행해서 home 화면이 제대로 출력이 되면 성공입니다.
에러 링크 확인 부탁드립니다.