当前位置: 首页 > 编程笔记 >

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

阮鸿煊
2023-03-14
本文向大家介绍Mybatis实现增删改查及分页查询的方法,包括了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实现增删改查及分页查询的方法的相关知识,就给大家介绍到这里,后续还会持续给大家更新,谢谢大家一直以来对小牛知识库网站的支持。

 类似资料:
  • 本文向大家介绍mybatis分页及模糊查询功能实现,包括了mybatis分页及模糊查询功能实现的使用技巧和注意事项,需要的朋友参考一下 mybatis中分页有3种方式来实现,通过sql语句(两种传参方式)来实现,通过mybatis 的 Rowbounds 来实现。 通过(自定义类型)传参 来实现分页: 映射文件: 测试代码: 通过map传参实现: 映射文件: 测试代码: 通过RowBounds来实

  • 本文向大家介绍MVC+jQuery.Ajax异步实现增删改查和分页,包括了MVC+jQuery.Ajax异步实现增删改查和分页的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了MVC+jQuery.Ajax异步实现增删改查和分页的具体代码,供大家参考,具体内容如下 1、Model层代码 2、View层代码 3、Controller层代码 4、两个分页辅助类PagedList和MikeP

  • 本文向大家介绍Mybatis 条件查询 批量增删改查功能,包括了Mybatis 条件查询 批量增删改查功能的使用技巧和注意事项,需要的朋友参考一下 模糊查询: 批量添加: 批量删除: 批量修改: 批量查询: 条件查询,if里面不仅可以判空,还可以判断是否满足某个条件 条件查询: 以上所述是小编给大家介绍的Mybatis 条件查询 批量增删改查功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,

  • 一、前言 在第二章《Memcached源码分析 - Memcached源码分析之命令解析(2)》 和第三章《Memcached源码分析 - Memcached源码分析之消息回应(3)》 中我们主要通过Memcached的get命令,分析了Memcached的命令解析和消息回应的模块功能。这一章,我们主要来详细看一下Memcached常用的增删改查操作。 在看Memcached的增删改查操作前,我们

  • 本文向大家介绍java操作mysql实现增删改查的方法,包括了java操作mysql实现增删改查的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了java操作mysql实现增删改查的方法。分享给大家供大家参考,具体如下: 首先,需要把MySQL与Java连接的jar(mysql-connector-java-5.1.6-bin.jar)包导入工程. notice: 1、现在一般用的驱动

  • 本文向大家介绍MyBatis增删改查快速上手,包括了MyBatis增删改查快速上手的使用技巧和注意事项,需要的朋友参考一下 作为一个快乐的小码农,在每一个阶段往往都在重复写着不同版本的,学生管理,用户管理,注册登录,从 JavaSE 的控制台版,或者 GUI 版,再到 JavaWeb的 JSP版,再到纯粹使用 HTML 作为前端展示的版本,以及使用一个更新的技术,在此其中,我们用过 txt 做数据