Skip to content

Commit

Permalink
添加用户与公司一对一映射
Browse files Browse the repository at this point in the history
  • Loading branch information
suvvm committed Nov 25, 2019
1 parent 5d82a0a commit 82a2924
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@
public interface CmpMapper {
// 根据动态sql查询company
@SelectProvider(type = CmpMapperProvider.class, method="findCmp")
@Results({
@Result(property = "id", column = "id", id = true),
@Result(property = "owner", column = "id", one = @One(select = "qdu.suvvm.onlinesurvey.mapper.UserMapper.getUserByCompanyId"))
})
public List<Company> getCompanies(Company cmp);

// 根据id删除company
@Delete("delete from companies where id=#{id}")
public int deleteCmpById(Integer id);

// 插入user并获取自增id
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into companies(name,description,forms,domain,ownerid) values(#{name},#{description},#{forms},#{domain},#{owner.id})")
public int insertCmp(Company cmp);

// 根据id更新company
@UpdateProvider(type = CmpMapperProvider.class, method = "updateCmp")
public int updateCmp(Company cmp);

@Select("select * from companies where ownerid = #{id}")
public Company getCompanyByUserId(Integer id);

/**
* @ClassName: CmpMapperProvider
* @Description: company动态sql的Provider类
Expand Down Expand Up @@ -80,6 +91,9 @@ public String updateCmp(Company cmp) {
if(cmp.getDomain() != null) {
SET("domain = #{domain}");
}
if(cmp.getOwner() != null) {
SET("ownerid = #{owner.id}");
}
WHERE("id = #{id}");
}
}.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package qdu.suvvm.onlinesurvey.mapper;

import javafx.beans.binding.When;
import jdk.nashorn.internal.objects.annotations.Where;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import qdu.suvvm.onlinesurvey.pojo.User;
Expand All @@ -19,7 +17,8 @@ public interface UserMapper {
@SelectProvider(type = UserMapperProvider.class, method="findUser")
@Results({
@Result(property = "id", column = "id", id = true),
@Result(property = "tags", column = "id", many = @Many(select = "qdu.suvvm.onlinesurvey.mapper.TagMapper.getTagByUserId"))
@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"))
})
public List<User> getUser(User user);

Expand All @@ -44,6 +43,10 @@ public interface UserMapper {
@Insert("insert into usertag values(#{uid},#{tid})")
public int insertUserTag(Integer uid, Integer tid);

// 根据公司id查询用户
@Select("select * from users where cmp_id=#{id}")
public User getUserByCompanyId(Integer id);

/**
* @ClassName: UserMapperProvider
* @Description: user动态sql的Provider类
Expand All @@ -57,21 +60,6 @@ class UserMapperProvider {
* @Parameter:
* user 查询数据封装的用户类
* @Return: 返回对应的sql语句
*/
/*
select
*
from
user b
  inner join
   tags m
  on
   b.bid=m.m_bid
  inner join
   category c
  on
   m.m_cid=c.cid
*/
public String findUser(User user) {
return new SQL() {
Expand Down Expand Up @@ -102,6 +90,9 @@ public String findUser(User user) {
if(user.getPower() != null) {
WHERE("power = #{power}");
}
if(user.getCompany() != null) {
WHERE("cmp_id = #{company.id}");
}
}
}.toString();
}
Expand Down Expand Up @@ -143,6 +134,9 @@ public String updateUser(User user) {
if(user.getPower() != null) {
SET("power = #{power}");
}
if(user.getCompany() != null) {
SET("cmp_id = #{company.id}");
}
WHERE("id = #{id}");
}
}.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ public User getOwner() {
public void setOwner(User owner) {
this.owner = owner;
}

@Override
public String toString() {
return "Company{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
", forms='" + forms + '\'' +
", domain='" + domain + '\'' +
", owner=" + owner +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class User {
private String avatar;
private String imgbase64;
private Integer power;

private Company company;
private List<Tag> tags;

public Integer getId() {
Expand Down Expand Up @@ -99,13 +101,13 @@ public void setPower(Integer power) {
this.power = power;
}

public List<Tag> getTags() {
return tags;
}
public Company getCompany() { return company; }

public void setTags(List<Tag> tags) {
this.tags = tags;
}
public void setCompany(Company company) { this.company = company; }

public List<Tag> getTags() { return tags; }

public void setTags(List<Tag> tags) { this.tags = tags; }

@Override
public String toString() {
Expand All @@ -120,6 +122,7 @@ public String toString() {
", avatar='" + avatar + '\'' +
", imgbase64='" + imgbase64 + '\'' +
", power=" + power +
", company=" + company +
", tags=" + tags +
'}';
}
Expand Down

0 comments on commit 82a2924

Please sign in to comment.