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

 

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>

 

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">
	<insert id="test02" parameterType="int">
		INSERT INTO table01(age) VALUES (#{age})
	</insert>
</mapper>

 

Hello.java
package Pack;

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;

public class Hello {

	public static void main(String[] args) {
		System.out.println(100);
		SqlSessionFactory ssf = null;
		SqlSession session = null;
		
		InputStream is = null;
		try {
			is = Resources.getResourceAsStream("mybatis-config.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ssf = new SqlSessionFactoryBuilder().build(is);
		System.out.println(2);
		
		session = ssf.openSession();
		
		try {
//			<insert id="test02" parameterType="int">
//				INSERT INTO table01(age) VALUES (#{age})
//			</insert>
			int result = session.insert("test02", 9999); // 이게 쿼리문 실행 문장임 test02라는 함수를 찾아 9999인수 전달을 한 것이다.
			System.out.println(result); // 양수가 나와야 정상적인 것임
			if (result>0) { // 정상적으로 양수가 나왔다면,
				session.commit(); // 커밋을 하면 된다.
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println(3);
	}
}
728x90

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

[Linux] 노트북 리눅스로 사용하기  (0) 2021.09.27
[Spring] Mybatis 와 MySQL  (0) 2021.09.23
[Spring] 사용 단계  (0) 2021.09.23
[Git] Pull & merge  (0) 2021.09.17
[SQL] PostgreSQL Window에서 실행하기  (0) 2021.09.17

+ Recent posts