우선적으로 데이터 베이스랑 연동 하기 위해서는 root-context.xml을 수정 하셔야 합니다.
위치는 src - main - webapp - WEB-INF - spring - root-context 입니다.
위치는 이렇게 되어 있습니다.
일단 beans 중간에 데이터베이슬 연동 하는 bean을 생성 해주셔야 합니다.
하나씩 설명을 하면서 코드도 붙여 놓겠습니다.
<?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">
<!-- 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://127.0.0.1:3306/lotto_tb" />
<!-- 데이터베이스 유저명 -->
<property name="username" value="root" />
<!-- 데이터베이스 패스워드 -->
<property name="password" value="루트비밀번호" />
</bean>
</beans>
이렇게 생성 해주시고 맵퍼들도 등록해 주셔야 합니다.
이렇게 등록 해주시고 일단은 데이터 베이스 생성 및 컬럼들을 만들겠습니다.
lotto.sql를 다운 받으시고 진행해 주시면 됩니다.
해당 파일을 다운받으신다음에
HeidSQL를 실행해주신다음에 ctrl + O 을 누르셔서 SQL 파일 불러오기를 해주세요.
다운받은 파일을 불러오시면
쿼리창에 sql문이 나오게 됩니다.
여기서 F9키를 눌러주셔도 되고 노란색으로 표시되어 있는 실행 버튼을 클릭해 주시면 데이터 베이스 및 테이블 들이 생성 됩니다.
일단 데이터 베이스를 생성 하셨으니 위에서 수정 하였던 root-context 를 마저 수정 하셔야 합니다.
<?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">
<!-- 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://127.0.0.1:3306/lotto_tb" />
<!-- 데이터베이스 유저명 -->
<property name="username" value="root" />
<!-- 데이터베이스 패스워드 -->
<property name="password" value="루트비밀번호" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml" />
<property name="mapperLocations" >
<list>
<!--메인 맵퍼 -->
<value>classpath:/mapper/main/*Mapper.xml</value>
<!-- 멤버 맵퍼 -->
<value>classpath:/mapper/member/*Mapper.xml</value>
<!-- 인증게시판 -->
<value>classpath:/mapper/board/*Mapper.xml</value>
<!--자유게시퍼판 -->
<value>classpath:/mapper/freeboard/*Mapper.xml</value>
<!-- noticeboard -->
<value>classpath:/mapper/noticeboard/*Mapper.xml</value>
<!-- 어드민 -->
<value>classpath:/mapper/admin/*Mapper.xml</value>
<!-- 대댓글 연습 -->
</list>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
최종적으로 root-context.xml 파일 입니다.
여기 까지 진행 하셨으면 맵퍼 파일들도 만들어 주셔야 합니다.
resources 폴더로 가셔서 mapper 라는 폴더를 생성 해주시고 root-context.xml에서 설정한 게시판들을 만들어 주셔야 합니다.
폴더 생성 하시고 root-context에 설정 대로 만들어 주시면 됩니다.
기본 맵퍼 파일 하나를 올려 드리겠습니다.
<?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">
</mapper>
맵퍼.xml의 기본 틀 입니다.
namespace에 이름만 다르게 지정해 주시면 됩니다.
여기까지 지정해 주셨으면 프로젝트를 실행해 보겠습니다.
프로젝트에서 마우스 오른쪽 Run AS - Run on Server 을 실행 해주세요
그럼 아주 멋진 일이 벌어 집니다.
무슨 일이냐고요?
바로 오류 나거든요.
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [mybatis-config.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:741)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4699)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5165)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: class path resource [mybatis-config.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:406)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706)
... 21 more
라는 오류 입니다. mybatis가 위치 없어요 . 찾을수가 없어요
라는 문구 입니다.
만들어 주시면 됩니다.
src/main/resources
밑에 mybatis-config.xml 파일을 하나 만들어 주시면 됩니다.
이것도 일일히 설명하기엔 따로 포스팅을 해야 하기 때문에 파일을 올려 드리겠습니다.
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>
<typeAliases>
</typeAliases>
</configuration>
차후에 내용은 입력할 예정 입니다.
이제 다시 프로젝트에서 마우스 오른쪽 Run AS - Run on Server 을 실행 해주세요.
잘 실행 됩니다.