데이터베이스 테이블 칼럼
D | 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>
<!-- 추가 -->
<delete id = "deleteById" parameterType = "int">
DELETE FROM STUDENT WHERE ID = #{id};
</delete>
</mapper>
MybatisDelete.java
package net.mybatis.main;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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 MybatisDelete {
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
//삭제전 조회
List<Student > students = session.selectList("Student.getAll");
System.out.println("삭제 전 ");
for(Student student : students) {
System.out.println(student);
}
System.out.println();
//삭제
session.delete("Student.deleteById",2);
System.out.println("삭제 성공");
System.out.println();
//삭제 후 조회
students = session.selectList("Student.getAll");
System.out.println("삭제 후");
for(Student student : students) {
System.out.println(student);
}
session.commit();
session.close();
}
}
[Mybatis] Update Operation (0) | 2020.01.20 |
---|---|
[Mybatis] Read Operation, (0) | 2020.01.20 |
[Mybatis] Configuration XML, Mapper XML, Create Operation (0) | 2020.01.19 |
댓글 영역