[Original] Adding and modifying the problem of judging data duplication
1. Problem requirements
operate | need |
---|---|
Add | The newly added data cannot exist in the database, and if it exists, it cannot be added |
Revise | If the modified data is the submitted current data, the submission is successful, and the modified data can be modified only if it does not exist in other data |
When encountering this problem, Xiao Liu immediately thought of select count(*) to judge the number of records in a certain data in the database
- Return row number = 0 means that the data does not exist
- The number of returned rows > 1 means that the parameters are in the data
But this does not take into account the problem that the user submits without modifying anything when modifying. Xiao Liu does not want to change too much code to fix this problem, so he wants to use the original select account(*) to solve this problem. After thinking In the end, it was solved by Mr. Liu, here is the solution
2. SQL code
<!--根据工号查询老师数量信息-->
<select id="queryCountTeacherInfoByJobNumber" resultType="int" parameterType="Teacher">
select count(*) from teacher where job_number = #{
jobNumber}
<if test="id!=null and id!=''">
and id !=#{
id}
</if>
</select>
When adding a judgment condition to the original basis, you can query the duplicate data information of other data except itself
3. Business code
//判断是否重复
int i = teacherDao.queryCountTeacherInfoByJobNumber(teacher);
if (i > 0) {
return Page.INFOERROR;
}
- i = 0 no duplicate data exists
- i > 1 In addition to itself, there are duplicate data
pay attention here
When modifying, we must also bring the id of the current data to be modified to determine the number of repetitions
Welcome to Xiaoliu Jun's official account