mybatis에서 매핑을 선택하면 resulttype 또는 resultmap과 함께 리턴 유형을 사용할 수 있습니다. resultType는 리턴 유형 (모델 객체의 엔티티에 해당)을 직접 나타내는 반면 결과 맵은 외부 결과 맵 (숨겨진 키-> 값 관계가 DB와 모델 간의 값 관계가 미리 정의됩니다)에 대한 참조입니다.
MyBatis가 매핑을 쿼리하는 경우, 실제로 각 속성 쿼리는 해당 맵에 배치됩니다. 여기서 키는 속성 이름이고 값은 해당 값입니다.
제공된 반환 유형 속성이 resultType 인 경우 MyBatis는 맵에서 키 값 쌍을 취해 resultType에 의해 지정된 객체의 해당 속성에 할당됩니다. 실제로 MyBatis의 각 쿼리 맵의 리턴 유형은 결과 맵입니다. 그러나 제공된 반환 유형 속성이 resultType 일 때 myBatis는 resultType에 의해 지정된 객체의 속성에 해당 값을 자동으로 할당합니다.
map 맵이 도메인 모델을 잘 나타낼 수 없기 때문에 제공된 반환 유형이 resultMap이면 해당 객체 자체로 더 변환해야하므로 종종 복잡한 쿼리에서 매우 유용합니다.
다음은 두 가지의 차이점을 설명하는 예입니다.
패키지 com.clark.model; import java.util.date; 공공 수업 상품 {개인 정수 ID; 개인 정수 Cateid; 개인 문자열 이름; 개인 이중 가격; 개인 문자열 설명; 개인 정수 주문 개인 날짜 업데이트 타임; 공공재 () {} 공공재 (정수 ID, 정수 CateId, 문자열 이름, 이중 가격, 문자열 설명, 정수 주문, 날짜 업데이트 타임) {super (); this.id = id; this.cateid = cateid; this.name = 이름; this.price = 가격; this.description = 설명; this.orderno = Orderno; this.updateTime = updateTime; } public Integer getId () {return id; } public void setId (정수 ID) {this.id = id; } public Integer getCateId () {return cateId; } public void setCateId (Integer CateId) {this.cateid = cateid; } public String getName () {return name; } public void setName (문자열 이름) {this.name = 이름; } public double getPrice () {반환 가격; } public void setPrice (이중 가격) {this.price = 가격; } public String getDescription () {return description; } public void setDescription (문자열 설명) {this.Description = 설명; } public Integer getOrderno () {return orderno; } public void setorderno (정수 OrderNo) {this.orderno = Orderno; } public date getTimestamp () {return updateTime; } public void settimestamp (날짜 updateTime) {this.updateTime = updateTime; } @Override public String toString () {return "[roods"[roods includ : id = "+this.getId ()+", name = "+this.getName ()+", orderNo = "+this.getOrderNo ()+", cateId = "+this.getCateId ()+", updateTime = "+this. this.getTimestamp ()" "; }}<? xml version = "1.0"alcoding = "utf-8"?> <! doctype configuration public "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <! alias = "goods"type = "com.clark.model.goods"> < /indecealias> < /inflealiases> <환경 = "개발"> <환경 ID = "Development"> <transactionManager type = "jdbc" /> <dataSource type = "plate"> <property name = "driver" "uracle.jdbc.driver.driver.oraCledRiver" value = "jdbc : thin :@172.30.0.125 : 1521 : oradb01"/> <property name = "value ="value = "settlement"/> <property name = "valess ="settlement "/> </datasource> </환경> </환경> <mapper resource ="com/clark/model/goodsmapper.dml </mappers> </configuration> </span>
<? xml version = "1.0"alcoding = "utf-8"?> <! doctype mapper public "-// mybatis.org//dtd Mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <Clark"> <resultmap type = "com.clark.model.goods"id = "t_good"> <id column = "id"property = "id"property = "id"/> <결과 열 = "cate_id"property = "cateid"/> <result column = "name"propertion = "name"/> <result column = "price ="price "/> <result column ="description ""description "/> <order_no" 속성 = "orderno"/> <result column = "update_time"property = "updateTime"/> </resultMap> <!-resultMap과 resultType의 차이-> <select id = "selectGoodById"ParameterType = "int"resultType = "goods"> select id, cate_id, 이름, 가격, description, order_no, updatte | selepte}. id = "selectallgoods"resultmap = "t_good"> id, cate_id, 이름, 가격, 설명, Order_No, update_time </select> <insert id = "insertgood"parametertype = "goods"> 상품에 삽입 값 (#{id},#{cateid},#{name},#{price},#{description},#{orderno},#{updateTime}) </insert> </mapper> 패키지 com.clark.mybatis; import java.io.ioexception; import java.io.Reader; 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; com.clark.model.goods import; public class testgoods {public static void main (String [] args) {String resource = "configuration.xml"; {reader reader = resources.getResourceAsReader (Resource); sqlsessionFactory sessionFactory = 새로운 SQLSESSIONFACTORYBUILDER (). 빌드 (Reader); sqlsession session = sessionfactory.opensession (); </span> <span style = "font-size : 18px;"> <span style = "흰색 공간 : pre"> </span> resultType goods = (goods) session.Selectone ( "clark.SelectGoodById", 4); System.out.println (goods.toString ()); </span> [html] Code <font-syize : 18px; "> <span style ="white-space : pre "> gs = session.selectlist ("clark.spans "); for (goods goods2 : gs) {system.out.println (goods2.toString ()); } // 상품 제품 = 새로운 제품 (4, 12, "Clark", 12.30, "Test is Ok", 5, New Date ()); // session.insert ( "clark.insertgood", goods); // session.commit (); } catch (ioexception e) {e.printstacktrace (); }}}결과 출력은 다음과 같습니다.
<span style="color:#cc0000;">[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---Result using resultType</span> <span style="color:#33ff33;">---Result using resultMap------------------</span>
위의 것은 MyBatis의 resulttype와 resultmap의 차이점에 대한 소개입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!