Skip to content

Commit

Permalink
添加回答持久化类与mapper 建立用户与回答 问卷与回答的一对多映射
Browse files Browse the repository at this point in the history
  • Loading branch information
suvvm committed Dec 2, 2019
1 parent 9f03bc1 commit e364215
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public interface InvMapper {
@SelectProvider(type = InvMapperProvider.class, method = "findInv")
@Results({
@Result(property = "id", column = "id", id = true),
@Result(property = "tags", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.TagMapper.getTagByInvId"))
@Result(property = "tags", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.TagMapper.getTagByInvId")),
@Result(property = "results", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.ResultMapper.getResultByIId"))
})
public List<Investigate> getInvestigate(Investigate inv);

Expand Down Expand Up @@ -100,7 +101,7 @@ public String updateInv(Investigate inv) {
SET("details = #{details}");
}
if(inv.getTime() != null) {
SET("time = #{time}");
SET("time =#{time}");
}
WHERE("id = #{id}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package qdu.suvvm.onlinesurvey.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import qdu.suvvm.onlinesurvey.pojo.Result;

import java.util.List;

/**
* @ClassName: ResultMapper
* @Description: TODO
* @Author: SUVVM
* @Date: 2019/12/2 18:32
*/
public interface ResultMapper {

@SelectProvider(type = ResultMapperProvider.class, method = "findRes")
public List<Result> getResult(Result res);

@Delete("delete from userresults where uid=#{uid}")
public int deleteResultByUId(Integer uid);

@Delete("delete from userresults where iid=#{iid}")
public int deleteResultByIId(Integer iid);

// 插入investigate并获取自增id
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into userresults(uid,iid,ans) values(#{uid},#{iid},#{ans})")
public int insertResult(Result res);

// 根据id更新investigate
@Update("update from userresults set ans=#{ans} where iid=#{iid} && uid=##{uid}")
public int updateResult(Result res);

@Select("select * from userresults where uid=#{id}")
public List<Result> getResultByUId(Integer id);

@Select("select * from userresults where iid=#{id}")
public List<Result> getResultByIId(Integer id);

class ResultMapperProvider{
/**
* @FunctionName: findRes
* @Description: 用于生成查询作答的动态sql
* @Parameter:
* res 查询数据封装的回答类
* @Return: 返回对应sql语句
*/
public String findRes(Result res) {
return new SQL() {
{
SELECT("*");
FROM("userresults");
if(res.getUid() != null) {
WHERE("uid = #{uid}");
}
if(res.getIid() != null) {
WHERE("iid=#{iid}");
}
}
}.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public interface UserMapper {
@Result(property = "id", column = "id", id = true),
@Result(property = "tags", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.TagMapper.getTagByUserId")),
@Result(property = "company", column = "id", one = @One(select = "qdu.suvvm.onlinesurvey.mapper.CmpMapper.getCompanyByUserId")),
@Result(property = "investigates", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.InvMapper.getInvByUserId"))
@Result(property = "investigates", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.InvMapper.getInvByUserId")),
@Result(property = "results", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.ResultMapper.getResultByUId"))
})
public List<User> getUser(User user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Investigate {
private User owner;
private Date time;
private List<Tag> tags;
private List<Result> results;

public Integer getId() {
return id;
Expand Down Expand Up @@ -81,6 +82,14 @@ public void setTags(List<Tag> tags) {
this.tags = tags;
}

public List<Result> getResults() {
return results;
}

public void setResults(List<Result> results) {
this.results = results;
}

@Override
public String toString() {
return "Investigate{" +
Expand All @@ -92,6 +101,7 @@ public String toString() {
", owner=" + owner +
", time=" + time +
", tags=" + tags +
", results=" + results +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package qdu.suvvm.onlinesurvey.pojo;

/**
* @ClassName: Result
* @Description: TODO
* @Author: SUVVM
* @Date: 2019/12/2 18:29
*/
public class Result {
private Integer uid;
private Integer iid;
private String ans;

public Integer getUid() {
return uid;
}

public void setUid(Integer uid) {
this.uid = uid;
}

public Integer getIid() {
return iid;
}

public void setIid(Integer iid) {
this.iid = iid;
}

public String getAns() {
return ans;
}

public void setAns(String ans) {
this.ans = ans;
}

@Override
public String toString() {
return "Result{" +
"uid=" + uid +
", iid=" + iid +
", ans='" + ans + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class User {
private Company company;
private List<Tag> tags;
private List<Investigate> investigates;
private List<Result> results;

public Integer getId() {
return id;
Expand Down Expand Up @@ -114,6 +115,14 @@ public void setPower(Integer power) {

public void setInvestigates(List<Investigate> investigates) { this.investigates = investigates; }

public List<Result> getResults() {
return results;
}

public void setResults(List<Result> results) {
this.results = results;
}

@Override
public String toString() {
return "User{" +
Expand All @@ -130,6 +139,7 @@ public String toString() {
", company=" + company +
", tags=" + tags +
", investigates=" + investigates +
", results=" + results +
'}';
}
}

0 comments on commit e364215

Please sign in to comment.