728x90
1. Hello.java 작성
2. mybatis-config.xml 작성
3. Mapper.xml 작성
4. pom.xml 작성

Hello.java

  • proxy 사용
interface Commend{
	int commend(SqlSession session);
}

class Test{
	void m1(UserDao dao, Commend c) {
		SqlSession session = dao.ssf.openSession();
				
		try {
			//int result = session.update("test04", num); // 이게 쿼리문 실행 문장임 test02라는 함수를 찾아 9999인수 전달을 한 것이다.
			int result = c.commend(session);
			if (result>0) session.commit(); // 커밋을 하면 된다.
		} catch (Exception e) { e.printStackTrace(); 
		} finally { session.close(); }
	}
}

class UserDao{
	SqlSessionFactory ssf = null;
	
	UserDao() {
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) { e.printStackTrace(); }
	}
	
	void insert(final int num) {
		Test t = new Test();
		t.m1(this, new Commend() {
			public int commend(SqlSession session) {
				return session.insert("test02", num);
			}
		});
	}
	
	void update(final int num) {
		Test t = new Test();
		t.m1(this, new Commend() {
			public int commend(SqlSession session) {
				return session.insert("test04", num); // 전달 변수로 쓰기 위해서는 final을 써야한다.
			}
		});
	}
	
	void showAge() {
		SqlSession session = ssf.openSession();
		
		List<Integer> mm = session.selectList("test01", null);
		for (Integer item : mm) {
			System.out.print(item + " ");
		}System.out.println();
		
		session.close();
	}
}

public class Hello {
	public static void main(String[] args) {
		UserDao dao = new UserDao();
		dao.insert(552);
//		dao.update(8888);
		dao.showAge();
		
		System.out.println("Exit");
	}
}

 

mybatis-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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
      
        <property name="driver" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/db01" />
		<property name="username" value="root" />
		<property name="password" value="1234" />
        
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="Mapper.xml"/>
  </mappers>
</configuration>

 

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="Mapper">
	<select id="test01" resultType="int">
		SELECT age FROM table01
	</select>

	<insert id="test02" parameterType="int">
		INSERT INTO table01(age) VALUES (#{age})
	</insert>
	
	<delete id="test03" parameterType="int">
		DELETE FROM table01 WHERE age = #{age}
	</delete>
	
	<update id="test04" parameterType="int">
		UPDATE table01 SET age = 777 WHERE age = #{age}
	</update>
</mapper>

 

pom.xml

<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>Mybatis01</groupId>
  <artifactId>Mybatis01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <dependencies>
    	<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.7</version>
		</dependency>
		
    	<!-- MySQL -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.26</version>
		</dependency>
		
    	<!-- commons-dbcp -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
    	
    	
		<dependency>
	    	<groupId>org.apache.maven.plugins</groupId>
	    	<artifactId>maven-compiler-plugin</artifactId>
	    	<version>3.8.1</version>
		</dependency>
  	</dependencies>
  
</project>
728x90

'웹 full stack 교육 > 이론' 카테고리의 다른 글

[Linux] Ubuntu에서 Java파일 실행시키기  (0) 2021.09.27
[Linux] 노트북 리눅스로 사용하기  (0) 2021.09.27
[Spring] Mybatis 시작하기  (0) 2021.09.23
[Spring] 사용 단계  (0) 2021.09.23
[Git] Pull & merge  (0) 2021.09.17

+ Recent posts