① if 标签
<select id="find" parameterType="demo" resultType="demo">
select * from test
<!--
where 标签中 if 有成立的会再 sql 语句后面自动添加 where
-->
<where>
<!--
if 标签判断,条件成立后会添加标签体内容 and 底层处理 第一个自动去掉 and
-->
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
<if test="phone != null">
and phone = #{phone}
</if>
</where>
</select>
----------------------------------------------------------------------------------------------------------------
② forearch 标签
<select id="in" parameterType="demo" resultType="demo">
select * from test
<where>
<!--
传入一个容器,将其中数据遍历取出
-->
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
----------------------------------------------------------------------------------------------------------------
③ sql 抽取
<!-- 抽取 sql -->
<sql id="sqlDemo" >select * from test</sql>
<select id="find" parameterType="demo" resultType="demo">
<!-- 使用抽取的 sql -->
<include refid="sqlDemo"></include>
</select>
① 一对一
<!--
封装为 map
id : 唯一标识
type : 类的全限定名
-->
<resultMap id="demoMap" type="user" >
<!--
手动指定 字段 与 属性之间的对应关系
property : 属性
column : 字段
-->
<!-- 普通属性 -->
<result property="id" column="uid"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<!-- 引用对象 -->
//方法一:
<result property="role.id" column="rid"/>
<result property="role.desc" column="desc"/>
//方法二:
<!--
property : 属性
javaType : 属性类型
-->
<association property="role" javaType="role">
<result property="id" column="rid" />
<result property="desc" column="desc" />
</association>
</resultMap>
<select id="find" resultMap="demoMap">
select *,u.id uid from user u, role r where u.rid = r.id
</select>
----------------------------------------------------------------------------------------------------------------
② 一对多
<resultMap id="demoMap" type="user" >
<result property="id" column="uid"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<collection property="role" ofType="role">
<result property="id" column="rid" />
<result property="desc" column="desc" />
</collection>
</resultMap>
<select id="find" resultMap="demoMap">
select *,u.id uid from user u left join role r on r.id = u.rid
</select>
----------------------------------------------------------------------------------------------------------------
③ 多对多
<resultMap id="demoMap" type="user" >
<result property="id" column="uid"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<collection property="role" ofType="role">
<result property="id" column="rid" />
<result property="desc" column="desc" />
</collection>
</resultMap>
<select id="find" resultMap="demoMap">
select r.*,u.*,u.id uid from user u left join user_role ur on u.id = ur.uid join role r on ur.rid = r.id
</select>
----------------------------------------------------------------------------------------------------------------
④ 注意
- 一对多 与 多对多 的区别在于查询语句
- 常用的多表查询为 左外 select * from 表1 left [outer] join 表2 on 查询条件
① 写一个类继承 BaseTypeHandler<T> 并复写其中的四个方法
/*
T 为需要转换的类型
一般用于 Date 存入 毫秒值 到数据库
*/
public class MyTypeHandle extends BaseTypeHandler<Date> {
/*
Date ---> Long
PreparedStatement 执行对象
i 字段的位置
data 需要转换的对象
*/
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType)
throws SQLException {
preparedStatement.setString(i,date.getTime() + "");
}
/*
resultSet 结果集
s 需要转换的字段字段
*/
public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
return new Date(resultSet.getLong(s));
}
/*
resultSet 结果集
i 字段的位置
*/
public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
return new Date(resultSet.getLong(i));
}
/*
callableStatement 执行函数
i 字段的位置
*/
public Date getNullableResult(CallableStatement callableStatement, int i)
throws SQLException {
return new Date(callableStatement.getLong(i));
}
}
----------------------------------------------------------------------------------------------------------------
② 注册
<!-- handler 为自定义类的全限定名 -->
<typeHandlers>
<typeHandler handler="com.softwareMan.typeHandle.MyTypeHandle" />
</typeHandlers>
① 导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>1.0</version>
</dependency>
----------------------------------------------------------------------------------------------------------------
② 配置插件【Mybatis.xml】
<plugins>
<!--
PageHelper 4.0 以前配置
需要指定为 PageHelper 并指定定方言
-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
<!--
PageHelper 4.0 以后配置
需要指定为 PageInterceptor 不需要指定方言
-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
----------------------------------------------------------------------------------------------------------------
③ 使用
public void TestC() {
/*
第一个参数为 当前页码;
第二个参数为 每页显示条数;
# 注意
一定要在执行查询之前设置,否则不生效
*/
PageHelper.startPage(1,4);
//执行查询 此处得到 lists 已经不是原来的 List 而是 PagesProxy 代理对象
List<Demo> lists = mapper.find();
//遍历输出
for (Demo list : lists) {
System.out.println(list);
}
// 获取分页信息对象
PageInfo<Demo> info = new PageInfo<Demo>(lists);
//获取分页信息方法
System.out.println("当前页:" + info.getPageNum());
System.out.println("总页数:" + info.getPages());
System.out.println("总条数:" + info.getTotal());
System.out.println("上一页:" + info.getPrePage());
System.out.println("下一页:" + info.getNextPage());
System.out.println("是否首页:" + info.isIsFirstPage());
System.out.println("是否末页:" + info.isIsLastPage());
System.out.println("每页显示条数:" + info.getPageSize());
}