본문 바로가기
수업(국비지원)/Mybatis

[MYBATIS] Mybatis DB 생성, 추가, 삭제

by byeolsub 2023. 4. 21.

📌 Main3.java

package main;

import java.io.IOException;
import java.io.InputStream;
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 mapper.StudentMapper3;

public class Main3 {
	private static SqlSessionFactory sqlMap;
	static { //static 초기화 블럭
		InputStream input = null;
		try {
			input = Resources.getResourceAsStream("mapper/mybatis-config.xml");
		} catch(IOException e) {
			e.printStackTrace();
		}
		sqlMap = new SqlSessionFactoryBuilder().build(input);
	}
	
	public static void main(String[] args) {
		SqlSession session = sqlMap.openSession();
		System.out.println("이몽룡 학생 추가하기");
		Student st = new Student();
		
		st.setStudno(1001);
		st.setName("이몽룡");
		st.setJumin("1234562345678");
		st.setId("leemy2");
		int result = session.getMapper(StudentMapper3.class).insert(st);
		System.out.println(result + "건 학생 정보에 추가");
		System.out.println("이몽룡 학생 조회하기");
		List<Student> list = session.getMapper(StudentMapper3.class).select_name("이몽룡");
		for(Student s : list) System.out.println(s);
//		session.commit(); // 실제 물리적인 저장공간에 레코드 저장됨.

    System.out.println("이몽룡 학생 학년:1, 몸무게:80, 키:175로 변경하기");
		st.setGrade(1);
		st.setWeight(80);
		st.setHeight(175);
		result = session.getMapper(StudentMapper3.class).update(st);
		System.out.println(result + "건 학생 정보에 추가");
		System.out.println("이몽룡 학생 조회하기");
		list = session.getMapper(StudentMapper3.class).select_name("이몽룡");
		for(Student s : list) System.out.println(s);

    System.out.println("1001번 학생의 정보 삭제하기");
		result = session.getMapper(StudentMapper3.class).delete(st.getStudno());
		System.out.println("이몽룡 학생 조회하기");
		list = session.getMapper(StudentMapper3.class).select_name("이몽룡");
		for(Student s : list) System.out.println(s);
	}
}

 

📌 StudentMapper3.java -interface로 생성

package mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import main.Student;

public interface StudentMapper3 {

	@Insert("insert into student (studno,name,jumin,id)"
			+ " values (#{studno},#{name},#{jumin},#{id})")
	int insert(Student st);

	@Select("select * from student where name = #{name}")
	List<Student> select_name(String string);

	@Update("update student set grade = #{grade}, weight = #{weight}, height = #{height} where studno = #{studno}")
	int update(Student st);

	@Delete("delete from student where studno = #{studno}")
	int delete(int studno);

}

 

'수업(국비지원) > Mybatis' 카테고리의 다른 글

[MYBATIS] (MVC 2) controller,Dao,Mapper  (0) 2023.04.21
[MYBATIS] Mybatis - MVC 2 변경하기  (0) 2023.04.21
[MYBATIS] Mybatis DB조회  (0) 2023.04.21
[MYBATIS] Mybatis DB연결하기  (0) 2023.04.21
[MYBATIS] Mybatisjava 설정  (0) 2023.04.21