准备工作:在昨天整合的代码中添加UserMapper接口和配置文件
public interface UserMapper {
List<User> queryUserByCondition(User user);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.linjiu.dao.UserMapper">
<select id="queryUserByCondition" resultType="com.linjiu.model.User" parameterType="com.linjiu.model.User">
</select>
</mapper>
格式
sql 语句
当条件成立的时候,会执行sql语句
if (条件){
sql 语句
}
案例
java代码
@Test
public void test02(){
User user = new User();
user.setPhone("120");
user.setAge(11);
List users = userMapper.queryUserByCondition(user);
System.out.println(users);
}
运行日志
DEBUG [main] - ==> Preparing: select id,username,password,age,phone from user where 1=1 and age=? and phone=?
DEBUG [main] - ==> Parameters: 11(Integer), 120(String)
格式
sql语句1
sql语句2
sql语句3
和我们java的if...else if ...else格式一样
当条件1成立,那么就不会执行后面的代码
案例
# 说明
- 标签里边的if 至少有一个成立,就会动态添加一个where,如果都不成立,不添加where
- 第一个if条件成立的,会自动去除连接符and 或者 or
案例
说明
说明: 动态添加了set字段,也会动态的去掉最后一个逗号
案例
update user
username=#{username},
password=#{password},
where id=#{id}
代码
@Test
public void test03(){
User user = new User();
user.setId(1);
user.setUsername("哥哥123");
user.setPassword("332211");
String message = userMapper.updateUser(user)>0?"成功":"失败";
System.out.println(message);
}
说明: 适用于 id in (x,x,x)
格式
循环遍历标签。适用于多个参数或者的关系。
获取参数
foreach>
案例
代码
@Test
public void test04(){
ArrayList ids = new ArrayList();
Collections.addAll(ids, 1, 2, 3, 4);
List users = userMapper.queryUsersByIds(ids);
System.out.println(users);
}
格式 pre- presay
格式
替换where
替换set
update user
password=#{password},
age=#{age},
where id=#{id}
格式
查询的所有字段
使用的时候
com.github.pagehelper
pagehelper
4.1.6
com.github.jsqlparser
jsqlparser
0.9.6
@Test
public void test02(){
User user = new User();
PageHelper.startPage(1, 3);
List users = userMapper.queryUsersByCondition(user);
PageInfo pageInfo = new PageInfo(users);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("每页显示长度:"+pageInfo.getPageSize());
System.out.println("是否第一页:"+pageInfo.isIsFirstPage());
System.out.println("是否最后一页:"+pageInfo.isIsLastPage());
List list = pageInfo.getList();
for (User user1 : list) {
System.out.println(user1);
}
}
# 步骤
- 建表 user 和 card
- 创建实体类
- 配置文件
- 测试
public class Card {
private int id;
private String num;
private User user;
}
CREATE TABLE card
(
id
int(11) NOT NULL AUTO_INCREMENT,
num
varchar(20) DEFAULT NULL,
uid
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
:配置数据库库字段和Java对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
:配置主键映射关系标签。
:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
:配置被包含对象的映射关系标签。
property 属性:被包含对象的变量名
javaType 属性:被包含对象的数据类型
@Test
public void test05(){
List<Card> allCard = cardMapper.findAllCard();
System.out.println(allCard);
}
package com.linjiu.model;
public class Student {
private int id;
private String name;
private int age;
}
public class Classes {
private int id;
private String name;
private List<Student> students;
}
CREATE TABLE classes
(
id
int(11) NOT NULL,
name
varchar(255) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE student
(
id
int(11) NOT NULL,
name
varchar(255) DEFAULT NULL,
age
int(11) DEFAULT NULL,
cid
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
private int id;
private String name;
private int age;
private List<Teacher> teachers;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Teacher {
private int id;
private String name;
private String sex;
private List<Student> students;
}
public interface StudentMapper {
List<Student> findAllStudent();
}
public interface StudentMapper {
List<Student> findAllStudent();
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.linjiu.dao.StudentMapper">
<resultMap id="student" type="com.linjiu.model.Student">
<id property="id" column="sid" />
<result property="name" column="sname"/>
<result property="age" column="sage"/>
<collection property="teachers" ofType="com.linjiu.model.Teacher">
<id property="id" column="tid" />
<result property="name" column="tname"/>
<result property="sex" column="tsex"/>
collection>
resultMap>
<select id="findAllStudent" resultMap="student">
select
s.id sid,s.name sname,s.age sage,
t.id tid,t.name tname,t.sex tsex
from
teacher t,student s,stu_teach st
where
t.id=st.tid and s.id=st.sid
select>
mapper>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.linjiu.dao.TeachertMapper">
<resultMap id="teacher" type="com.linjiu.model.Teacher">
<id property="id" column="tid" />
<result property="name" column="tname"/>
<result property="sex" column="tsex"/>
<collection property="students" ofType="com.linjiu.model.Student">
<id property="id" column="sid" />
<result property="name" column="sname"/>
<result property="age" column="sage"/>
collection>
resultMap>
<select id="findAllTeacher" resultMap="teacher">
select
t.id tid,t.name tname,t.sex tsex,
s.id sid,s.name sname,s.age sage
from
teacher t,student s,stu_teach st
where
t.id=st.tid and s.id=st.sid
select>
mapper>
@Autowired
private StudentMapper studentMapper;
@Autowired
private TeachertMapper teachertMapper;
@Test
public void test06(){
List<Student> allStudent = studentMapper.findAllStudent();
for (Student student : allStudent) {
String name = student.getName();
System.out.println(name);
for (Teacher teacher : student.getTeachers()) {
System.out.print("\t"+teacher.getName()+"-"+teacher.getSex()+"\t");
}
System.out.println();
}
}