재미있게 코딩합시다

Mybatis - Spring 연동하기 본문

IT /Spring(스프링)

Mybatis - Spring 연동하기

감민셔 2019. 1. 9. 16:04

Mybatis_Spring 연동하기

마이바티스에서 사용하던 객체들을 스프링컨테이너에 등록하고 받아서 사용하면 된다.


SqlSessionFactoryBuilder

->설정정보(configuration)읽어서 SqlSessionFactory 만들어주는 역할


SqlSessionFactory

-> SqlSession을 만들어주는 역할


SqlSession

-> 실제 sql쿼리를 날리는 역할


SqlSessionFactory를 만들기 위해서는 configuration.xml 에 있는 정보들이 필요하다.

(디비 연결정보, 매퍼위치, type alias 등 정보들)

메이븐 설정먼저 해주세요~

이름은 당연히 applicationContext.xml로


apllicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://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"> <context:component-scan base-package="dao"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> <property name="url" value="jdbc:mysql://localhost/bitcamp"> <property name="username" value="root"> <property name="password" value="mysql"> </property></property></property></property></bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"> <property name="typeAliasesPackage" value="model"> <property name="mapperLocations" value="classpath*:dao/mapper/**/*.xml"> </property></property></property></bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"> </constructor-arg></bean> <bean id="(사용할Dao이름)" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="dao.IDeptDao"> <property name="sqlSessionTemplate" ref="sqlSession"> </property></property></bean> </context:component-scan></beans>

사용할 DAO 코드~

DeptDao


package dao; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import model.Dept; public interface IDeptDao { //어노테이션 방식은 구현할 Dao의 껍데기를 정의하는 //Interface에서 각 함수에 @ 어노테이션을 이용해서 //sql에 대한 정보를 넣어줌.. public void insertDept(Dept dept); public void updateDept(Dept dept); public void deleteDept(int deptno); public Dept selectOne(int deptno); public List

selectAll(); }


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 = "dao.IDeptDao"> <!--원래는 dao.IDeptDao 가아니라 dao.emloyeeMapper 엿다--> <insert id="insertDept" parameterType="dept"> insert into dept values ( #{deptno},#{dname},#{dloc} ) </insert> <update id="updateDept" parameterType="dept"> update dept set dname= #{dname}, dloc = #{dloc} where deptno=#{deptno} </update> <delete id="deleteDept" parameterType="int"> delete from dept where deptno = #{deptno} </delete> <select id="selectOne" parameterType="int" resultType="dept"> select * from dept where deptno = #{deptno} </select> <select id="selectAll" resultType="dept"> select * from dept </select> </mapper>


model 객체

	package model;

public class Dept {

	private int deptno;
	private String dname;
	private String dloc;
	
	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 getDloc() {
		return dloc;
	}
	public void setDloc(String dloc) {
		this.dloc = dloc;
	}
	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + ", dloc=" + dloc + "]";
	}
}

Main 클래스에서 코드 구현후 mysql 과 연동되는지 알아보기

Comments