MyBatis简单的增删改查以及简单的分页查询实现[通俗易懂]

MyBatis简单的增删改查以及简单的分页查询实现

大家好,又见面了,我是全栈君。

MyBatis简单的增删改查以及简单的分页查询实现

<?

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;
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116205.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • mysql之group_concat函数详解[通俗易懂]

    mysql之group_concat函数详解[通俗易懂]函数语法:group_concat([DISTINCT]要连接的字段[OrderBY排序字段ASC/DESC][Separator‘分隔符’])下面举例说明:selectid,pricefromgoods;以id分组,把price字段的值在同一行打印出来,逗号分隔(默认)selectid,group_concat(price…

  • 面试基础知识整理

    面试基础知识整理写在前面:3月伊始便经历了几次笔面试,深深感到自己知识储备的不足,痛下决心,在期末考试前一周,着手整理基础知识,希望可以对接下来的笔面试以及秋招有所帮助。本系列文章涉及数据结构及算法均使用Java语言描述。本文系基础知识整理,文章内容多来源于各经典书籍。本系列文章不定期更新,希望我可以有毅力完成这一工作。1.数据结构数组链表栈队列数图堆2.

  • 异步传输模式atm采用_什么是云主机

    异步传输模式atm采用_什么是云主机异步传输模式(ATM)也称为信元中继(在固定大小的信元中传输数据),它通过光纤或双绞线电缆(高速交换)在OSI模型的数据链路层(第2层)运行一种基于ITU-T宽带综合业务数字网(B-ISDN)标准的网络技术,该标准是由电信业开发的,用于实现下一代网络。ATM专为在WAN(如公用电话系统和公司数据网络)中使用而设计,尽管它也已被用于创建超高速LAN。ATM可以同时传输各种流量:语音,视频和数据,速度高达每秒155兆比特。它将语音,视频数据转换为数据包,并通过相同的介质传递大数据包数据。ATM与TCP/.

  • Kafka容错性测试

    Kafka容错性测试1.查看topic信息(“副本”是为该分区复制日志的节点列表,无论它们是引导者还是当前处于活动状态,“isr”是“同步”副本的集合。这是副本列表的子集,当前仍处于活动状态并追随领导者。)[hadoop@hadoop000tmp]$kafka-topics.sh–describe–zookeeperhadoop000:2181–topicmy-replicated-topi…

  • Android屏幕适配之Autosize

    Android屏幕适配之Autosize一、简介Autosize今日头条屏幕适配方案终极版,一个极低成本的Android屏幕适配方案。二、使用1、引用implementation’me.jessyan:autosize:1.2.1’2、配置<meta-dataandroid:name=”design_width_in_dp”…

  • 麻将公式一定要背下来「大全」

    麻将公式一定要背下来「大全」麻将计算公式:一、可胡牌情况1:N*(ABC)+M*(AAA)+AA,N+M<=4.2:7*AA(又叫七对)二、倍数计算:一倍:杂色平胡,即:N*(ABC)+M*(AAA)+AA,1<=N<=4两倍:1:一砍,即手上或旁边(AAAA)2:大对子,即:M*(AAA)+AA,1<=M<=43:海底捞,即桌上最后一张牌自摸四倍:1:清一色,即所有麻将花色一样2:金钩钓,即AA3:小七对,即7*(AA)4:杠上花,即开杠后自摸注:以上

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号