MybatisはIbatisの前身でした。これは、データ永続性レイヤー(ORM)フレームワークです。 MyBatisは、プレーンSQLクエリ、ストアドプロシージャ、高度なマッピングをサポートする優れた永続レイヤーフレームワークです。 MyBatisは、ほぼすべてのJDBCコードとパラメーターの手動設定を排除し、結果セットを検索します。 MyBatisは、構成とオリジナルマッピング、マッピングインターフェイス、JavaのPojo(古いJavaオブジェクト、通常のJavaオブジェクトを計画)にデータベース内のレコードに使用するために、簡単なXMLまたはアノテーションを使用します。各MyBatisアプリケーションは主にSQLSessionFactoryインスタンスを使用しており、SQLSessionFactoryBuilderを介してSQLSessionFactoryインスタンスを取得できます。
特定のコードは次のとおりです。
<?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"/> <プロパティname = "username" value = "settlement"/> <プロパティ名= "value =" value = "value =" value " </mappers> </configuration> <?xml version = "1.0" encoding = "utf-8"?> <!doctype mapper public " - // mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.モデルへのdbクエリの結果 - goods-> <resultmap type = "com.clark.model.goods" id = "t_good"> <id column = "id" property = "id"/> <result column = "cate_id" Property = "cateid"/> <result column = "property =" "complien =" "" "" prict "/> < <result column = "order_no" property = "orderno"/> <result column = "update_time" property = "updatetime"/> </resultmap> <! - id queryに基づいて商品タイプを返す<"goods" type = "com.clark.model.goods"> </typentmap-> < id = "SelectGoodByID" parameterType = "int" resultType = "goods"> Select id、cate_id、name、ride、description、order_no、update_time from商品=#{id} </select> <! id、cate_id、name、rice、description、order_no、update_time from goods </select> <! - mapがmap <string、pagebean> map-> <selectgoodsbypage "resultmap =" t_good "parametertype =" map "> <! <![cdata [select * from(Select g商品(ID、CATE_ID、名前、価格、説明、Order_no、update_time)Values(#{id}、#{cateid}、#{name}、#{rice}、#{description}、{orderno}、#{updatime})、#{updatime}) GOODS G SET G.NAME =#{name}、G.Order_no =#{Orderno}ここで、g.id =#{id} </update> <! - 削除商品パラメータータイプはint-> <deleteType "deletEgood" parameterType = "int"> delete g.id = {id} {id} </delete> </delete> </delete 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()+""; }}パッケージcom.clark.model; //シミュレートされたページネーションオブジェクトpagebean public class pagebean {//番号を開始するprivate integer startnumber; // end number private integer endnumber; public pagebean(){} public pagebean(integer startnumber、integer endnumber){super(); this.startnumber = startnumber; this.endnumber = endnumber; } public Integer getStartNumber(){return startnumber; } public void setStartNumber(integer startnumber){this.startnumber = startnumber; } public Integer getEndNumber(){return endnumber; } public void setendnumber(integer endnumber){this.endnumber = endnumber; }}パッケージcom.clark.mybatis; java.io.ioexceptionをインポートします。 java.io.readerをインポートします。 java.util.hashmapをインポートします。 java.util.listをインポートします。 java.util.mapをインポートします。 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をインポートします。 com.clark.model.pagebeanをインポートします。 public class testgoods {public static void main(string [] args)throws ioexception {string resource = "configuration.xml"; Reader reader = null; sqlSessionFactory SessionFactory = null; sqlsession session = null; try {reader = resources.getResourceasReader(resource); sessionfactory = new sqlsessionfactorybuilder()。build(reader); session = sessionfactory.opensession(); PageBean PageBean = new PageBean(8、20); map <string、pagebean> map = new hashmap <string、pagebean>(); map.put( "pagebean"、pagebean); list <goods> gs = findgoodsbypage(session、map); for(goods goods2:gs){system.out.println(goods2.tostring()); }} catch(ioexception e){e.printstacktrace(); }最後に{session.close(); reader.close(); }} // find by id public static goods findgoodbyid(sqlsession session、integer id){// clarkはnamespace name = "clark"に対応しています。商品goods =(goods)session.selectone( "clark.selectgoodbyid"、id);返品商品; } //すべてのpublic static list <goods> findallgoods(sqlsession session){list <goods> goods = session.selectlist( "clark.selectallgoods");返品商品; } public static list <goods> findgoodsbypage(sqlsession session、map <string、pagebean> map){list <goods> goods = session.selectlist( "clark.selectgoodsbypage"、map);返品商品; } //商品を挿入public static int insertgoods(sqlsessionセッション、商品){int result = session.insert( "clark.insertgood"、goods); session.commit();返品結果; } // Goods Public static int updategoods(sqlsessionセッション、商品){int result = session.update( "clark.updategood"、goods); session.commit();返品結果; } //削除商品public static int deletegood(sqlsession session、integer id){int result = session.delete( "clark.deletegood"、id); session.commit();返品結果; }}追加、削除、修正、ページネーションクエリを実装するためのMyBatisの方法に関する関連する知識を紹介します。後で更新し続けます。 Wulin Network Webサイトへの継続的なサポートをありがとうございます。