on
JDBC MyBatis 01
JDBC MyBatis 01
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
//////////////////////////Main 클래스////////////////////////// import java.util.List; import com.dto.Dept; import com.service.Service; public class DeptMain { public static void main( String [] args) { Service service = new Service(); List < Dept > list = service.selectAll(); for (Dept dept : list) { System . out . println (dept); } } } //////////////////////////Service 클래스////////////////////////// import java.util.List; import com.dto.Dept; import com.service.Service; public class DeptMain { public static void main( String [] args) { Service service = new Service(); List < Dept > list = service.selectAll(); for (Dept dept : list) { System . out . println (dept); } } } //////////////////////////Dao클래스////////////////////////// package com.dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.dto.Dept; public class Dao { public Dao() { } public List < Dept > selectAll(SqlSession session) { List < Dept > list = session.selectList( "selectAll" ); //selectAll을 해당 메서드의 아이디로 하여 //sql이 실행될수 있게 만들어줌 return list; } } ////////////////////////// Dept클래스 ////////////////////////// package com.dto; public class Dept { int deptno; String dname; String loc; public int getDeptno() { return deptno; } public void setDeptno( int deptno) { this .deptno = deptno; } public String getDname() { return dname; } public void setDname( String dname) { this .dname = dname; } public String getLoc() { return loc; } public void setLoc( String loc) { this .loc = loc; } @Override public String toString () { return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]" ; } public Dept( int deptno, String dname, String loc) { super (); this .deptno = deptno; this .dname = dname; this .loc = loc; } public Dept() { super (); } } ////////////////////////// DeptMapper.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 = "org.mybatis.example.BlogMapper" > < insert id = "deptInsert" parameterType = "com.dto.Dept" > insert into dept (deptno, dname, loc) values (#{deptno}, #{dname}, #{loc}) < / insert > < select id = "selectAll" resultType = "com.dto.Dept" > select deptno, dname loc from dept < / select > < / mapper > ////////////////////////// jdbc.properties ////////////////////////// #key = value #4information for db connecting oracle.jdbc = oracle.jdbc.driver.OracleDriver oracle.url = jdbc:oracle:thin:@localhost: 1521 :xe oracle.userid = scott oracle.passwd = tiger ////////////////////////// Configuration.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 > < properties resource = "jdbc.properties" / > < environments default = "development" > < environment id = "development" > < transactionManager type = "JDBC" / > < ! - - db연결 4가지 정보 - - > < dataSource type = "POOLED" > < property name = "driver" value = "${oracle.jdbc}" / > < property name = "url" value = "${oracle.url}" / > < property name = "username" value = "${oracle.userid}" / > < property name = "password" value = "${oracle.passwd}" / > < / dataSource > < / environment > < / environments > < ! - - mapper경로등록 (mapper.xml파일이 존재해야 함) - - > < mappers > < mapper resource = "DeptMapper.xml" / > < / mappers > < / configuration > ////////////////////////// DeptFactory ////////////////////////// package com.config; import java.io.IOException; 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 DeptFactory { static SqlSessionFactory sqlSessionFactory = null ; static { //초기화 String resource = "Configuration.xml" ; //수정이 필요한 유일한 부분 //Configuration.xml의 경로가 바뀔시에 수정이 필요 InputStream inputStream = null ; try { inputStream = Resources.getResourceAsStream(resource); System . out . println ( "configuration.xml 로딩 성공" ); } catch (IOException e) { // TODO Auto-generated catch block //파일의 경로가 틀린경우 ioException발생 e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //DriverManager와 비슷한 객체 } //end static //SqlSession 반환 public static SqlSession getSqlSession() { // //SqlSession session =MySqlSessionFactory.getSqlSession(); SqlSession session = sqlSessionFactory.openSession(); //getConnection의 Connection과 비슷한 기능의 SqlSesstion객체 생성 리턴 return session; } } Colored by Color Scripter
from http://cocoshin.tistory.com/29 by ccl(A) rewrite - 2021-12-01 19:27:50