@IocBean

@At("/front/userInfo")

public class UserInfoController {

@Inject

private UserInfoService userInfoService;

@At("/getUserInfoByIDNum")

@Ok("json")

@AdaptBy(type = JsonAdaptor.class)

public Object getUserInfoByIDNum(@Param("idNum") String idNum) {

if(Strings.isBlank(idNum)) {

return Result.error("参数缺失");

}

return userInfoService.getUserInfoByIDNum(idNum);

}

@At("/saveUserInfo")

@Ok("json")

@AdaptBy(type = JsonAdaptor.class)

public Object saveUserInfo(@Param("idNum") String idNum, @Param("name") String name,

@Param("phoneNum") String phoneNum) {

if (Strings.isBlank(idNum) || Strings.isBlank(name) || Strings.isBlank(phoneNum)) {

return Result.error("参数缺失");

}

return userInfoService.saveUserInfo(idNum, name, phoneNum);

}

}

@IocBean(args = { "refer:dao" })

public class UserInfoServiceImpl extends BaseServiceImpl implements UserInfoService {

private static final Log log = Logs.get();

@Inject

private UserInfoService userInfoService;

public UserInfoServiceImpl(Dao dao) {

super(dao);

}

@Override

public Result getUserInfoByIDNum(String idNum) {

UserInfo userInfo = this.fetch(idNum);

if (userInfo == null) {

return Result.error("查询不到相关数据");

}

return Result.success("ok", userInfo);

}

@Override

public Result saveUserInfo(String idNum, String name, String phoneNum) {

try {

UserInfo dbInfo = this.fetch(idNum);

if (dbInfo == null) {

UserInfo userInfo = new UserInfo();

userInfo.setId(idNum);

userInfo.setName(name);

userInfo.setPhoneNum(phoneNum);

userInfo.setCreateDate(new Date());

userInfo=this.insert(userInfo);

return Result.success("ok", userInfo);

} else {

dbInfo.setId(idNum);

dbInfo.setName(name);

dbInfo.setPhoneNum(phoneNum);

this.updateIgnoreNull(dbInfo);

return Result.success("ok", dbInfo);

}

} catch (Exception e) {

log.error(e.getMessage());

return Result.error("插入或更新用户信息时出现异常");

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐