if进行判断,test的内容是表达式,使用like进行模糊查询,%作为占位符,
进
行
拼
接
查
询
,
c
o
n
c
a
t
(
)
表
示
字
符
串
拼
接
,
{}进行拼接查询,concat()表示字符串拼接,
进行拼接查询,concat()表示字符串拼接,{}无法被识别为字符串,因此要加单引号,#{}可以被识别为字符串,无需加单引号。由于在mapper文件中<会被识别成一个标签的开始,因此我们的< <= 是没有办法直接写的,因此需要转译:
select * from t_role where id > #{id}
and name like "%${name}%"
select * from t_role where id>#{id}
and name like concat('%','${name}','%')
select * from t_role where id#{id}
and name like concat('%',#{name},'%')
public class User {
private Integer id;
private String account;
private String password;
private Integer type;
private String fileName;
private Role role;
}
public class Role {
private Integer id;
private String name;
private Integer userid;
private Date operTime;
}
(2)创建对应的mapper接口。
(3)创建对应的Mapper.xml文件。
sql语句
一对多关联查询
使用resultMap。
public class User {
private Integer id;
private String account;
private String password;
private Integer type;
private String fileName;
private List roles;
}
public class Role {
private Integer id;
private String name;
private Integer userid;
private Date operTime;
}
SELECT
u.id as uid,
u.account as uaccount,
u.PASSWORD as upassword,
u.type as utype,
u.file_name as ufilename,
r.id as rid,
r.NAME as rname,
r.userid as ruserid,
r.oper_time as ropertime
FROM
t_user AS u
INNER JOIN user_role AS ur ON u.id = ur.userid
INNER JOIN t_role AS r ON ur.roleid = r.id
WHERe
u.id = #{userId}
多对多关联查询
准备表
--用户表
CREATE table users(
uid int(4) not null,
uname VARCHAR(20) DEFAULT null,
sex VARCHAR(3) DEFAULT null,
birthday date default null,
address VARCHAR(100) default null,
PRIMARY Key(uid)
)
--订单表
create table orders(
oid int(4) not null,
userid int(4) default null,
orderid VARCHAR(20) DEFAULT null,
createtime date default null,
status varchar(10) default null,
primary key(oid)
)
--订单详情表
create table orderdetail(
odid int(4) not null PRIMARY key,
orderid VARCHAR(20) DEFAULT null,
itemid int(4) DEFAULT null,
itemnum int(4) DEFAULT null
)
--商品表
CREATE TABLE items(
iid int(4) not null,
name VARCHAR(30) DEFAULT NULL,
detail VARCHAR(50) DEFAULT NULL,
price DOUBLE(5,2) DEFAULT NULL,
PRIMARY KEY(iid)
)
问题:获得所有用户的所有订单里的所有商品信息!
(1)创建四张表对应的四个实体类。
(2)创建对应的mapper接口。
(3)创建对应的mapper.xml文件。
SELECT
u.uid,
u.uname,
u.sex,
u.birthday,
u.address,
o.oid,
o.orderid,
o.createtime,
o.STATUS,
od.odid,
od.itemnum,
i.iid,
i.NAME,
i.detail,
i.price
FROM
users AS u
INNER JOIN orders AS o ON u.uid = o.userid
INNER JOIN orderdetail AS od ON o.orderid = od.orderid
INNER JOIN items AS i ON od.itemid = i.iid
WHERe
u.uid = #{uid}
分页查询
方案一:使用Page工具类
创建util工具类page
public class Page {
//获得表里一共有多少条数据
private Integer dataCount;
//每页显示多少条数据
private Integer showData;
//一共分成多少页
private Integer pageCount;
//当前是第几页
private Integer pageIndex;
//当前页面显示的集合信息
private List list;
public Integer getDataCount() {
return dataCount;
}
public void setDataCount(Integer dataCount) {
this.dataCount = dataCount;
}
public Integer getShowData() {
return showData;
}
public void setShowData(Integer showData) {
this.showData = showData;
}
public Integer getPageCount() {
//数据库的总条数如果能除尽每页显示的条数就直接赋值,若不能则除数取整+1
return this.pageCount = this.dataCount%this.showData==0 ? this.dataCount/this.showData:this.dataCount/this.showData+1;
}
public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
创建接口方法,获取数据库总条数和第x列的数据集合
//分页获得一共有多少条数据
int selectCount() throws Exception;
//获得当前页显示的集合信息
List selectUsersByPage(Page page) throws Exception;
创建两个方法对应的xml文件
select count(uid) from users;
select * from users order by uid limit #{beginIndex},#{showData}
调用方法进行使用
public class test {
private SqlSessionFactory factory;
@Before
public void init() throws IOException {
InputStream res = Resources.getResourceAsStream("sqlMapperConfig.xml");
SqlSessionFactoryBuilder fac = new SqlSessionFactoryBuilder();
factory = fac.build(res);
}
@Test
public void test2() throws Exception {
SqlSession sqlSession = factory.openSession();
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
Page page = new Page<>();
//一共有多少条数据
int dataCount = mapper.selectCount();
System.out.println(dataCount);
page.setDataCount(dataCount);
page.setShowData(2);
page.setPageIndex(1);
//获取对应页的数据集合
List users = mapper.selectUsersByPage(page);
System.out.println(users);
page.setList(users);
sqlSession.close();
for (Users user : page.getList()) {
System.out.println(users);
}
}
}
方案二:使用RowBounds插件
创建实体类.创建接口,参数使用RowBounds
//RowBounds插件,分页插件
List selectItems(RowBounds rb) throws Exception;
创建对应的xml文件
select * from items;
测试使用
@Test
public void test3() throws Exception {
SqlSession sqlSession = factory.openSession();
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
//new RowBounds(从第几个下标开始查,查多少条数据);
RowBounds rb = new RowBounds(3,2);
List items = mapper.selectItems(rb);
sqlSession.close();
System.out.println(items);
}