Simple addition, deletion, modification, and query of mybatis and simple paging query implementation

Source: Internet
Author: User
Simple addition, deletion, modification, and query of mybatis and simple paging query implementation
<?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><typeAliases><!-- give a alias for model --><typeAlias alias="goods" type="com.clark.model.Goods"></typeAlias></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="oracle.jdbc.driver.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@172.30.0.125:1521:oradb01" /><property name="username" value="settlement" /><property name="password" value="settlement" /></dataSource></environment></environments><mappers><mapper resource="com/clark/model/goodsMapper.xml" /></mappers></configuration>
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" Clark "> <! -- Map the database query results to model -- Goods --> <resultmap type = "com. clark. model. goods "id =" t_good "> <ID column =" ID "property =" ID "/> <result column =" cate_id "property =" cateid "/> <result column = "name" property = "name"/> <result column = "price" property = "price"/> <result column = "Description" property = "Description"/> <result column = "order_no" property = "orderno"/> <result column = "update_time" property = "updatetime"/> </resultmap> <! -- Returns the goods type <typealias alias = "goods" type = "com. Clark. model. Goods"> </typealias> --> <! -- Difference between resultmap and resulttype --> <select id = "selectgoodbyid" parametertype = "int" resulttype = "goods"> select ID, cate_id, name, price, description, order_no, update_time from goods where ID =#{ ID} </SELECT> <! -- Query all goods and return the resultmap type --> <select id = "selectallgoods" resultmap = "t_good"> select ID, cate_id, name, price, description, order_no, update_time from goods </SELECT> <! -- Specify parametertype = map. The map format is Map <string, pagebean> map --> <select id = "selectgoodsbypage" resultmap = "t_good" parametertype = "map"> <! -- Order by id asc refers to sorting query results in ascending order --> <! [CDATA [select * from (select G. *, rownum rn from (select * from goods) g where 1 = 1 and rownum <==#{ pagebean. endnumber}) Where rn >=# {pagebean. startnumber} order by id asc]> </SELECT> <! -- Goods parameter type is added to goods --> <insert id = "insertgood" parametertype = "goods"> insert into goods (ID, cate_id, name, price, description, order_no, update_time) values (# {ID}, # {cateid}, # {name}, # {price}, # {description}, # {orderno}, # {updatetime }) </Insert> <! -- Update goods parameter type to goods --> <update id = "updategood" parametertype = "goods"> Update goods G set G. name = # {name}, G. order_no =#{ orderno} Where G. id = # {ID} </update> <! -- Delete the goods parameter type as int --> <Delete id = "deletegood" parametertype = "int"> Delete from goods g where G. id = # {ID} </delete> </mapper>

package com.clark.model;import java.util.Date;public class Goods {private Integer id;private Integer cateId;private String name;private double price;private String description;private Integer orderNo;private Date updateTime;public Goods(){}public Goods(Integer id, Integer cateId, String name, double price,String description, Integer 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(double price) {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 setTimeStamp(Date updateTime) {this.updateTime = updateTime;}@Overridepublic String toString() {return "[goods include:Id="+this.getId()+",name="+this.getName()+",orderNo="+this.getOrderNo()+",cateId="+this.getCateId()+",updateTime="+this.getTimeStamp()+"]";}}
Package COM. clark. model; // a simulated pagebeanpublic class pagebean {// start count private integer startnumber; // end count 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 ;}}
Package COM. clark. mybatis; import Java. io. ioexception; import Java. io. reader; import Java. util. hashmap; import Java. util. list; import 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; import COM. clark. model. goods; import COM. clark. model. pagebean; public class testgoods {public static void main (string [] ARGs) throws ioexception {string resource = "configuration. XML "; reader = NULL; sqlsessionfactory sessionfactory = NULL; sqlsession session = NULL; try {reader = resources. getresourceasreader (Resource); sessionfactory = new sqlsessionfactorybuilder (). build (Reader); Session = sessionfactory. opensession (); 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 ();} finally {session. close (); reader. close () ;}// find by idpublic static goods findgoodbyid (sqlsession session, integer ID) {// Clark corresponds to goodmapper. namespace name = "Clark" goods Goods = (goods) session in the XML configuration file. selectone ("Clark. selectgoodbyid ", ID); return goods;} // find allpublic static list <goods> findallgoods (sqlsession session) {list <goods> goods = session. selectlist ("Clark. selectallgoods "); return goods;} public static list <goods> findgoodsbypage (sqlsession session, Map <string, pagebean> map) {list <goods> goods = session. selectlist ("Clark. selectgoodsbypage ", MAP); return goods;} // insert a goodspublic static int insertgoods (sqlsession session, goods Goods) {int result = session. insert ("Clark. insertgood ", goods); Session. commit (); return result;} // update goodspublic static int updategoods (sqlsession session, goods Goods) {int result = session. update ("Clark. updategood ", goods); Session. commit (); return result;} // Delete goodspublic static int deletegood (sqlsession session, integer ID) {int result = session. delete ("Clark. deletegood ", ID); Session. commit (); return result ;}}





Simple addition, deletion, modification, and query of mybatis and simple paging query implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.