java中的接口是类吗
197
2023-07-23
Mybatis实现增删改查及分页查询的方法
MyBatis的前身就是iBatis。是一个数据持久层(ORM)框架。 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持 久层框架。MyBatis消除了几乎所有的JDBC 代码和参数的手工 设置以及结果集的检索。MyBatis使用简单的XML或注解用于 配置和原始映射,将接口和java 的POJOs(Plan Old Java Objects,普通的Java 对象)映射成数据库中的记录。每个 MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个 SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。
具体代码如下所示:
"http://mybatis.org/dtd/mybatis-3-config.dtd">
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select id,cate_id,name,price,description,order_no,update_time
from goods where id = #{id}
select id,cate_id,name,price,description,order_no,update_time from goods
<![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
]]>
insert into goods(id,cate_id,name,price,description,order_no,update_time)
values(#{id},#{cateId},#{name},#{price},#{description},#{orderNo},#{updateTime})
update goods g
set g.name = #{name},g.order_no =#{orderNo}
where g.id = #{id}
delete from goods g
where g.id = #{id}
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 ordhttp://erNo, 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
map.put("pageBean", pageBean);
List
for (Goods goods2 : gs) {
System.out.println(goods2.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally{
session.close();
reader.close();
}
}
//find by id
phttp://ublic 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
List
return goods;
}
public static List
List
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实现增删改查及分页查询的方法的相关知识,就给大家介绍到这里,后续还会持续给大家更新,谢谢大家一直以来对我们网站的支持。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~