【MyBatis】MyBatis带参数和使用对象参数查询
本文最后更新于 1739 天前,其中的信息可能已经有所发展或是发生改变。

[successbox title=”准备前提”]

这里我们直接用之前MyBatis项目的goods实体类直接作为演示,源代码中直接再test.java调试各个方法就行。

[/successbox]

一.只有一个参数查询。
1.增加接口。
[sourcecode language=”java” title=”GoodsMapper.java”]
public Goods findById(int id);
[/sourcecode]
2.对应的xml文件添加内容。
[sourcecode language=”java” title=”GoodsMapper.xml”]
<select id="findById" parameterType="int" resultMap="resultGoods">
select * from goods
where goods_id = #{id}
</select>
[/sourcecode]
3.测试方法。
[sourcecode language=”java” title=”test.java”]
public void test04() {
try {
GoodsMapper goodsMapper = this.sqlSession.getMapper(GoodsMapper.class);
Goods goods = goodsMapper.findById(2);
System.out.println(goods);
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}
[/sourcecode]
二.多参数查询。
1.增加接口。
[sourcecode language=”java” title=”GoodsMapper.java”]
public List<Goods> findByPrice(@Param("minPrice") double minPrice, @Param("maxPrice") double maxPrice);
[/sourcecode]
2.对应的xml文件添加内容。
[sourcecode language=”java” title=”GoodsMapper.xml”]
<select id="findByPrice" resultMap="resultGoods">
select * from goods
where goods_price between #{minPrice} and #{maxPrice}
</select>
[/sourcecode]
3.测试方法。
[sourcecode language=”java” title=”test.java”]
public void test05(){
try {
GoodsMapper goodsMapper = this.sqlSession.getMapper(GoodsMapper.class);
List<Goods> goodsList = goodsMapper.findByPrice(0, 4);
for (Goods goods : goodsList) {
System.out.println(goods);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}
[/sourcecode]

三.使用对象作为参数写入数据。
1.增加接口。
[sourcecode language=”java” title=”GoodsMapper.java”]
public void save(Goods goods);
[/sourcecode]
2.对应的xml文件添加内容。
[sourcecode language=”java” title=”GoodsMapper.xml”]
<insert id="save" parameterType="com.bean.Goods">
insert into goods(goods_id,goods_name,goods_price,goods_num,goods_type)
value(#{goodsId},#{goodsName},#{goodsPrice},#{goodsNum},#{goodsType})
</insert>
[/sourcecode]
3.测试方法。
[sourcecode language=”java” title=”test.java”]
public void test06(){
try{
GoodsMapper goodsMapper = this.sqlSession.getMapper(GoodsMapper.class);
Goods goods = new Goods();
goods.setGoodsName("篮球");
goods.setGoodsPrice(80.0);
goods.setGoodsNum(5);
goods.setGoodsType(1);
goodsMapper.save(goods);
this.sqlSession.commit(); //提交事务
System.out.println("添加数据成功!");
} catch(Exception e) {
e.printStackTrace();
this.sqlSession.rollback();
throw new RuntimeException(e);
} finally {
this.sqlSession.close();
}
}
[/sourcecode]

四.使用对象作为多条件查询。
1.创建对象类。
[sourcecode language=”java” title=”com/vo/GoodsSearchVO.java”]
package com.vo;

public class GoodsSearchVO {

//封装实体类进行查询。
private String goodsName;
private double minPrice;
private double maxPrice;

public GoodsSearchVO() {
}

public GoodsSearchVO(String goodsName, double minPrice, double maxPrice) {
this.goodsName = goodsName;
this.minPrice = minPrice;
this.maxPrice = maxPrice;
}

public String getGoodsName() {
return goodsName;
}

public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}

public double getMinPrice() {
return minPrice;
}

public void setMinPrice(double minPrice) {
this.minPrice = minPrice;
}

public double getMaxPrice() {
return maxPrice;
}

public void setMaxPrice(double maxPrice) {
this.maxPrice = maxPrice;
}

@Override
public String toString() {
return "GoodsSearchVO [goodsName=" + goodsName + ", minPrice="
+ minPrice + ", maxPrice=" + maxPrice + "]";
}

}
[/sourcecode]
2.增加接口。
[sourcecode language=”java” title=”GoodsMapper.java”]
public List<Goods> findBySearchVO(GoodsSearchVO goodsSearchVO);
[/sourcecode]
3.对应的xml文件添加内容。
[sourcecode language=”java” title=”GoodsMapper.xml”]
<select id="findBySearchVO" parameterType="com.vo.GoodsSearchVO" resultMap="resultGoods">
select * from goods
where goods_name like concat(‘%’, concat(#{goodsName}, ‘%’))
and goods_price between #{minPrice} and #{maxPrice}
</select>
[/sourcecode]
4.测试方法。
[sourcecode language=”java” title=”test.java”]
public void test07(){
try{
GoodsMapper goodsMapper = this.sqlSession.getMapper(GoodsMapper.class);
GoodsSearchVO goodsSearchVO = new GoodsSearchVO("球",10,100);
//goodsSearchVO.setGoodsName("球");
//goodsSearchVO.setMaxPrice(100);
//goodsSearchVO.setMinPrice(10);
List<Goods> goodsList = goodsMapper.findBySearchVO(goodsSearchVO);
for (Goods goods : goodsList) {
System.out.println(goods);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}
[/sourcecode]

五.使用对象封装聚合查询的结果。
1.创建对象类。
[sourcecode language=”java” title=”com/vo/GoodsGroupVO.java”]
package com.vo;

public class GoodsGroupVO {
//查询结果封装的实体类
private double minPrice;
private double maxPrice;

public GoodsGroupVO() {
}

public GoodsGroupVO(double minPrice, double maxPrice) {
this.minPrice = minPrice;
this.maxPrice = maxPrice;
}

public double getMinPrice() {
return minPrice;
}

public void setMinPrice(double minPrice) {
this.minPrice = minPrice;
}

public double getMaxPrice() {
return maxPrice;
}

public void setMaxPrice(double maxPrice) {
this.maxPrice = maxPrice;
}

@Override
public String toString() {
return "GoodsGroupVO [minPrice=" + minPrice + ", maxPrice=" + maxPrice
+ "]";
}

}
[/sourcecode]
2.增加接口。
[sourcecode language=”java” title=”GoodsMapper.java”]
public GoodsGroupVO findByGroup();
[/sourcecode]
3.对应的xml文件添加内容。
[sourcecode language=”java” title=”GoodsMapper.xml”]
<select id="findByGroup" resultType="com.vo.GoodsGroupVO">
select
min(goods_price) as ‘minPrice’,
max(goods_price) as ‘maxPrice’
from goods
</select>
[/sourcecode]
4.测试方法。
[sourcecode language=”java” title=”test.java”]
public void test08(){
try{
GoodsMapper goodsMapper = this.sqlSession.getMapper(GoodsMapper.class);
GoodsGroupVO goodGroupVO = goodsMapper.findByGroup();
System.out.println(goodGroupVO);
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}
[/sourcecode]

源代码下载:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/源代码/MB004.rar[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇