스프링

myBatis 추가

https://mvnrepository.com/artifact/org.mybatis/mybatis/3.4.4

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.4</version>
</dependency>

 

 

시작하기

 

 

 mybatis-config-test.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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"/>
        <property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3306/team4"/>
        <property name="username" value="wjheo"/>
        <property name="password" value="1111"/>
      </dataSource>
    </environment>
  </environments>
    
  <mappers>
    <mapper resource="Userapper.xml"/>
  </mappers> 
  
  
</configuration>


 

 

 Userapper.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="UserMapper">
  
  <select id="findById" resultType="com.java.dto.User">
  	select * from USERS where userId=#{userId}
  </select>
  
  
  <insert id="create">
  	insert into USERS values(#{userId}, #{password} , #{name} , #{email})
  </insert>
  
</mapper>

 

 

 class MyBatisTest

package com.java.dao.users;


import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.java.dto.User;

public class MyBatisTest {
	private static final Logger log = LoggerFactory.getLogger(MyBatisTest.class);
	
	private SqlSessionFactory sqlSessionFactory;
	
	@Before
	public void setUp() throws IOException{
		String resource="mybatis-config-test.xml";
		InputStream inputStream=Resources.getResourceAsStream(resource);
		sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
	}
	
	
	@Test
	public void gettingStarted() throws IOException{
		try(SqlSession session = sqlSessionFactory.openSession()) {
			User user =session.selectOne("UserMapper.findById", "braverokmc");
			log.debug(" gettingStarted  {} " , user.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unused")
	@Test
	public void insert() {
		SqlSession session=null;
		try{
			session =sqlSessionFactory.openSession();
			User user =new User("snajgi79", "password" , "sanjigi" , "sangigi@slipp.net");
			session.insert("UserMapper.create", user); 
			session.commit();
			session.close();
			session=sqlSessionFactory.openSession();
		    User actual =session.selectOne("UserMapper.findById", user.getUserId());
			assertThat(actual.getUserId(), is(user.getUserId()));
			
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(session!=null)session.close();
		}
	}
	
	
	
	
	
}




 

 

 

spring

 

about author

PHRASE

Level 60  머나먼나라

나는 어릴 때, 가난 속에서 자랐기 때문에 온갖 고생을 참으며 살았다. 겨울이 되어도 팔굽이 노출되는 헌 옷을 입었고, 발가락이 나오는 헌 구두를 신었다. 그러나 소년시절의 고생은 용기와 희망과 근면을 배우는 하늘의 은총이라 생각하지 않으면 안 된다. 영웅과 위인은 모두 가난 속에 태어났다. 성실 근면하며, 자신의 일에 최선을 다한다는 정신만 있으면, 가난

댓글 ( 4)

댓글 남기기

작성