欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis实现增删改查及分页查询的方法

程序员文章站 2024-03-08 11:20:36
mybatis的前身就是ibatis。是一个数据持久层(orm)框架。 mybatis是支持普通sql查询,存储过程和高级映射的优秀持 久层框架。mybatis消除了几乎所...

mybatis的前身就是ibatis。是一个数据持久层(orm)框架。 mybatis是支持普通sql查询,存储过程和高级映射的优秀持 久层框架。mybatis消除了几乎所有的jdbc 代码和参数的手工 设置以及结果集的检索。mybatis使用简单的xml或注解用于 配置和原始映射,将接口和java 的pojos(plan old java objects,普通的java 对象)映射成数据库中的记录。每个 mybatis应用程序主要都是使用sqlsessionfactory实例的,一个 sqlsessionfactory实例可以通过sqlsessionfactorybuilder获得。

具体代码如下所示:

<?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//en" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="clark"> 
<!-- 将db查询出来的结果映射到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> 
<!-- 根据id查询 返回goods类型 <typealias alias="goods" type="com.clark.model.goods"></typealias>--> 
<!--resultmap 和 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> 
<!-- 查询所有goods 返回resultmap类型--> 
<select id="selectallgoods" resultmap="t_good"> 
select id,cate_id,name,price,description,order_no,update_time from goods 
</select> 
<!-- 指定parametertype=map 其中map的形式为map<string,pagebean> map--> 
<select id="selectgoodsbypage" resultmap="t_good" parametertype="map"> 
<!-- order by id asc是指对查询后的结果进行升序排序 --> 
<![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 参数类型为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> 
<!-- 更新goods 参数类型为goods--> 
<update id="updategood" parametertype="goods"> 
update goods g 
set g.name = #{name},g.order_no =#{orderno} 
where g.id = #{id} 
</update> 
<!-- 删除goods 参数类型为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; 
} 
@override 
public string tostring() { 
return "[goods include:id="+this.getid()+",name="+this.getname()+ 
",orderno="+this.getorderno()+",cateid="+this.getcateid()+ 
",updatetime="+this.gettimestamp()+"]"; 
} 
}

package com.clark.model; 
//模拟的一个分页对象pagebean 
public class pagebean { 
//开始数 
private integer startnumber; 
//结束数 
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 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(); 
}finally{ 
session.close(); 
reader.close(); 
} 
} 
//find by id 
public static goods findgoodbyid(sqlsession session,integer id){ 
//clark对应着goodmapper.xml配置文件中的namespace name="clark" 
goods goods = (goods)session.selectone("clark.selectgoodbyid", id); 
return goods; 
} 
//find all 
public 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 goods 
public static int insertgoods(sqlsession session,goods goods){ 
int result = session.insert("clark.insertgood", goods); 
session.commit(); 
return result; 
} 
//update goods 
public static int updategoods(sqlsession session,goods goods){ 
int result = session.update("clark.updategood", goods); 
session.commit(); 
return result; 
} 
//delete goods 
public static int deletegood(sqlsession session,integer id){ 
int result = session.delete("clark.deletegood", id); 
session.commit(); 
return result; 
} 
}

关于mybatis实现增删改查及分页查询的方法的相关知识,就给大家介绍到这里,后续还会持续给大家更新,谢谢大家一直以来对网站的支持。