[1] MySQL DataBase Table 생성
CREATE TABLE details.student(
ID int(10) NOT NULL AUTO_INCREMENT,
NAME varchar(100) NOT NULL,
BRANCH varchar(255) NOT NULL,
PERCENTAGE int(3) NOT NULL,
PHONE int(11) NOT NULL,
EMAIL varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
);
[2] Maven Project 생성
[3] pom.xml : dependency 및 build 추가
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
[4] MyBatis Conficuration XML
SqlMapConfig.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://localhost:3306/details"/>
<property name = "username" value = "root"/>
<property name = "password" value = "1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource = "mybatis/Student.xml"/>
</mappers>
</configuration>
[5] POJO Class
public class Student {
private int id;
private String name;
private String branch;
private int percentage;
private int phone;
private String email;
//Constructor, Setters and getters
}
[6] mybatis/Student.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 = "Student">
<insert id = "insert" parameterType = "Student">
INSERT INTO STUDENT (NAME, BRANCH, PERCENTAGE, PHONE, EMAIL ) VALUES (#{name}, #{branch}, #{percentage}, #{phone}, #{email});
<selectKey keyProperty = "id" resultType = "int" order = "AFTER">
select last_insert_id() as id
</selectKey>
</insert>
</mapper>
[7] MybatisInser.java
import java.io.IOException;
import java.io.Reader;
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 mybatisInsert {
public static void main(String args[]) throws IOException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
//Create a new student object
Student student = new Student("Mohammad","It", 80, 984803322, "Mohammad@gmail.com" );
//Insert student data
session.insert("Student.insert", student);
System.out.println("record inserted successfully");
session.commit();
session.close();
}
}
[Mybatis] Delete Operation (0) | 2020.01.20 |
---|---|
[Mybatis] Update Operation (0) | 2020.01.20 |
[Mybatis] Read Operation, (0) | 2020.01.20 |
댓글 영역