建表sql文:
use mybatistest1;
create table product_(
id int NOT NULL AUTO_INCREMENT,
name varchar(30) DEFAULT NULL,
price float DEFAULT 0,
cid int ,
PRIMARY KEY (id)
)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Product.java类
package com.mybatis.pojo;
//系统内由很多产品,但是不一定是全部都关联到需要查找的特定的订单
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
where标签配置Product.xml
<mapper namespace="com.mybatis.pojo">
<select id="listProduct" resultType="Product">
select p.*
from product_ p
<where>
<if test="nameKey!=null">
and name like concat('%',#{nameKey},'%')
if>
<if test="priceKey!=null">
and price > #{priceKey}
if>
where>
select>
mapper>
where标签测试testMybatis.java
package com.mybatis;
import java.awt.event.ItemEvent;
import java.io.IOException;
import java.io.InputStream;
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.mybatis.pojo.*;
public class testMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml"; //获取全局配置信息
InputStream inputStream = Resources.getResourceAsStream(resource); //全局配置信息装入输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //建立sql会话工厂,将输入流装入
SqlSession session=sqlSessionFactory.openSession(); //开启一个会话
listProduct(session);
session.commit(); //更新到数据库
session.close();
}
private static void listProduct(SqlSession session) {
List<Product> ps=session.selectList("listProduct");
for(Product p:ps) {
System.out.println(p);
}
System.out.println("模糊查询");
//新建一个map集合,其中key为String类型,value可以也为String类型
Map<String ,String> params=new HashMap<>();
params.put("nameKey", "a");
params.put("priceKey", "10"); //用map集合传入两个参数到listProduct
List<Product> ps2=session.selectList("listProduct",params);
for(Product p:ps2) {
System.out.println(p);
}
}
}
set标签配置Product.xml
<mapper namespace="com.mybatis.pojo">
<select id="listProduct" resultType="Product">
select p.*
from product_ p
<where>
<if test="nameKey!=null">
and name like concat('%',#{nameKey},'%')
if>
<if test="priceKey!=null">
and price > #{priceKey}
if>
where>
select>
<update id="updateProduct" parameterType="Product">
update product_
<set>
<if test="name!=null">name=#{name},if>
<if test="price!=null">price=#{price}if>
set>
where id=#{id}
update>
mapper>
set标签测试testMybatis.java
package com.mybatis;
import java.awt.event.ItemEvent;
import java.io.IOException;
import java.io.InputStream;
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.mybatis.pojo.*;
public class testMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml"; //获取全局配置信息
InputStream inputStream = Resources.getResourceAsStream(resource); //全局配置信息装入输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //建立sql会话工厂,将输入流装入
SqlSession session=sqlSessionFactory.openSession(); //开启一个会话
Product p=new Product();
p.setId(6);
p.setName("zhaotest");
p.setPrice(1000);
session.update("updateProduct",p);
listProduct(session);
//session.commit(); //更新到数据库
//session.close();
}
private static void listProduct(SqlSession session) {
List<Product> ps=session.selectList("listProduct");
for(Product p:ps) {
System.out.println(p);
}
System.out.println("模糊查询");
//新建一个map集合,其中key为String类型,value可以也为String类型
Map<String ,String> params=new HashMap<>();
params.put("nameKey", "zhao");
params.put("priceKey", "5"); //用map集合传入两个参数到listProduct的test属性,传入priceKey和nameKey
/*
Product p2=new Product();
p2.setId(1);
p2.setName("product a");
p2.setPrice(5); */
List<Product> ps2=session.selectList("listProduct",params);
for(Product p:ps2) {
System.out.println(p);
}
}
}
1、where标签功能:去除多余and和or
2、set标签功能:和where一样用于update操作
3、test属性传入的值可以是map集合或者类,如果4、传入的是类,需要加上parameterType=Product
5、set标签功能:可以去掉后缀,比如去掉sql语句之间的逗号
6、trim标签的功能:定制自己想要的功能:比如
定制where功能去掉前缀and和or
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
trim>
定制set功能去掉后缀逗号
<trim prefix="SET" suffixOverrides=",">
...
trim>