Mybatis实现增删改查及分页查询的方法
MyBatis的前身就是iBatis。是一个数据持久层(ORM)框架。MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(PlanOldJavaObjects,普通的Java对象)映射成数据库中的记录。每个MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。
具体代码如下所示:
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <!--giveaaliasformodel--> <typeAliasalias="goods"type="com.clark.model.Goods"></typeAlias> </typeAliases> <environmentsdefault="development"> <environmentid="development"> <transactionManagertype="JDBC"/> <dataSourcetype="POOLED"> <propertyname="driver"value="oracle.jdbc.driver.OracleDriver"/> <propertyname="url"value="jdbc:oracle:thin:@172.30.0.125:1521:oradb01"/> <propertyname="username"value="settlement"/> <propertyname="password"value="settlement"/> </dataSource> </environment> </environments> <mappers> <mapperresource="com/clark/model/goodsMapper.xml"/> </mappers> </configuration> <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="clark"> <!--将db查询出来的结果映射到Model--Goods--> <resultMaptype="com.clark.model.Goods"id="t_good"> <idcolumn="id"property="id"/> <resultcolumn="cate_id"property="cateId"/> <resultcolumn="name"property="name"/> <resultcolumn="price"property="price"/> <resultcolumn="description"property="description"/> <resultcolumn="order_no"property="orderNo"/> <resultcolumn="update_time"property="updateTime"/> </resultMap> <!--根据id查询返回Goods类型<typeAliasalias="goods"type="com.clark.model.Goods"></typeAlias>--> <!--resultMap和resultType的使用区别--> <selectid="selectGoodById"parameterType="int"resultType="goods"> selectid,cate_id,name,price,description,order_no,update_time fromgoodswhereid=#{id} </select> <!--查询所有Goods返回resultMap类型--> <selectid="selectAllGoods"resultMap="t_good"> selectid,cate_id,name,price,description,order_no,update_timefromgoods </select> <!--指定parameterType=map其中map的形式为Map<String,PageBean>map--> <selectid="selectGoodsByPage"resultMap="t_good"parameterType="map"> <!--orderbyidasc是指对查询后的结果进行升序排序--> <![CDATA[ select*from (selectg.*,rownumrnfrom(select*fromgoods)gwhere1=1andrownum<=#{pageBean.endNumber}) wherern>=#{pageBean.startNumber} orderbyidasc ]]> </select> <!--新增Goods参数类型为Goods--> <insertid="insertGood"parameterType="goods"> insertintogoods(id,cate_id,name,price,description,order_no,update_time) values(#{id},#{cateId},#{name},#{price},#{description},#{orderNo},#{updateTime}) </insert> <!--更新Goods参数类型为Goods--> <updateid="updateGood"parameterType="goods"> updategoodsg setg.name=#{name},g.order_no=#{orderNo} whereg.id=#{id} </update> <!--删除Goods参数类型为int--> <deleteid="deleteGood"parameterType="int"> deletefromgoodsg whereg.id=#{id} </delete> </mapper> packagecom.clark.model; importjava.util.Date; publicclassGoods{ privateIntegerid; privateIntegercateId; privateStringname; privatedoubleprice; privateStringdescription; privateIntegerorderNo; privateDateupdateTime; publicGoods(){ } publicGoods(Integerid,IntegercateId,Stringname,doubleprice, Stringdescription,IntegerorderNo,DateupdateTime){ super(); this.id=id; this.cateId=cateId; this.name=name; this.price=price; this.description=description; this.orderNo=orderNo; this.updateTime=updateTime; } publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicIntegergetCateId(){ returncateId; } publicvoidsetCateId(IntegercateId){ this.cateId=cateId; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicdoublegetPrice(){ returnprice; } publicvoidsetPrice(doubleprice){ this.price=price; } publicStringgetDescription(){ returndescription; } publicvoidsetDescription(Stringdescription){ this.description=description; } publicIntegergetOrderNo(){ returnorderNo; } publicvoidsetOrderNo(IntegerorderNo){ this.orderNo=orderNo; } publicDategetTimeStamp(){ returnupdateTime; } publicvoidsetTimeStamp(DateupdateTime){ this.updateTime=updateTime; } @Override publicStringtoString(){ return"[goodsinclude:Id="+this.getId()+",name="+this.getName()+ ",orderNo="+this.getOrderNo()+",cateId="+this.getCateId()+ ",updateTime="+this.getTimeStamp()+"]"; } } packagecom.clark.model; //模拟的一个分页对象PageBean publicclassPageBean{ //开始数 privateIntegerstartNumber; //结束数 privateIntegerendNumber; publicPageBean(){ } publicPageBean(IntegerstartNumber,IntegerendNumber){ super(); this.startNumber=startNumber; this.endNumber=endNumber; } publicIntegergetStartNumber(){ returnstartNumber; } publicvoidsetStartNumber(IntegerstartNumber){ this.startNumber=startNumber; } publicIntegergetEndNumber(){ returnendNumber; } publicvoidsetEndNumber(IntegerendNumber){ this.endNumber=endNumber; } } packagecom.clark.mybatis; importjava.io.IOException; importjava.io.Reader; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importorg.apache.ibatis.io.Resources; importorg.apache.ibatis.session.SqlSession; importorg.apache.ibatis.session.SqlSessionFactory; importorg.apache.ibatis.session.SqlSessionFactoryBuilder; importcom.clark.model.Goods; importcom.clark.model.PageBean; publicclassTestGoods{ publicstaticvoidmain(String[]args)throwsIOException{ Stringresource="configuration.xml"; Readerreader=null; SqlSessionFactorysessionFactory=null; SqlSessionsession=null; try{ reader=Resources.getResourceAsReader(resource); sessionFactory=newSqlSessionFactoryBuilder().build(reader); session=sessionFactory.openSession(); PageBeanpageBean=newPageBean(8,20); Map<String,PageBean>map=newHashMap<String,PageBean>(); map.put("pageBean",pageBean); List<Goods>gs=findGoodsByPage(session,map); for(Goodsgoods2:gs){ System.out.println(goods2.toString()); } }catch(IOExceptione){ e.printStackTrace(); }finally{ session.close(); reader.close(); } } //findbyid publicstaticGoodsfindGoodById(SqlSessionsession,Integerid){ //clark对应着goodMapper.xml配置文件中的namespacename="clark" Goodsgoods=(Goods)session.selectOne("clark.selectGoodById",id); returngoods; } //findall publicstaticList<Goods>findAllGoods(SqlSessionsession){ List<Goods>goods=session.selectList("clark.selectAllGoods"); returngoods; } publicstaticList<Goods>findGoodsByPage(SqlSessionsession,Map<String,PageBean>map){ List<Goods>goods=session.selectList("clark.selectGoodsByPage",map); returngoods; } //insertagoods publicstaticintinsertGoods(SqlSessionsession,Goodsgoods){ intresult=session.insert("clark.insertGood",goods); session.commit(); returnresult; } //updategoods publicstaticintupdateGoods(SqlSessionsession,Goodsgoods){ intresult=session.update("clark.updateGood",goods); session.commit(); returnresult; } //deletegoods publicstaticintdeleteGood(SqlSessionsession,Integerid){ intresult=session.delete("clark.deleteGood",id); session.commit(); returnresult; } }
关于Mybatis实现增删改查及分页查询的方法的相关知识,就给大家介绍到这里,后续还会持续给大家更新,谢谢大家一直以来对毛票票网站的支持。