第一章 Java EE应用和开发环境
1.6 Maven的安装和使用
- maven是一个项目管理工具,采用约定大于配置(COC, Convention Over Configuration)的策略
- 文件结构
- bin:保存maven可执行的命令,其中mvn和mvn.bat就是执行maven工具的命令
- boot :只有plexus-classworlds.jar,它是一个类加载器框架,与默认的java类加载器相比,它提供了更丰富的语法以方便配置,maven使用该框架加载自己的类库,通常无须理会该文件
- conf:保存maven配置文件的目录,比如settings.xml文件
- lib:该目录包含所有maven运行时需要的类库
- license、readme.txt等说明性文档
- maven需要配置的环境变量
- JAVA_HOME: JDK的安装路径
- M2_HOME: MAVEN的安装路径
- idea有内置的maven,别忘了改为自己安装的maven(settings中)
- 将%M2_HOME%\bin路径添加到环境变量,这样可以识别到bin下的mvn.bat命令
- settings文件
- localRepository: 设置本地资源库路径
- interactiveMode: 输入maven命令时是否是交互模式,是的话有提示
- offline: 设置maven是否处于离线状态,false代表找不到插件或者依赖库时总会尝试从网络下载
- proxies: 设置代理服务器
- mirrors: 设置镜像
- 约定大于配置的约定
- 源代码位置: ${basedir}/src/main/java
- 资源文件: ${basedir}/src/main/resources
- 测试代码: ${basedir}/src/test
- 编译生成的class文件: ${basedir}/target/classes
- 生成的jar文件: ${basedir}/target
- 生命周期
- 三个基本的生命周期。
- clean生命周期
- default生命周期
- site生命周期(发布运行相关,这个功能其实被devops替代了,实际上没人用这个生命周期,这里就不做介绍了)
- clean生命周期包含的阶段:
- pre-clean:在构建之前执行预清理
- clean:执行清理
- post-clean:最后清理(在一个生命周期内执行了后面的步骤,前面的步骤也会被执行,比如执行第二个,第一个会先执行)
- default(build)生命周期包含的阶段
- compile:编译项目
- test:单元测试
- package:项目打包
- install:安装到本地仓库(在一个生命周期内执行了后面的步骤,前面的步骤也会被执行,比如这一步,从compile到这一步都会执行,当然只会执行build生命周期中的步骤,clean中的并不会执行)
- deploy:部署到远程仓库
- 其实build生命周期中包含更详细的阶段,共23个,这里就不列举了
- 三个基本的生命周期。
- <dependency>元素下的<scope>元素的类型
- compile:默认的范围,编辑、测试、打包时需要
- provided:表示容器会在运行时提供,web容器会提供,jar中不会带,防止冲突
- runtime:表示编译时不需要,但测试和运行时需要,最终打包会包含进来
- test:只用于测试阶段
- system:与provided类似,但要求该JAR包是系统自带的
- import:继承父pom文件中用dependencyManagement配置的依赖,import范围只能在dependencyManagement元素中使用(为了解决多继承)
- pom文件的元素
- <properties…>:定义全局属性
- <dependencies…>: 定义依赖
- <dependencyManagement…>:依赖管理
- <build…>:定义构建信息
- <reporting…>:定义站点报告的相关信息
- <licenses…>:License信息
- <organization…>:组织信息
- <developers…>:开发者信息
- <contributors…>:贡献者信息
- <issueManagement…>:bug跟踪系统
- <mailingLists…>:邮件列表
- <scm…>:源代码管理工具
- <repositories…>:远程资源库的位置
- <pluginRepositories…>:插件资源库的位置
- <distributionManagement…>:部署管理
- <profiles…>:根据指定环境调整构建信息
第二章 MyBatis的基础用法
第三章 深入使用MyBatis
3.5 动态SQL
<if><choose>
不能删除运算符and、or,<where><set><trim>
可以。
3.5.1 if元素的用法
场景:对传入的参数值存在与否不确定时
tip: <if…/>元素不能自动删除不需要的and字符,where元素可以
示例:
/**
* 查询岗位数据集合
*
* @param post 岗位信息
* @return 岗位数据集合
*/
public List<SysPost> selectPostList(SysPost post);
<sql id="selectPostVo">
select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark
from sys_post
</sql>
<select id="selectPostList" parameterType="SysPost" resultMap="SysPostResult">
<include refid="selectPostVo"/>
<where>
<if test="postCode != null and postCode != ''">
AND post_code like concat('%', #{postCode}, '%')
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="postName != null and postName != ''">
AND post_name like concat('%', #{postName}, '%')
</if>
</where>
</select>
3.5.2 在update更新列中使用if
示例:
/**
* 修改角色信息
*
* @param role 角色信息
* @return 结果
*/
public int updateRole(SysRole role);
<update id="updateRole" parameterType="SysRole">
update sys_role
<set>
<if test="roleName != null and roleName != ''">role_name = #{roleName},</if>
<if test="roleKey != null and roleKey != ''">role_key = #{roleKey},</if>
<if test="roleSort != null and roleSort != ''">role_sort = #{roleSort},</if>
<if test="dataScope != null and dataScope != ''">data_scope = #{dataScope},</if>
<if test="menuCheckStrictly != null">menu_check_strictly = #{menuCheckStrictly},</if>
<if test="deptCheckStrictly != null">dept_check_strictly = #{deptCheckStrictly},</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = sysdate()
</set>
where role_id = #{roleId}
</update>
3.5.3 在insert动态插入列中使用if
示例:
/**
* 新增任务日志
*
* @param jobLog 调度日志信息
* @return 结果
*/
public int insertJobLog(SysJobLog jobLog);
<insert id="insertJobLog" parameterType="SysJobLog">
insert into sys_job_log(
<if test="jobLogId != null and jobLogId != 0">job_log_id,</if>
<if test="jobName != null and jobName != ''">job_name,</if>
<if test="jobGroup != null and jobGroup != ''">job_group,</if>
<if test="invokeTarget != null and invokeTarget != ''">invoke_target,</if>
<if test="jobMessage != null and jobMessage != ''">job_message,</if>
<if test="status != null and status != ''">status,</if>
<if test="exceptionInfo != null and exceptionInfo != ''">exception_info,</if>
create_time
)values(
<if test="jobLogId != null and jobLogId != 0">#{jobLogId},</if>
<if test="jobName != null and jobName != ''">#{jobName},</if>
<if test="jobGroup != null and jobGroup != ''">#{jobGroup},</if>
<if test="invokeTarget != null and invokeTarget != ''">#{invokeTarget},</if>
<if test="jobMessage != null and jobMessage != ''">#{jobMessage},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="exceptionInfo != null and exceptionInfo != ''">#{exceptionInfo},</if>
sysdate()
)
</insert>
3.5.4 choose、when、otherwise元素的用法
场景:用于多种可能性,且可能性具有优先级,而只选择其中一个时。比如要在下个月发奖品,第一优先级发给小a,小a离职了就发给小b
,小a和小b都离职了,默认发给小c。
tip: choose元素也不能删除and;它有点类似switch,这些分支具有排他性,程序只能执行其中一个,即各个when和otherwise中只能执行一个分支。
示例:
select * from tablename where 1=1
<choose>
<when test="a != null">
and tablename_a = #{a}
</when>
<when test="b != null">
and tablename_b = #{b}
</when>
<otherwise test="c != null">
and tablename_c = #{c}
</otherwise>
<choose>
3.5.5/6/7 where与trim的用法
tip:可以剔除and、or等多余的运算符,<set…/>也可以做到。
示例:
where元素:
/**
* 根据条件分页查询字典数据
*
* @param dictData 字典数据信息
* @return 字典数据集合信息
*/
public List<SysDictData> selectDictDataList(SysDictData dictData);
<sql id="selectDictDataVo">
select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark
from sys_dict_data
</sql>
<select id="selectDictDataList" parameterType="SysDictData" resultMap="SysDictDataResult">
<include refid="selectDictDataVo"/>
<where>
<if test="dictType != null and dictType != ''">
AND dict_type = #{dictType}
</if>
<if test="dictLabel != null and dictLabel != ''">
AND dict_label like concat('%', #{dictLabel}, '%')
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
</where>
order by dict_sort asc
</select>
trim元素四个属性的作用:
prefix:前缀
suffix:后缀
prefixOverrides:动态删除前面不需要的内容
suffixOverrides:动态删除后面不需要的内容
示例:
<insert id="saveNews">
insert into news_inf (
<trim suffixOverrides=",">
<if test="title != null">
news_title,
</if>
<if test="content != null">
news_content,
</if>
<if test="status != null">
news_status
</if>
</trim>)
values (
<trim suffixOverrides=",">
<if test="title != null">
#{title},
</if>
<if test="content != null">
#{content},
</if>
<if test="status != null">
#{status}
</if>
</trim>)
</insert>
3.5.8/9/10 foreach元素的基本用法
属性:
collection:指定要遍历的集合,数组默认值是array,集合默认值是list;
index:相当于key
item:相当于value
open:遍历开始处放置的字符串
close:遍历结束时放置的字符串
separator:指定在迭代元素之间添加的分隔符
示例一:
/**
* 批量删除角色部门关联信息
*
* @param ids 需要删除的数据ID
* @return 结果
*/
public int deleteRoleDept(Long[] ids);
<delete id="deleteRoleDept" parameterType="Long">
delete from sys_role_dept where role_id in
<foreach collection="array" item="roleId" open="(" separator="," close=")">
#{roleId}
</foreach>
</delete>
示例二:
/**
* 修改子元素关系
*
* @param depts 子元素
* @return 结果
*/
public int updateDeptChildren(@Param("depts") List<SysDept> depts);
<update id="updateDeptChildren" parameterType="java.util.List">
update sys_dept set ancestors =
<foreach collection="depts" item="item" index="index"
separator=" " open="case dept_id" close="end">
when #{item.deptId} then #{item.ancestors}
</foreach>
where dept_id in
<foreach collection="depts" item="item" index="index"
separator="," open="(" close=")">
#{item.deptId}
</foreach>
</update>
示例三:
/**
* 批量新增角色部门信息
*
* @param roleDeptList 角色部门列表
* @return 结果
*/
public int batchRoleDept(List<SysRoleDept> roleDeptList);
<insert id="batchRoleDept">
insert into sys_role_dept(role_id, dept_id) values
<foreach item="item" index="index" collection="list" separator=",">
(#{item.roleId},#{item.deptId})
</foreach>
</insert>
3.5.11 bind元素的用法
tip:在xml中定义一个变量
属性:
name:变量名;value:变量名
示例:
<if test="userName != null and userName != ''">
<bind name="nameLike" value="'%' + userName + '%'"/>
and user_name like #{nameLike}
</if>