背景
在写代码的过程中,总是断断续续遇到各种各样的问题或者各种错误,总是记录的零零散散,在这里集中总结一下,不断更新,不断总结;加油
请求头header中添加中文乱码的问题
解决方法:RLEncoder和URLDecoder可以解决这个问题(实际上,就是对中文进行了编码)
String name = URLEncoder.encode("懒羊羊", "utf-8");
String res = URLDecoder.decode(name, "utf-8");
target不会更新一些文件夹
在使用mybatis的过程中,mapper问价夹下面的xml包不会同步更新到target文件夹中,生成.class,这也代表着程序无法正常执行
解决办法:在pom.xml中导入
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
mybatis向xml传参
实体类对象和变量对象
在mapper层接收的时候都应该 @Param("参数名称") 类型 参数名称
List<Teacher> findQueryPage(@Param("current") int current, @Param("limit") int limit, @Param("teacherQueryVo") TeacherQueryVo teacherQueryVo);
在xml文件中使用变量名时,变量使用:#{变量名},实体类使用:#{实体类名 . 变量名}
<select id="findQueryPage" resultMap="teacherResultMap">
select * from teacher
<where>
<if test = "teacherQueryVo.name != null and teacherQueryVo.name != ''">
and name like concat('%',#{teacherQueryVo.name},'%')
</if>
<if test = "teacherQueryVo.level != null">
and level = #{teacherQueryVo.level}
</if>
<if test = "teacherQueryVo.joinDateBegin != null and teacherQueryVo.joinDateBegin != ''">
and joinDate > #{teacherQueryVo.joinDateBegin}
</if>
<if test = "teacherQueryVo.joinDateEnd != null and teacherQueryVo.joinDateEnd != ''">
and joinDate < #{teacherQueryVo.joinDateEnd}
</if>
</where>
limit ${(current - 1) * limit}, #{limit}
</select>
返回值为List
当方法返回值为List时,尽量不要采用resultType,要使用resultMap
<resultMap id="teacherResultMap" type = "com.atguigu.ggkt.model.vod.Teacher">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="intro" column="intro"/>
<result property="career" column="career"/>
<result property="level" column="level"/>
<result property="avater" column="avater"/>
<result property="sort" column="sort"/>
<result property="joinDate" column="join_date"/>
</resultMap>
mybatis 找不到xml文件
应该在application中配置路径
例如:
mapper-locations: classpath:com/atguigu/ggkt/vod/mapper/**/*.xml