2019-8-30 大纲
1.动态sql语句
2.逆向工程(generator)
3.分页助手(pagehelper)
1.动态sql语句(即sql语句的拼接)
1.1 if标签(属性:test(判断条件))
缺点:如果仅仅是第二个条件满足导致sql语句出错(使用if+where)
<select id="select1" resultType="com.zhiyou.clg.bean.User">
select *from user
<if test="name!=null">
where name=#{name}
</if>
<if test="age!=null and age!=‘‘">
and age=#{age}
</if>
</select>
1.2 if+where ( where标签会知道如果它包含的标签中有返回值的话,就会插入where 若其以and或or开头会将and或or剔除)
<select id="select2" resultType="com.zhiyou.clg.bean.User">
select *from user
<where>
<if test="name!=null">
and name=#{name}
</if>
<if test="sex!=null">
and sex=#{sex}
</if>
<if test="age!=null and age!=‘‘">
and age=#{age}
</if>
</where>
</select>
1.3 if+set( set标签会知道如果它包含的标签中有返回值的话,就会插入set并且剔除最后一个满足条件的逗号“,”)
<update id="update1" parameterType="com.zhiyou.clg.bean.User">
update user
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="sex!=null">
sex=#{sex},
</if>
<if test="age!=null and age!=‘‘">
age=#{age},
</if>
</set>
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</update>
1.4 choose(标签when+标签otherwise)----类似于switch语句,有且仅有一个条件会满足
<sql id="usercolumn" >
id,name,age,sex
</sql>
<select id="select3" resultType="com.zhiyou.clg.bean.User">
select
<include refid="usercolumn"></include>
from user
<where>
<choose>
<when test="name!=null">
and name=#{name}
</when>
<when test="age!=null and age!=‘‘">
and age=#{age}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
1.5 trim(trim标记是一个格式化的标记,可以完成set或者是where标记的功能;)
属性:prefix:前缀
prefixoverrides:去掉前缀
suffix:后缀
suffixoverrides:去掉后缀
<update id="update2" parameterType="com.zhiyou.clg.bean.User">
update user
<trim prefix="set" suffixOverrides=",">
<if test="name!=null">
name=#{name},
</if>
<if test="sex!=null">
sex=#{sex},
</if>
<if test="age!=null and age!=‘‘">
age=#{age},
</if>
</trim>
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</update>
1.6 sql片段------使用sql标签定义(属性:id);引用片段时用include标签(属性:refid)
1.7foreach
属性: collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
<sql id="namecolumn" >
name
</sql>
<select id="select4" resultType="com.zhiyou.clg.bean.User">
select
<include refid="namecolumn"></include>
from user where id in
<foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</select>
1.8模糊查询-----like后使用concat函数拼接(‘%’,#{name},’%’)
name like concat(‘%’,#{name},’%’)
2逆向工程(generator)---由表帮我们来生成dao、bean、xml
*引入mybatis-generator的jar包
*在工程目录下新建generator.xml文件(复制官网内容)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动jar包所在的位置 -->
<classPathEntry location="D:\\mybatis\\generator830\\lib\\mysql-connector-java-5.1.47.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据源信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成的实体类所在的位置 -->
<javaModelGenerator targetPackage="com.zhiyou.clg.bean" targetProject="./src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件所在的位置 -->
<sqlMapGenerator targetPackage="com.zhiyou.clg.mapper" targetProject="./resource">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成的dao所在的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou.clg.dao" targetProject="./src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 表和实体类的对象关系
schema:该表所在的数据库
tableName:表名
domainObjectName:实体类名
-->
<table schema="DB2ADMIN" tableName="user" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
*在src目录下新建test包,再新建Test类,在main下复制官网内容,导包后运行
package com.zhiyou.clg.test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class TestGenerator {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
3. 分页助手(pageHelper)
*引入jar包 jsqlparser-2.0.jar和pagehelper-5.1.10.jar
*测试时:(参考官网)
@Test
void testselectall() {
//1.使用PageHelper类设置起始页和每页显示的条数
int pageNum=1;//当前页码 从网页中可以获取
int pageSize=2;//pageSize:自定义
PageHelper.startPage(pageNum,pageSize);
//2调用查询所有的方法
List<User> list=userMapper.selectall();
//3.把查询的结果封装到Pageinfo中
PageInfo<User> pageinfo=new PageInfo<>(list);
System.out.println(pageinfo);
}
原文地址:https://www.cnblogs.com/jingmochen/p/11437698.html