반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

Spring.MyBatis.DB사용 본문

[IT]/Spring

Spring.MyBatis.DB사용

givemebro 2021. 5. 26. 18:36
반응형
<?xml version="1.0" encoding="UTF-8"?>
<!-- Sql Mapper -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
	<sql id="selectMember">
		select id, password, name, address from mybatis_member
	</sql>
	<select id="findMemberById" resultType="memberVO">
		<include refid="selectMember" />
		where id=#{value}
	</select>
	<select id="findMemberByNameAndAddress" parameterType="memberVO"
		resultType="memberVO">
		<include refid="selectMember" />
		where name=#{name} and address=#{address}
	</select>
</mapper>
<?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.xsd">
	<!-- dbcp 객체 생성 -->
	<bean id="dbcp" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName"
			value="oracle.jdbc.OracleDriver" />
		<property name="url"
			value="jdbc:oracle:thin:@127.0.0.1:1521:xe" />
		<property name="username" value="scott" />
		<property name="password" value="tiger" />
	</bean>

	<!-- spring과 mybatis famework 연동 설정 -->
	<!-- SqlSessionFactoryBean은 SqlSessionFactory를 생성하는 역할을 한다 Spring에서 FactroyBean 
		Interface 하위의 클래스인 경우에는 IoC(DI, DL)를 할 때 실제 객체 SqlSessionFactoryBean이 아니라 
		FactoryBean interface의 abstract method인 getObject()를 실행해 반환되는 객체를 IoC(DI, 
		DL)로 전달한다 SqlSessionFactoryBean의 getObject()는 SqlSessionFactory 구현체를 반환하므로 
		SqlSessionTemplate으로는 SqlSessionFactory 구현체가 주입된다 짧게 줄이면 SqlSessionFactoryBean은 
		SqlSessionFactory 구현체를 만들고 SqlSessionTemplate으로는 SqlSessionFactory 구현체가 주입된다 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dbcp" />
		<property name="mapperLocations"
			value="classpath:/mybatis/config/*-mapper.xml" />
		<property name="typeAliasesPackage" value="org.kosta.model" />
	</bean>
	<!-- MyBatis Spring 연동시 개발 생산성을 위해 SqlSessionTemplate을 이용(반복적인 작업을 줄인다) 
		SqlSessionTemplate 은 선언적 방신의 트랜잭션을 지원한다 (AOP 기반 Transaction 제어) -->
	<!-- 반복작업을 template가 해준다 -->
	<bean id="sqlSessionTemplate"
		class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory" />
	</bean>
	<bean id="MemberDAO" class="org.kosta.model.MemberDAOImple">
		<constructor-arg ref="sqlSessionTemplate"></constructor-arg>
	</bean>

</beans>
package org.kosta.model;

import java.util.List;

public interface MemberDAO {
	public MemberVO findMemberById(String id);

	public List<MemberVO> findMemberByNameAndAddress(MemberVO paramVO);
}
package org.kosta.model;

import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;

public class MemberDAOImple implements MemberDAO {
	private SqlSessionTemplate template;

	public MemberDAOImple(SqlSessionTemplate template) {
		super();
		this.template = template;
	}

	@Override
	public MemberVO findMemberById(String id) {
		// 1. mapper의 namespace.sql id 2. 전달할 검색조건
		return template.selectOne("member.findMemberById", id);
	}

	@Override
	public List<MemberVO> findMemberByNameAndAddress(MemberVO paramVO) {
		return template.selectList("member.findMemberByNameAndAddress", paramVO);
	}

}
package org.kosta.model;

public class MemberVO {
	private String id;
	private String password;
	private String name;
	private String address;

	public MemberVO(String id, String password, String name, String address) {
		super();
		this.id = id;
		this.password = password;
		this.name = name;
		this.address = address;
	}

	public MemberVO() {
		super();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "MemberVO [id=" + id + ", password=" + password + ", name=" + name + ", address=" + address + "]";
	}

}
package test;

import java.util.List;

import org.kosta.model.MemberDAO;
import org.kosta.model.MemberVO;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMyBatis {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
		MemberDAO dao = (MemberDAO) ctx.getBean("MemberDAO");
//		System.out.println("**아이디 회원검색**");
//		System.out.println(dao.findMemberById("java"));
		System.out.println("**이름과 주소로 회원검색**");
		// 검색조건이 여러개 일 경우에는 Object(VO or DTO) 또는 Map으로 전달한다
		MemberVO paramVO = new MemberVO();
		paramVO.setName("아이유");
		paramVO.setAddress("오리");
		List<MemberVO> list = dao.findMemberByNameAndAddress(paramVO);
		for (MemberVO vo : list) {
			System.out.println(vo);
		}
		ctx.close();
	}
}
<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>3-MyBatis-Study2-Member</groupId>
	<artifactId>3-MyBatis-Study2-Member</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.14.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.14.RELEASE</version>
		</dependency>

	</dependencies>
</project>

반응형

'[IT] > Spring' 카테고리의 다른 글

param.productNo 값 넣고 insert  (0) 2021.05.28
Spring.MyBatis.table column 명과 VO의 instance변수 명이 다른 경우  (0) 2021.05.28
Spring.MyBatis.DB.Map  (0) 2021.05.27
Spring.MyBatis Framework  (0) 2021.05.27
Spring.AOP.around  (0) 2021.05.25
log4j  (0) 2021.05.24
Spring.AOP(Aspect Oriented Programming)  (0) 2021.05.21
Spring.IOC.판서  (0) 2021.05.21
Comments