mybatis的mapperxml文件
1.首先使用得到是springboot继承环境,创建mybatis的配置文件,在yml文件中添加mybatis的一些配置信息mybatis:typeAliasesPackage:com.ruoyi.**.domainmapperLocations: classpath*:mapper/**/*Mapper.xml#加载全局配置文件configLocation:classpath:mybatis/m
1.首先使用得到是springboot继承环境,创建mybatis的配置文件,在yml文件中添加mybatis的一些配置信息
mybatis:
typeAliasesPackage:com.ruoyi.**.domain
mapperLocations: classpath*:mapper/**/*Mapper.xml
#加载全局配置文件
configLocation:classpath:mybatis/mybatis-config.xml
mybatis的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局参数 -->
<settings>
<!-- 使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 允许JDBC 支持自动生成主键 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 配置默认的执行器.SIMPLE就是普通执行器;REUSE执行器会重用预处理语句(prepared statements);BATCH执行器将重用语句并执行批量更新 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 指定 MyBatis 所用日志的具体实现 -->
<setting name="logImpl" value="SLF4J" />
<!-- 使用驼峰命名法转换字段 -->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
</settings>
</configuration>
2.xml文件中的一些细节
例如这个mapper接口中的方法: public List<DjDeclaration> selectDjDeclarationList(DjDeclaration djDeclaration);
mapper文件表头
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.dangjian.mapper.DjDeclarationMapper">
//namespace是对应mapper接口的位置
通用的操作可以写到<sql />中,一般查询所有字段不会使用*,具体写出所有的字段
<sql id="selectDjDeclarationVo">
select declaration_id, title, content, date,
send_time, major_class, student_name, zan, is_hot, sort,
comment_num, status, remark, created_by,
created_time, updated_by, update_time from dj_declaration
</sql>
上边接口方法的select<> ,其中<include refid="selectDjDeclarationVo"/>就是引用上边的sql通用语句,resultMap还是resultType一定要注意清楚,返回多个字段一般创建一个resultmap,返回一对多的数据(eg:用户对应多个角色,在mybatis-plus中使用@TableField(exist = false)注释的字段@TableField(exist = false)
private List<SysMenu> menus;
)也需要使用resultmap,等会穿插进去。
<select id="selectDjDeclarationList" parameterType="DjDeclaration" resultMap="DjDeclarationResult">
<include refid="selectDjDeclarationVo"/>
<where>
<if test="title != null and title != ''"> and title = #{title}</if>
<if test="content != null and content != ''"> and content = #{content}</if>
<if test="date != null and date != ''"> and date = #{date}</if>
<if test="sendTime != null "> and send_time = #{sendTime}</if>
<if test="majorClass != null and majorClass != ''"> and major_class = #{majorClass}</if>
<if test="studentName != null and studentName != ''"> and student_name like concat('%', #{studentName}, '%')</if>
<if test="zan != null "> and zan = #{zan}</if>
<if test="isHot != null and isHot != ''"> and is_hot = #{isHot}</if>
<if test="sort != null "> and sort = #{sort}</if>
<if test="commentNum != null "> and comment_num = #{commentNum}</if>
<if test="status != null "> and status = #{status}</if>
<if test="createdBy != null "> and created_by = #{createdBy}</if>
<if test="createdTime != null "> and created_time = #{createdTime}</if>
<if test="updatedBy != null "> and updated_by = #{updatedBy}</if>
</where>
</select>
注意这里的<where> 和 <if test>的标签的使用,虽然这里是list全部列表的方法的xml文件,但是参数有个对象实例,如果是直接查询就是对象实例为空where标签都不会执行,如果是进行模糊查询列表后边的就会根据where和if的组合查询出来。
eg:还有很多多种操作使用同一个方法的案例:增加和修改使用同一个controller方法,如果添加时sysUser就为null,修改时sysuser就是前端传回来的数据
/**
* 编辑
*/
@RequestMapping("/form")
public String form(Model model, Integer id, SysUser sysUser) {
//获取所有的角色
List<SysRole> roleList = sysRoleService.list();
if (id != null) {
sysUser = sysUserService.findUserById(id);
}
model.addAttribute("roleList", roleList);
System.out.println(sysUser);
model.addAttribute("sysUser", sysUser);
return "sys/userForm";
}
接下来继续写resultmap,<id>对应前边写的resultmap名称,<result>中的property对应中的字段名称,column对应数据库中的查询字段。
<resultMap type="DjDeclaration" id="DjDeclarationResult">
<result property="declarationId" column="declaration_id" />
<result property="title" column="title" />
<result property="content" column="content" />
<result property="date" column="date" />
<result property="sendTime" column="send_time" />
<result property="majorClass" column="major_class" />
<result property="studentName" column="student_name" />
<result property="zan" column="zan" />
<result property="isHot" column="is_hot" />
<result property="sort" column="sort" />
<result property="commentNum" column="comment_num" />
<result property="status" column="status" />
<result property="remark" column="remark" />
<result property="createdBy" column="created_by" />
<result property="createdTime" column="created_time" />
<result property="updatedBy" column="updated_by" />
<result property="updateTime" column="update_time" />
</resultMap>
前边讲到mybatis-plus中使用@TableField(exist = false)注释数据库中不存在的中间字段,在xml文件中查询的时候,需要在<resultmap> 中添加<collection>标签对应一对多
<rseultMap id="userMap" type="SysUser">
<id property="id" column="id">
<result peoperty="username" column="username">
<collection peoperty="roles" ofType="SysUser">
<id property="id" column="role_id">
<result property="name" column="role_name">
</collection>
添加操作,id为mapper文件的方法名称,
useGeneratedKeys="true" keyProperty="declarationId"这里是主键declarationId自增,插入时不用写id值
<trim>标签中prefix"("是在下边if判断的所有标签前边加上(
suffix=")" 是在所有的结束后加上) ,suffixoverrides=","是去掉,换上)
<insert id="insertDjDeclaration" parameterType="DjDeclaration" useGeneratedKeys="true" keyProperty="declarationId">
insert into dj_declaration
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="title != null">title,</if>
<if test="content != null">content,</if>
<if test="date != null">date,</if>,
<if test="sendTime != null">send_time,</if>
<if test="majorClass != null">major_class,</if>
<if test="studentName != null">student_name,</if>
<if test="zan != null">zan,</if>
<if test="isHot != null">is_hot,</if>
<if test="sort != null">sort,</if>
<if test="commentNum != null">comment_num,</if>
<if test="status != null">status,</if>
<if test="remark != null">remark,</if>
<if test="createdBy != null">created_by,</if>
<if test="createdTime != null">created_time,</if>
<if test="updatedBy != null">updated_by,</if>
<if test="updateTime != null">update_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="title != null">#{title},</if>
<if test="content != null">#{content},</if>
<if test="date != null">#{date},</if>
<if test="sendTime != null">#{sendTime},</if>
<if test="majorClass != null">#{majorClass},</if>
<if test="studentName != null">#{studentName},</if>
<if test="zan != null">#{zan},</if>
<if test="isHot != null">#{isHot},</if>
<if test="sort != null">#{sort},</if>
<if test="commentNum != null">#{commentNum},</if>
<if test="status != null">#{status},</if>
<if test="remark != null">#{remark},</if>
<if test="createdBy != null">#{createdBy},</if>
<if test="createdTime != null">#{createdTime},</if>
<if test="updatedBy != null">#{updatedBy},</if>
<if test="updateTime != null">#{updateTime},</if>
</trim>
</insert>
删除操作,in配合<foreach>实现删除语句
<delete id="deleteDjDeclarationByDeclarationIds" parameterType="String">
delete from dj_declaration where declaration_id in
<foreach item="declarationId" collection="array" open="(" separator="," close=")">
#{decla rationId}
</foreach>
</delete>
更多推荐
所有评论(0)