📋 전자정부프레임워크 4.1.0 MySQL + MyBatis 연동하기 📋
최종 디렉토리 구조
- RED : 새로 생성한 파일
- BLUE : 기존 파일 위치 변경 <기존위치 \src\main\webapp\WEB-INF\jsp\egovframework\example>
dispatcher-servlet.xml
<!-- VIEW 경로 변경 -->
<!-- 위치 46행~ -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:order="1"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/" p:suffix=".jsp"/>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<jsp:forward page="/main.do"/>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main</title>
</head>
<body>
<h1>test main</h1>
<h2>전체회원 수 ${ num }</h2>
</body>
</html>
pom.xml
<dependencies>
<!-- 기존 라이브러리 ··· -->
<!-- MySQL 추가 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
context-datasource.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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
<!-- Mysql (POM에서 commons-dbcp, mysql-connector-java 관련 라이브러리 설정 ) -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/데이터베이스명" />
<property name="username" value="사용자"/>
<property name="password" value="암호"/>
</bean>
</beans>
context-mapper.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 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- SqlSession setup for MyBatis Database Layer -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/egovframework/sqlmap/example/sql-mapper-config.xml" />
<property name="mapperLocations" value="classpath:/egovframework/sqlmap/example/mappers/*.xml" />
</bean>
<!-- MapperConfigurer setup for MyBatis Database Layer with @Mapper("deptMapper") in DeptMapper Interface -->
<bean class="org.egovframe.rte.psl.dataaccess.mapper.MapperConfigurer">
<property name="basePackage" value="egovframework.example.sample.service.impl" />
</bean>
</beans>
MemberVO.java <테스트 시 미입력>
package egovframework.example.sample.service;
public class MemberVO {}
sql-mapper-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>
<typeAliases>
<typeAlias alias="egovMap" type="org.egovframe.rte.psl.dataaccess.util.EgovMap"/>
<typeAlias alias="memberVO" type="egovframework.example.sample.service.MemberVO"/>
</typeAliases>
</configuration>
Member_SQL.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="egovframework.example.sample.service.impl.MemberMapper">
<select id="select_membercount" resultType="int">
select count(*) from member
</select>
</mapper>
MemberMapper.java
package egovframework.example.sample.service.impl;
import org.egovframe.rte.psl.dataaccess.mapper.Mapper;
@Mapper("memberMapper")
public interface MemberMapper {
public int select_membercount() throws Exception;
}
MemberService.java
package egovframework.example.sample.service;
public interface MemberService {
public int select_membercount() throws Exception;
}
MemberServiceImpl.java
package egovframework.example.sample.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import egovframework.example.sample.service.MemberService;
@Service("memberService")
public class MemberServiceImpl implements MemberService {
@Autowired private MemberMapper membermapper;
@Override
public int select_membercount() throws Exception {
return membermapper.select_membercount();
}
}
MemberController.java
package egovframework.example.sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import egovframework.example.sample.service.*;
@Controller
public class MemberController {
@Autowired private MemberService memberservice;
@RequestMapping(value="/main.do")
public String main(Model model) throws Exception {
int num = memberservice.select_membercount();
model.addAttribute("num", num);
return "main";
}
}
실행화면
'utility > eGovFrame' 카테고리의 다른 글
전자정부프레임워크 4.1.0 스프링 시큐리티로 암호화하기 (0) | 2024.03.08 |
---|---|
전자정부프레임워크 4.1.0 다운로드 및 실행하기 (0) | 2024.03.08 |