动态SQL
-
基于OGNL表达式
-
完成多条件查询等逻辑实现
-
用于实现动态SQL的元素主要有
-
if
-
trim
-
where
-
set
-
choose(when、otherwise)
-
foreach
-
if
- 改造查询用户信息列表的演示示例,增加查询条件 用户角色(根据角色id查询) 用户名称(模糊查询)
<resultMap id="BaseResultMap" type="com.zking.ssm.pojo.SysUser">
<id property="userId" column="user_id"></id>
<result property="userName" column="username"></result>
<result property="password" column="password"></result>
<result property="realName" column="realName"></result>
<result property="phone" column="phone"></result>
<result property="salt" column="salt"></result>
</resultMap>
<!--动态sql之if-->
<select id="list4" parameterType="sysUser" resultMap="BaseResultMap">
select * from sys_user where 1=1
<if test="userName!=null">
and username like concat('%',#{userName},'%')
</if>
<if test="locked!=null">
and locked =#{locked}
</if>
</select>
where
-
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
-
如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为:
<select id="list5" parameterType="sysUser" resultMap="BaseResultMap">
select * from sys_user
<where>
<if test="userName!=null">
and username like concat('%',#{userName},'%')
</if>
<if test="locked!=null">
and locked =#{locked}
</if>
</where>
</select>
where:自动加where,自动去掉第一个and
set
- 若某个参数为null,则不需要更新,保持数据库原值
-
这个例子中,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。
<update id="update2" parameterType="SysUser">
update sys_user
<set>
<if test="userName!=null">username=#{userName},</if>
<if test="password!=null">password=#{password},</if>
<if test="realName!=null">realName=#{realName},</if>
<if test="phone!=null">phone=#{phone},</if>
<if test="salt!=null">salt=#{salt},</if>
<if test="locked!=null">locked=#{locked},</if>
<if test="timeStamp!=null">create_datetime=#{timeStamp}</if>
</set>
where user_id=#{userId}
</update>
trim
-
属性 prefix suffix prefixOverrides suffixOverrides
-
更灵活地去除多余关键字
-
替代where和set
<select id="list6" parameterType="sysUser" resultMap="BaseResultMap">
select * from sys_user
<trim prefix="where" prefixOverrides="and">
<if test="userName!=null">
and username like concat('%',#{userName},'%')
</if>
<if test="locked!=null">
and locked =#{locked}
</if>
</trim>
</select>
使用if+trim替代if+set进行更新用户表数据
<!-- 第一种方法
仅仅只是用trim替换set,以及普通的删除多于的“,”-->
<update id="update3" parameterType="SysUser">
update sys_user
<trim prefix="set" suffixOverrides=",">
<if test="userName!=null">username=#{userName},</if>
<if test="password!=null">password=#{password},</if>
<if test="realName!=null">realName=#{realName},</if>
<if test="phone!=null">phone=#{phone},</if>
<if test="salt!=null">salt=#{salt},</if>
<if test="locked!=null">locked=#{locked},</if>
<if test="timeStamp!=null">create_datetime=#{timeStamp}</if>
</trim>
where user_id=#{userId}
</update>
<!-- 第二种方法
将判断条件用suffix进行拼接到末端-->
<update id ="modify" parameterType="User">
update smbms_user
<trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
<if test="userCode != null">userCode = #{userCode},</if>
<if test="userName!= null">userCode = #{userName },</if>
<if test="userPassword!= null">userPassword=#{userPassword },</if>
</trim>
</update>
foreach
-
迭代一个集合,通常用于in条件
-
属性
-
item(集合项) :声明可以在元素体内使用的集合项
-
index(索引):声明可以在元素体内使用的变量
-
collection:必须指定
-
list
-
array
-
map-key
-
open
-
separator
-
close
-
-
**提示**
你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
int delete2_foreach_array(Long[] userIds);
<!-- 根据用户角色列表,获取该角色列表下用户列表信息-foreach_array -->
<delete id="delete2_foreach_array">
delete from sys_user where user_id in
<foreach collection="array" item="userIds" open="(" separator="," close=")" >
#{userIds}
</foreach>
</delete>
改造上一个演示示例,使用foreach实现,参数类型改为List
int delete2_foreach__list(List<Integer> roleList);
<!-- 根据用户角色列表,获取该角色列表下用户列表信息-foreach_list -->
<select id="delete2_foreach__list" resultMap="userMapByRole">
select * from smbms_user where userRole in
<foreach collection="list" item="roleList" open="(" separator="," close=")">
#{roleList}
</foreach>
</select>
在上一个演示示例中,增加一个参数:gender,要求查询出指定性别和用户角色列表下的用户列表
int delete2_foreach_map(Map<String,Object> roleMap);
<!-- 根据用户角色列表(单参数),获取该角色列表下用户列表信息-foreach_map -->
<select id="delete2_foreach_map" resultMap="userMapByRole">
select * from smbms_user where userRole in
<foreach collection="rKey" item="roleMap" open="(" separator="," close=")">
#{roleMap}
</foreach>
</select>
choose
-
相当于Java中switch语句
-
当when有条件满足的时候,就跳出choose
<!-- 查询用户列表(choose) -->
<select id="getUserList_choose" resultType="User">
select * from smbms_user where 1=1
<choose>
<when test="userName != null and userName != ''">
and userName like CONCAT ('%',#{userName},'%')
</when>
<when test="userCode != null and userCode != ''">
and userCode like CONCAT ('%',#{userCode},'%')
</when>
<when test="userRole != null">
and userRole=#{userRole}
</when>
<otherwise>
<!-- and YEAR(creationDate) = YEAR(NOW()) -->
and YEAR(creationDate) = YEAR(#{creationDate})
</otherwise>
</choose>
</select>
日志
pom.xml导入依赖
<properties>
<log4j2.version>2.9.1</log4j2.version>
</properties>
<!--日志 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
配置文件
log4j2.xml
分页
- 为用户管理之查询用户列表功能增加分页实现 列表结果按照创建时间降序排列
添加分页jar
<properties>
<pagehelper.version>5.1.4</pagehelper.version>
</properties>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
配置
在myatis配置文件中添加如下配置
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
使用测试
@Test
public void pagahelper() throws IOException {
SqlSession sqlSession= MybatisUtil.createSqlSession();
SysUserMapper sysUserMapper=sqlSession.getMapper(SysUserMapper.class);
SysUser user=new SysUser();
// 1.PageHelper.startPage(第几页,页大小)
PageHelper.startPage(1,2);
// 2.普通的查询会变成分页查询
List<SysUser> sysUserList=sysUserMapper.list6(user);
// 3.获取总记录数等信息
// PageInfo.of(list) 封装查询结果,pageInfo对象中封装总记录数及第几页的数据
PageInfo<SysUser> pageInfo=PageInfo.of(sysUserList);
System.out.println("总记录数="+pageInfo.getTotal());
MybatisUtil.closeSqlSession(sqlSession);
for (SysUser sysUser: sysUserList) {
System.out.println(sysUser.getUserId()+"\t"+sysUser.getUserName());
}
}
Mybatis_generator
mybatis-generator插件可自动生成实体类和mapper还有xml配置文件。在IDEA中只需修改插件中的generatorConfig.xml文件,然后运行配置文件就可以得到说需要的类,接口,xml文件。
导入依赖
<properties>
<generator.version>1.4.1</generator.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${generator.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${generator.version}</version>
<configuration>
<!--在控制台打印执行日志-->
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src\main\resources\generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
配置generatorConfig.xml
1.配置连接的数据库
<!--jdbc的数据库连接 smbms 为数据库名字-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/zmall?useUnicode=true&characeterEncoding=utf-8&serverTimezone=UTC"
userId="root"
password="sasa"></jdbcConnection>
2.配置实体类所在的位置
<javaModelGenerator targetPackage="com.zking.ssm.pojo"
targetProject="src/main/java">
<!-- 是否允许子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对modal添加构造函数 -->
<property name="constructorBased" value="false"/>
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true"/>
<!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
3.配置生成的mapper文件你希望它存在的位置
<sqlMapGenerator targetPackage="com.zking.ssm.mapper"
targetProject="src/main/resources">
<!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
4.你的数据库的列与实体类的联系
<!-- tableName是数据库中的表名,domainObjectName是生成的JAVA模型名,后面的参数不用改,要生成更多的表就在下面继续加table标签 -->
<table tableName="sys_user" domainObjectName="SysUser"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="sys_permission" domainObjectName="SysPermission"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>