Mybatis實現增刪改查及分頁查詢的方法

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

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實現增刪改查及分頁查詢的方法的相關知識,就給大家介紹到這裡,後續還會持續給大家更新,謝謝大家一直以來對指令碼之家網站的支援。

您可能感興趣的文章:

SpringMVC4 MyBatis3 SQL Server 2014整合教程(含增刪改查分頁)Spring boot mybatis Vue.js ElementUI 實現資料的增刪改查例項程式碼(二)Spring boot mybatis Vue.js ElementUI 實現資料的增刪改查例項程式碼(一)MyBatis入門之增刪改查 資料庫欄位和實體欄位不一致問題處理方法MyBatis中SqlSession實現增刪改查案例Mybatis增刪改查mapper檔案寫法詳解mybatis generator 配置 反向生成Entity簡單增刪改查(推薦)Mybatis實現增刪改查(CRUD)例項程式碼mybatis實現增刪改查_動力節點Java學院整理

相關文章

程式語言 最新文章