企业内部开发经常遇到的一个问题是,bug追踪问题,一些公司使用excel表格追踪问题,虽然可以,但是随着业务的增加,变的难以维护。为了有效地管理bug,我们需要要一个强大的错误跟踪解决方案,比如现代化的缺陷跟踪管理软件,它们可以跟踪每个项目阶段的bug,从开发过程到软件测试和发布阶段。
创建domain(需要了解lombok的使用)
public class User implements Serializable {
private Long id;
private String userName;
private String password;
private Date regTime;
private Date updateTime;
private int status;
private String token;
private int code;
}
public class Role implements Serializable {
private Long id;
private String name;
private int code;
private Date updateTime;
private int status;
}
public class Res implements Serializable {
private Long id;
private String name;
private String url;
private int code;
private Date updateTime;
private int status;
}
public class Project implements Serializable {
private Long id;
private String name;
private Date regTime;
private Date updateTime;
private int status;
private String frontUrl;
private String backUrl;
private String author;
private Long userId;
}
public class Menu implements Serializable {
private Long id;
private Date updateTime;
private int status;
private String name;
private String url;
private long code;
private String icon;
}
public class Member implements Serializable {
private Long id;
private Long userId;
private Long projectId;
private String userName;
private String projectName;
}
public class BugFile implements Serializable {
private Long id;
private Long bugId;
private String path;
private String url;
private Long projectId;
private String projectName;
}
public class Bug implements Serializable {
private Long id;
private Long projectId;
private Long userId;
private String userName;
private Date regTime;
private Date updateTime;
private int lv;
private String description;
private String image;
private int status;
private int origin;
private String feedback;
private int round;
}
编写Mybatis mapper。只使用Mybatis核心组件,不需要其他增强框架,比如Mybatis-plus(懒人框架)等。Mybatis是个非常灵活的持久层框架,半个orm框架。
- MyBatis的优点
- 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件就可以了,易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现。
- 灵活:mybatis不会对应用程序或者数据库的现有设计强加任何影响。sql写在xml里,便于统一管理和优化。通过sql语句可以满足操作数据库的所有需求。
- 解除sql与程序代码的耦合:通过提供DAO层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰,更易维护,更易单元测试。sql和代码的分离,提高了可维护性。
- 提供xml标签,支持编写动态sql。
以上这些已经足够了,没必要在引入其他的!
如果追求性能可以使用sql2o,个人认为这是更好的选择。
@Repository
public interface UserMapper extends BaseMapper<Long,User> {
List<User> list(@Param("pageNum")int pageNum, @Param("pageSize")int pageSize, @Param("userName")String userName);
int count(@Param("userName")String userName);
List<User> listByCode(@Param("code")int code);
}
@Repository
public interface RoleMapper extends BaseMapper<Long,Role> {
List<Role> list(int status);
Role getWithMaxCode();
}
@Repository
public interface ResMapper extends BaseMapper<Long,Res> {
Res getWithCode(@Param("status")int status,@Param("url")String url,@Param("code")int code);
List<Res> list(@Param("pageNum")int pageNum, @Param("pageSize")int pageSize, @Param("name")String name);
int count(@Param("name")String name);
}
@Repository
public interface ProjectMapper extends BaseMapper<Long,Project> {
List<Project> list(@Param("pageNum")int pageNum,
@Param("pageSize")int pageSize,
@Param("status")int status,
@Param("name")String name,
@Param("userId")Long userId);
int count(@Param("status")int status,
@Param("name")String name,
@Param("userId")Long userId);
}
@Repository
public interface MenuMapper extends BaseMapper<Long,Menu> {
List<Menu> list(@Param("status") int status, @Param("code") int code);
}
@Repository
public interface MemberMapper extends BaseMapper<Long,Member> {
int delete(Long projectId);
List<User> listByProjectId(Long projectId);
List<Project> listByUserId(Long userId);
}
@Repository
public interface BugMapper extends BaseMapper<Long,Bug> {
List<Bug> list(@Param("pageNum")int pageNum,
@Param("pageSize")int pageSize,
@Param("projectId")Long projectId,
@Param("level")int level,
@Param("origin")int origin,
@Param("status")int status);
int count(@Param("projectId")Long projectId,
@Param("level")int level,
@Param("origin")int origin,
@Param("status")int status);
}
@Repository
public interface BugFileMapper extends BaseMapper<Long,BugFile> {
BugFile getByBugId(@Param("bugId")Long bugId);
List<BugFile> list(@Param("pageNum")int pageNum,
@Param("pageSize")int pageSize,
@Param("projectName")String projectName);
int count(@Param("projectName")String projectName);
}
编写mapper.xml
MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制。 为了使它更加强大而且易于配置,我们对 MyBatis 3 中的缓存实现进行了许多改进。
这个项目没有使用缓存,实际开发中大家不要忘记加上,配置也是很简单了,支持redis。
关于<sql id="id"></sql>这个标签想说两句,可以把它理解为var或者let,一处定义到处使用,并且可修改值,而这个东西,一旦定义就不能变了,虽然可以重用,但是实际开发中,使用相同的值并不常见,个人认为还不如不用。
<?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.dguo.web.mapper.UserMapper">
<select id="list" resultType="user">
select id,user_name,password,reg_time,update_time,status from t_user
<where>
<if test="userName != null">
user_name like concat("%",#{userName},"%")
</if>
</where>
order by id desc
limit #{pageNum},#{pageSize}
</select>
<select id="count" resultType="int">
select count(*) from t_user
<where>
<if test="userName != null">
user_name like concat("%",#{userName},"%")
</if>
</where>
</select>
<select id="listByCode" resultType="user">
select id,user_name from t_user
where status =2 and BITAND(code,#{code}) !=0
</select>
</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.dguo.web.mapper.RoleMapper">
<select id="list" resultType="role">
select id,name,code,update_time,status from t_role
<where>
<if test="status !=0">
status = #{status}
</if>
</where>
</select>
<select id="getWithMaxCode" resultType="role">
select * from t_role where code = (select max(code) from t_role)
</select>
</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.dguo.web.mapper.ResMapper">
<select id="getWithCode" resultType="res">
select name,url,code,status from t_res
<where>
<if test="status !=0">
status = #{status}
</if>
<if test="url !=null">
and url = #{url}
</if>
<if test="code != null">
and BITAND(code,#{code}) !=0
</if>
</where>
</select>
<select id="list" resultType="res">
select id,name,url,code,update_time,status from t_res
<where>
<if test="name != null">
name like concat("%",#{name},"%")
</if>
</where>
order by id asc
limit #{pageNum},#{pageSize}
</select>
<select id="count" resultType="int">
select count(*) from t_res
<where>
<if test="name != null">
name like concat("%",#{name},"%")
</if>
</where>
</select>
</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.dguo.web.mapper.ProjectMapper">
<select id="list" resultType="project">
select id,name,reg_time,update_time,status,front_url,back_url
from t_project
<where>
<if test="status !=0">
status = #{status}
</if>
<if test="name != null and name != ''">
and name like concat("%",#{name},"%")
</if>
<if test="userId !=null">
and user_id = #{userId}
</if>
</where>
order by id desc
limit #{pageNum},#{pageSize}
</select>
<select id="count" resultType="int">
select count(*)
from t_project
<where>
<if test="status !=0">
status = #{status}
</if>
<if test="name != null and name != ''">
and name like concat("%",#{name},"%")
</if>
<if test="userId !=null">
and user_id = #{userId}
</if>
</where>
</select>
</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.dguo.web.mapper.MenuMapper">
<select id="list" resultType="menu">
select id,name,code,url,icon,update_time,status from t_menu
<where>
<if test="status !=0">
status = #{status}
</if>
<if test="code != 0">
and BITAND(code,#{code}) !=0
</if>
</where>
</select>
</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.dguo.web.mapper.MemberMapper">
<select id="listByProjectId" resultType="user">
select a.id,a.user_name
from t_user a inner join t_member b
on a.id = b.user_id
where b.project_id = #{projectId}
</select>
<delete id="delete">
delete from t_member where project_id = #{projectId}
</delete>
<select id="listByUserId" resultType="project">
select a.id,a.name
from t_project a inner join t_member b
on a.id = b.project_id and a.status = 2
where b.user_id = #{userId}
</select>
</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.dguo.web.mapper.BugMapper">
<select id="list" resultType="bug">
select id,user_name,reg_time,lv,description,status,round
from t_bug
<where>
<if test="projectId !=null">
project_id = #{projectId}
</if>
<if test="level !=0">
and lv = #{level}
</if>
<if test="origin !=0">
and origin=#{origin}
</if>
<if test="status !=0">
and status = #{status}
</if>
</where>
order by id desc
limit #{pageNum},#{pageSize}
</select>
<select id="count" resultType="int">
select count(*)
from t_bug
<where>
<if test="projectId !=null">
project_id = #{projectId}
</if>
<if test="level !=0">
and lv = #{level}
</if>
<if test="origin !=0">
and origin=#{origin}
</if>
<if test="status !=0">
and status = #{status}
</if>
</where>
</select>
</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.dguo.web.mapper.BugFileMapper">
<select id="getByBugId" resultType="bugFile">
select id,url
from t_bug_file
where bug_id = #{bugId}
</select>
<select id="list" resultType="bugFile">
select * from t_bug_file
<where>
<if test="projectName !=null and projectName !=''">
project_name like concat("%",#{projectName},"%")
</if>
</where>
limit #{pageNum},#{pageSize}
</select>
<select id="count" resultType="int">
select count(*) from t_bug_file
<where>
<if test="projectName !=null and projectName !=''">
project_name like concat("%",#{projectName},"%")
</if>
</where>
</select>
</mapper>
下次编写权限拦截器,异常处理。