데이터베이스 칼럼
ID | NAME | BRANCH | PERCENTAGE | PHONE | |
1 | Mohammad | It | 80 | 984803322 | Mohammad@gmail.com |
2 | shyam | It | 75 | 984800000 | shyam@gmail.com |
Student.java
package net.school.lesson;
public class Student {
private int id;
private String name;
private String branch;
private int percentage;
private int phone;
private String email;
public Student() {
}
public Student(String name, String branch, int percentage, int phone, String email) {
this.name = name;
this.branch = branch;
this.percentage = percentage;
this.phone = phone;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public int getPercentage() {
return percentage;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", branch="
+ branch + ", percentage=" + percentage + ", phone="
+ phone + ", email=" + email + "]";
}
}
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 = "net.school.lesson.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>
<resultMap id = "result" type = "net.school.lesson.Student">
<result property = "id" column = "ID"/>
<result property = "name" column = "NAME"/>
<result property = "branch" column = "BRANCH"/>
<result property = "percentage" column = "PERCENTAGE"/>
<result property = "phone" column = "PHONE"/>
<result property = "email" column = "EMAIL"/>
</resultMap>
<select id = "getAll" resultMap = "result">
SELECT * FROM STUDENT;
</select>
<select id = "getById" parameterType = "int" resultMap = "result">
SELECT * FROM STUDENT WHERE ID = #{id};
</select>
<update id = "update" parameterType = "net.school.lesson.Student">
UPDATE STUDENT SET NAME = #{name},
BRANCH = #{branch},
PERCENTAGE = #{percentage},
PHONE = #{phone},
EMAIL = #{email}
WHERE ID = #{id};
</update>
</mapper>
MybatisUpdate.java
package net.mybatis.main;
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;
import net.school.lesson.Student;
public class MybatisUpdate {
public static void main(String[] args) throws IOException {
String resource = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
Student student = session.selectOne("Student.getById",1);
System.out.println("업데이트 전 : " + student);
student.setEmail("mohamad123@yahoo.com");
student.setPhone(9000000);
//update the studenet record
session.update("Student.update", student);
System.out.println("Record updated successful");
Student updatedStudent = session.selectOne("Student.getById",1);
System.out.println("Detail of the student after update operation");
System.out.println("업데이트 후 : "+updatedStudent);
session.commit();
session.close();
}
}
실행후 결과 확인
[Mybatis] Delete Operation (0) | 2020.01.20 |
---|---|
[Mybatis] Read Operation, (0) | 2020.01.20 |
[Mybatis] Configuration XML, Mapper XML, Create Operation (0) | 2020.01.19 |
댓글 영역