动态SQL
<if>标签
//if标签
select 字段,字段 from 表名
<if test="进行参数判断">
拼接的内容
</if>
//单独使用容易出现bug,一般配合<where>标签使用
<select id="findUser" parameterType="Map" resultType="com.yunhe.domain.Users">
select * from users where
<if test="username!=null">
username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</select>
<choose>标签
<choose>
<when test="参数判断">
拼接内容
</when>
<when test="参数判断">
拼接内容
</when>
<when test="参数判断">
拼接内容
</when>
<otherwise>
以上条件都不满足时,拼接的内容
</otherwise>
</choose>
<where>标签
//where标签代替SQL语句中的where关键字,使用where标签可以避免拼接出现and , or 等关键字造成sql语句错误
<select id="findUser" parameterType="Map" resultType="com.yunhe.domain.Users">
select * from users
<where>
<if test="username!=null">
username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</where>
</select>
<set> 标签
//<set>标签替代set关键字,可以避免,等引起的拼接错误
<update id="updateByNameOrAge">
update tb_user
<set>
<if test="name != null">
user_name = #{name},
</if>
<if test="age != null">
age = #{age}
</if>
</set>
where id = #{id}
</update>
<foreach>标签
//该标签配合in关键字使用,但是in关键字存在bug,一般不使用。
<select id="queryIds" resultType="User">
select * from tb_user where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
多参数传入
/**
* 登录
*/
//使用@Param("参数名称")
public User login(@Param("name") String name ,@Param("passworld") String passworld);
<!--登录-->
<select id="login" resultType="User">
<!--使用#-->
select * from tb_user where user_name = #{参数名} and passworld = #{参数名}
</select>
分页插件
实现步骤
//添加依赖
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
//配置插件
<!--分页插件-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!--数据库方言-->
<property name="dialect" value="mysql"/>
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
</plugins>
//实现分页 参数1:当前页(从1开始) 参数2:显示多少条
PageHelper.startPage(5, 10);
List<Books> books1 = bookDao.findBooks();
for (Books books2 : books1) {
System.out.println(books2);
}
//获取更多的分页信息
PageInfo<Books> pageInfo = new PageInfo<Books>(books1);
//当前页码
System.out.println("当前页:" + pageInfo.getPageNum());
//上一页
System.out.println("上一页"+pageInfo.getPrePage());
//下一页
System.out.println("下一页"+pageInfo.getNextPage());
//当前展示条数
System.out.println("当前页显示的条数:" + pageInfo.getPageSize());
//分页总共页数
System.out.println("总页码:" + pageInfo.getPages());
//尾页
System.out.println("最后一页:" + pageInfo.getLastPage());
System.out.println("分页相关的信息:" + pageInfo.getList());
//总数
System.out.println("分页总条数:" + pageInfo.getTotal());
多表查询
一对一
//实体类中设置一对一的对象属性
private Account account;
//查询结果设置
<!--一对一-->
<resultMap id="结果名称" type="包名.类名" autoMapping="true">
<id column="主键字段" property="实体类对应字段属性"/>
<association property="实体类 属性名" javaType="包名.类名" autoMapping="true">
<id column="主键字段" property="主键字段对应属性"/>
</association>
</resultMap>
<select id="findAccount" resultMap="上面的结果名称">
select * from account,idCard where account.acc_id = idCard.acc_id;
</select>
一对多
//实体类中定义的关联表类使用list声明
private List<Shi> shiList;
//配置结果形式
<resultMap id="结果名称" type="com.yunhe.entity.Sheng(包名)" autoMapping="true">
<id column="数据库表主键字段" property="实体类主键字段对应属性名"/>
<collection property="shiList(定义的属性名)" javaType="java.util.List(报名.类型名)" ofType="com.yunhe.entity.Shi(设置泛型)" autoMapping="true">
<id column="数据库表主键字段" property="实体类主键字段对应属性名"/>
</collection>
</resultMap>
<!--一对多,查询省份下的所有市-->
<select id="findSheng" resultMap="shengshi">
select * from sheng , shi where sheng.pid = shi.pid;
</select>