Spring MVC Mybatis DB 사용해보기

메타플랫폼대표
|2024. 7. 18. 12:19
반응형

2024_0718_SpringVisit [ADD] SPRING DATABASE

 

GitHub - chaSunil/FirstProject: 1차 프로젝트

1차 프로젝트. Contribute to chaSunil/FirstProject development by creating an account on GitHub.

github.com

 

 

vo 등록하기

package vo;

public class DeptVo {

	int deptno;
	String dname;
	String loc;
	
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	
}

 

 

 

alias 등록하기

 

 

dao 등록하기

package dao;

import org.apache.ibatis.session.SqlSession;

public class DeptDao {
	
	SqlSession sqlSession;

	// Constructor Injection
	public DeptDao(SqlSession sqlSession) {
		super();
		this.sqlSession = sqlSession;
	}

}

 

 

dept.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="">

	<select id="dept_list" resultType="dept">
		select * from dept
	</select>

</mapper>

 

 

context-3-dao에서 dao 경로 지정해주기

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<!-- DeptDao -->
	<!-- DeptDao dept_dao = new DeptDao(sqlSession) -->
	<bean id="dept_dao" class="dao.DeptDao">
		<constructor-arg ref="sqlSession" />
	</bean>
	
</beans>

 

 

Dao에서 selectList 등록하는 과정

public class DeptDao {
	
	SqlSession sqlSession;

	// Constructor Injection
	public DeptDao(SqlSession sqlSession) {
		super();
		this.sqlSession = sqlSession;
	}
	
	public List<DeptVo> selectList() {
		
		return sqlSession.selectList("dept.dept_list");
	}

}

 

 

과거 factory를 통해서 sqlSession을 받아오고 sql문을 넣어준뒤 close하는 작업을 전부 Spring이 대신 해준다.

 

 

controller 등록하기

@Controller
public class DeptController {
	
	DeptDao dept_Dao;
	// 어차피 DeptController는 프로그램 시작시 Singleton이 기본이기에, 기본 생성자 필요없음
	public DeptController(DeptDao dept_Dao) {
		super();
		this.dept_Dao = dept_Dao;
	}
	
	@RequestMapping("/dept/list.do")
	public String list() {
		
		List<DeptVo> list = dept_Dao.selectList();
		System.out.println(list.size());
		
		return "dept/dept_list"; // /WEB-INF/views/ + dept_list + .jsp
	}
	
}

 

 

수동으로 생성하기

	<!-- Autowired지원속성 : 수동생성시에는 반드시 기록  -->
	<context:annotation-config/>
	
	<!-- 수동생성 -->
	<beans:bean class="controller.DeptController">
		<beans:constructor-arg ref="dept_dao" />
	</beans:bean>

 

 

경로 확인해서 생성하기

 

 

 

바로 되면 좋은데 version을 downgrade 해주어야 한다. 그리고, Tomcat 9 version 사용(9 version안에 ojdbc.jar 넣어준다)

<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>
	<groupId>com.githrd</groupId>
	<artifactId>db</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	
	
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<release>17</release>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
			</plugin>
		</plugins>
	</build>


	<dependencies>

<!--		 https://mvnrepository.com/artifact/org.springframework/spring-web 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>6.0.0</version>
		</dependency>-->
		
		
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.0.0.RELEASE</version>
		</dependency>

<!--		
		https://mvnrepository.com/artifact/org.springframework/spring-webmvc 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>6.0.0</version>
		</dependency>-->

		<!--
		https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.0.0.RELEASE</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.3</version>
		</dependency>


		<!--
		https://mvnrepository.com/artifact/commons-collections/commons-collections -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.2</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.3.RELEASE</version>
		</dependency>


	</dependencies>

</project>

 

 

 

 

그리고 update 해줘야 경로명 변경이 된다.

 

 

 

views안에 dept.jsp 파일을 넣어주면 정상적으로 실행이 된다.

반응형