A few days ago, the company planned to develop a large platform integrating various management functions, and let me be responsible for the development of test related services. For the first time, I was really busy as a dog. The project was finally approved last week and officially entered the development stage this week. The general requirements are as follows:
- Test account management
- Public data management
- test case management
- Test case collection management
- Test case run result report
About these five big functions, because many of the spring boots that I learned a little before are almost forgotten, I reviewed them in advance. As expected, I found that many knowledge points were not proficient in learning, and some deeper difficulties were not considered before.
Fortunately, I prepared ahead of time, otherwise I won't be able to finish it in 10 days. The platform itself is developed by Python, with a separate front-end engineer. The moco function for development is also done by the front-end mockJS, and then the back-end is compatible. The test service also has to analyze the data of this mockJS. I haven't touched it before, and I'm a little flustered.
Public data management is a test case that needs some common parameters and simple parameters in the common header. This is relatively simple. The data is completely processed and stored by me, so I prepared to develop it first. Let's share some requirements and technical implementation in this regard.
Requirement prototype
mock document for interface
Charging content
The development of this basic function is relatively simple. How to store the public data? Originally, the whole public data I designed was used as a json object, but it was not. Because this public data will be imported into the interface parameters and Header, and it should be consistent with the data format of mockJS, so it is stored in the form of an array. Each line takes text and value, and the default type is string type, so there is no need to pass it here. There is a problem: the interface request mapping object property is set as follows:
class PubDataBean extends AbstractBean { private static final long serialVersionUID = 8754205931721071606L; int id @Range(min = 1L, max = 5L, message = "Environment setting error") int envId @Min(value = 1L) int uid @NotNull(message = "Public data cannot be empty") @Size(min = 1, message = "Data length error") List<PubDataDetailBean> list @Length(min = 1, max = 16, message = "Name length error") String name }
The type of property list used to hold public data is List<PubDataDetailBean> List, how to match the non basic data type object with the field in the database? I charged the power for a while and found the solution: just customize a base typehandler and specify the generics. There is also a type handler of JSonobject in the commonly used mysql type conversion class, which is shared together:
ListPubDetailHandler
package com.okay.family.common.typehandler; import com.alibaba.fastjson.JSON; import com.okay.family.common.basedata.OkayConstant; import com.okay.family.common.bean.pubdata.PubDataDetailBean; import com.okay.family.fun.config.Constant; import com.okay.family.fun.utils.Join; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @MappedTypes(com.okay.family.common.bean.pubdata.PubDataDetailBean.class) @MappedJdbcTypes(JdbcType.VARCHAR) public class ListPubDetailHandler extends BaseTypeHandler<List<PubDataDetailBean>> { @Override public void setNonNullParameter(PreparedStatement ps, int i, List<PubDataDetailBean> parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, Join.join(parameter, OkayConstant.MYSQL_SEPARATE)); } @Override public List<PubDataDetailBean> getNullableResult(ResultSet rs, String columnName) throws SQLException { String str = rs.getString(columnName); if (null != str) { List<String> beans = Arrays.asList(str.split(OkayConstant.MYSQL_SEPARATE)); return beans.stream().map(x -> JSON.parseObject(x, PubDataDetailBean.class)).collect(Collectors.toList()); } return null; } @Override public List<PubDataDetailBean> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String str = rs.getString(columnIndex); if (null != str) { List<String> beans = Arrays.asList(str.split(Constant.PART)); return beans.stream().map(x -> JSON.parseObject(x, PubDataDetailBean.class)).collect(Collectors.toList()); } return null; } @Override public List<PubDataDetailBean> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String str = cs.getString(columnIndex); if (null != str) { List<String> beans = Arrays.asList(str.split(Constant.PART)); return beans.stream().map(x -> JSON.parseObject(x, PubDataDetailBean.class)).collect(Collectors.toList()); } return null; } }
JsonHandler
package com.okay.family.common.typehandler; import com.alibaba.fastjson.JSONObject; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * @description A user-defined converter for converting fields in json format in mysql to JSONObject properties of entity classes */ @MappedTypes(JSONObject.class) @MappedJdbcTypes(JdbcType.VARCHAR) public class JsonHandler extends BaseTypeHandler<JSONObject> { /** * Set non empty parameters * * @param ps * @param i * @param jdbcType * @throws SQLException */ @Override public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, String.valueOf(parameter.toJSONString())); } /** * Get the result that can be empty according to the column name * * @param rs * @param columnName * @return * @throws SQLException */ @Override public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException { String sqlJson = rs.getString(columnName); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } /** * Get nullable results based on column index * * @param rs * @param columnIndex * @return * @throws SQLException */ @Override public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String sqlJson = rs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } @Override public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String sqlJson = cs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } }
mapper.xml to configure
<?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.okay.family.mapper.PubDataMapper"> <sql id="table"> qa_pub_data </sql> <resultMap id="entityMap" type="com.alibaba.fastjson.JSONObject"> <!-- Entity class json Field, typeHandler Specify custom typeHandler --> <result column="list" property="pubdata" typeHandler="com.okay.family.common.typehandler.JsonHandler"/> </resultMap> <resultMap type="com.okay.family.common.bean.pubdata.PubDataBean" id="PubDataBean"> <result property="id" column="id"/> <result property="uid" column="uid"/> <result property="envId" column="envId"/> <result property="name" column="name"/> <result property="list" column="list" typeHandler="com.okay.family.common.typehandler.ListPubDetailHandler"/> </resultMap> <select id="getAllDatas" parameterType="java.lang.Integer" resultMap="PubDataBean"> select * from <include refid="table"/> where uid = #{0} </select> <select id="getDatasByEnv" resultMap="PubDataBean"> select * from <include refid="table"/> where uid = #{arg0} and envId = #{arg1} </select> <update id="updateDataAttribute" parameterType="com.okay.family.common.bean.pubdata.EditPubBean"> UPDATE <include refid="table"/> SET name=#{name} WHERE id=#{id} and uid = #{uid} </update> <update id="saveData" parameterType="com.okay.family.common.bean.pubdata.SavePubDataBean"> UPDATE <include refid="table"/> SET list=#{list,jdbcType=OTHER,typeHandler=com.okay.family.common.typehandler.ListPubDetailHandler} WHERE id=#{id} AND uid=#{uid} </update> <insert id="add" useGeneratedKeys="true" keyProperty="id" parameterType="com.okay.family.common.bean.pubdata.EditPubBean"> INSERT INTO <include refid="table"/> (uid,name,envId) VALUES (#{uid},#{name},#{envId}) </insert> <delete id="delData" parameterType="com.okay.family.common.bean.pubdata.EditPubBean"> delete from <include refid="table"/> where id = #{id} and uid = #{uid} </delete> </mapper>
Of course, there are still some shortcomings and areas to be optimized, and we will continue to improve them in the future.
- The official account is that the public number "FunTester" starts, and welcomes attention and prohibits the third party from reprinting. More original articles: FunTester 18 original albums , please contact Fhaohaizi@163.com .