MyBatisでマッピングを選択すると、ResultTypeまたはresultMapで返品タイプを使用できます。 resultTypeは、リターンタイプ(モデルオブジェクトのエンティティに対応する)を直接表し、結果マップは外部結果マップ(DBとモデルの値の関係が事前に定義されている)への参照ですが、resultTypeとresultMapは同時に存在することはできません。
MyBatisがマッピングをクエリしている場合、実際、各属性クエリは対応するマップに配置されます。キーは属性名で、値は対応する値です。
remived提供されたreturnタイプ属性がresultTypeである場合、MyBatisはマップ内のキー値のペアを取り出し、resultTypeで指定されたオブジェクトの対応する属性に割り当てます。したがって、実際には、mybatisの各クエリマップの返品タイプは結果マップです。ただし、提供された返品タイプ属性がresultTypeである場合、mybatisはresultTypeで指定されたオブジェクトの属性に対応する値を自動的に割り当てます。
remivied提供された返品タイプが結果マップである場合、マップはドメインモデルを適切に表すことができないため、それを対応するオブジェクト自体にさらに変換する必要があります。これは、複雑なクエリで非常に役立つことがよくあります。
次に、2つの違いを説明する例を示します。
パッケージcom.clark.model; Import Java.util.date;パブリッククラスグッズ{プライベート整数ID;プライベート整数CATEID;プライベート文字列名;プライベートダブル価格。プライベート文字列の説明;プライベート整数orderno;プライベートデートアップデートタイム; Public Goods(){}公共財(整数ID、整数CATEID、文字列名、2倍の価格、文字列説明、整数ORDERNO、date updateTime){super(); this.id = id; this.cateid = cateid; this.name = name; this.price = price; this.description = description; this.orderno = orderno; this.updateTime = updateTime; } public Integer getId(){return id; } public void setid(integer 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(string name){this.name = name; } public double getPrice(){return price; } public void setPrice(2倍の価格){this.price = price; } public string getDescription(){return description; } public void setDescription(string description){this.description = description; } public Integer getOrderNo(){return orderno; } public void setorderno(integer orderno){this.orderno = orderno; } public date getTimestamp(){return updateTime; } public void sittimestamp(date updatetime){this.updateTime = updateTime; } @Override public String toString(){return "[good include:id ="+this.getId()+"+"+this.getName()+"、orderno ="+this.getorderno()+"、cateid ="+this.getcateid()+"、updatime ="+thisgetimestamp()+""; }}<?xml version = "1.0" encoding = "utf-8"?> <!doctype構成public " - // mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-crofig.dtd"エイリアス= "goods" type = "com.clark.model.goods"> < /typealias> < /typealiase> <環境> <環境= "development"> <環境id = "development"> <transactionmanager type = "jdbc" /> <datasource type = "pooled"> <propertion name "oracle.jdbc. dirceledriver" /> dirceledriver " /> dirceledriver" name = "url" value = "jdbc:oracle:thin:@172.30.0.125:1521:oradb01"/> <プロパティ名= "username" value = "settlement"/> <Property name = "Password" value = "value =" value " </mappers> </configuration> </span>
<?xml version = "1.0" encoding = "UTF-8"?> < type = "com.clark.model.goods" id = "t_good"> <id column = "id" property = "id"/> <result column = "cate_id" property = "cateid"/> <result column = "name =" name = "name =" name "/> <result column =" result column = "pricep" = "property"/> <result column = "説明"プロパティ= "Orderno"/> <result column = "update_time" Property = "updateTime"/> </resultmap> < id = "selectalgoods" resultmap = "t_good"> select id、cate_id、name、name、rise、description、order_no、update_time from goods </select> <insert inters = "insertgood" parametertype = "goods" values(#{id}、#{cateid}、#{name}、#{rice}、#{description}、#{orderno}、#{updateTime})</insert> </mapper>パッケージcom.clark.mybatis; java.io.ioexceptionをインポートします。 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をインポートします。 public class testgoods {public static void main(string [] args){string resource = "configuration.xml"; try {reader reader = resources.getResourceasReader(resource); sqlSessionFactory SessionFactory = new SQLSessionFactoryBuilder()。build(reader); sqlsession session = sessionfactory.opensession(); </span> <span style = "font-size:18px;"> <span style = "white-space:pre"> </span> // resultType goods =(good)session.selectone( "clark.selectgoodbyid"、4); system.out.println(goods.tostring()); </span> [html]ビュープレーンコピーコードfragments on code <span style = "font-size:18px;"> <span style = "white style =" white sty。 for(goods goods2:gs){system.out.println(goods2.tostring()); } // Goods Goods = New Goods(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;"> [商品ID = 4、name = clark、orderno = null、cateid = null、updateTime = null] ---結果を使用した結果</span> <span style = "color:#33ff33;"> ---結果を使用した結果------------ </span>
上記は、MyBatisのResultTypeとresultMapの違いの紹介の紹介です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!