今天就半天时间了,感慨一下;最近一年多都忙的不可开交;今天终于可有点儿时间,可以着手写点东西了.....非常感谢工作和生活.....
结合自身项目特点,合理组织自身代码
wind7 、jdk1.8、IJ EDEA 2017(实际开发中2016版本,在集成kafka的过程中始终报错,巨坑)
安装mongoDB (不说)
整体配置:org.springframework.boot spring-boot-starter-data-mongodb org.springframework.data spring-data-mongodb 1.10.4.RELEASE
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
4.0.0
com.qhzx.sdip
sdip-config-center
1.0-SNAPSHOT
war
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-mongodb
com.alibaba
fastjson
1.2.35
commons-codec
commons-codec
1.10
commons-collections
commons-collections
3.2.2
org.apache.httpcomponents
httpclient
4.5.3
org.apache.commons
commons-lang3
3.6
org.springframework.data
spring-data-mongodb
1.10.4.RELEASE
commons-beanutils
commons-beanutils
1.9.3
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.1.2
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import
org.springframework.boot
spring-boot-maven-plugin
#mongodb配置 spring.data.mongodb.uri=mongodb://username:password@IP:PORT/mongoDBName
import com.mongodb.DBCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
/**
* 类描述
*
* @author YUANYIRUI839
*/
@Component("mgTemplate")
public class MgTemplate {
@Autowired
private MongoTemplate mongoTemplate;
public DBCollection getCollection(String collectionName) {
return mongoTemplate.getCollection(collectionName);
}
}
import com.mongodb.BasicDBList;
import com.mongodb.DBObject;
/**
* 类描述
* 用户管理:
* 1.分配用户:机构号,手机,法人身份证,联系人,状态(启用,禁用),描述-->>分配用户和密码
* 2.更新用户:
* 3.找回用户:机构号,手机,法人身份证
*
* @created by yuanyirui839 20170904
*/
public interface IUserService {
//新增用户 返回 用户名和密码
int addUser(String orgCode, String mobileNum, String IDCard, String contact, String userId, String pwd);
//查询用户(找回用户)
DBObject queryUser(String userId, String orgCode, String mobileNum, String IDCard);
//删除(禁用和启用)
int operateUser(String orgCode, String status);
//更新
int updateUser(String userId, String orgCode, String mobileNum, String IDCard, String contact);
//更新密码
int updateUserPwd(String orgCode, String oldPwd, String newPwd);
//删除用户
int deleteUser(String orgCode, String userId);
}
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 类描述
* 用户管理:
* 1.分配用户:机构号,手机,法人身份证,联系人,状态(启用,禁用),描述-->>分配用户和密码
* 2.更新用户:
* 3.找回用户:机构号,手机,法人身份证
*
* @created by yuanyirui839 20170904
*/
@Component("userService")
public class UserService implements IUserService {
private static Log logger = LogFactory.getLog(UserService.class);
//商户用户
static String COLLECTION_NAME = "ORG_USER";
@Autowired
MgTemplate mgTemplate;
//新增用户 返回 用户名和密码
public int addUser(String orgCode, String mobileNum, String IDCard, String contact, String userId, String pwd) {
//构建文档(本身是面向文档存储)
DBObject document = new BasicDBObject();
document.put(Const.ORG_CODE, orgCode);
document.put(Const.MOBILE_NUM, mobileNum);
document.put(Const.ID_CARD, IDCard);
document.put(Const.CONTACT, contact);
document.put(Const.USER_ID, userId);
document.put(Const.USER_PWD, pwd);
//设置为使用中
document.put(Const.STATUS, Const.INUSE);
//存入的时候,注意相同的数据只保存一条
document.put(Const._ID, orgCode);
//获取集合,在集合中插入数据 getN()返回受影响的条数
int row = mgTemplate.getCollection(COLLECTION_NAME).save(document).getN();
return row;
}
//查询用户(找回用户信息)
public DBObject queryUser(String userId, String orgCode, String mobileNum, String idCard) {
DBObject query = buildQuery(userId, orgCode, mobileNum, idCard);
DBObject dbObject = new BasicDBObject();
dbObject.put(Const._ID, 0);
DBObject result = mgTemplate.getCollection(COLLECTION_NAME).findOne(query, dbObject);
return result;
}
//构造查询条件
private DBObject buildQuery(String userId, String orgCode, String mobileNum, String idCard) {
DBObject query = new BasicDBObject();
if (StringUtils.isNotBlank(userId)) {
query.put(Const.USER_ID, userId);
}
if (StringUtils.isNotBlank(orgCode)) {
query.put(Const.ORG_CODE, orgCode);
}
if (StringUtils.isNotBlank(mobileNum)) {
query.put(Const.MOBILE_NUM, mobileNum);
}
if (StringUtils.isNotBlank(idCard)) {
query.put(Const.ID_CARD, idCard);
}
return query;
}
//删除(禁用和启用)
public int operateUser(String orgCode, String status) {
DBObject update = queryUser("", orgCode, "", "");
//构建文档(本身是面向文档存储)
DBObject query = new BasicDBObject();
//存入的时候,注意相同的数据只保存一条
query.put(Const.ORG_CODE, orgCode);
update.put(Const.STATUS, status);
//获取集合,在集合中插入数据 getN()返回受影响的条数
int row = mgTemplate.getCollection(COLLECTION_NAME).update(query, update).getN();
return row;
}
//更新
public int updateUser(String userId, String orgCode, String mobileNum, String idCard, String contact) {
DBObject document = queryUser(userId, orgCode, "", "");
logger.info("返回值:"+document.toString());
document.put(Const.MOBILE_NUM, mobileNum);
document.put(Const.ID_CARD, idCard);
document.put(Const.CONTACT, contact);
//存入的时候,注意相同的数据只保存一条
document.put(Const._ID, orgCode);
//设置为使用中
document.put(Const.STATUS, Const.INUSE);
//获取集合,在集合中插入数据 getN()返回受影响的条数
int row = mgTemplate.getCollection(COLLECTION_NAME).save(document).getN();
return row;
}
//更新密码
public int updateUserPwd(String orgCode, String oldPwd, String newPwd) {
DBObject query = new BasicDBObject();
query.put(Const.ORG_CODE, orgCode);
DBObject update = queryUser("", orgCode, "", "");
update.put(Const.USER_PWD, newPwd);
//获取集合,在集合中插入数据 getN()返回受影响的条数
int row = mgTemplate.getCollection(COLLECTION_NAME).update(query, update).getN();
return row;
}
//删除
@Override
public int deleteUser(String orgCode, String userId) {
DBObject query = new BasicDBObject();
if (StringUtils.isNotBlank(orgCode)) {
query.put(Const.ORG_CODE, orgCode);
}
if (StringUtils.isNotBlank(userId)) {
query.put(Const.USER_ID, userId);
}
//获取集合,在集合中插入数据 getN()返回受影响的条数
int row = mgTemplate.getCollection(COLLECTION_NAME).remove(query).getN();
return row;
}
}
@RestController @RequestMapping("dsint")代码:
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by YUANYIRUI839 on 2017-09-04.
* 用户配置中心
*/
@RestController
@RequestMapping("userConfCenter")
public class UserConfigCenter {
private static Log logger = LogFactory.getLog(UserConfigCenter.class);
@Autowired
private IUserService userService;
/**
* 新增用户;机构可以申请或者系统分配用户名和密码
*/
@ResponseBody
@RequestMapping("/addUser")
public AjaxResult addUser(String orgCode, String mobileNum, String IDCard, String contact, String userId, String userPwd) {
AjaxResult result = AjaxResult.createSuccAjaxResult();
try {
if (StringUtils.isBlank(orgCode)) {
return AjaxResult.createErrorAjaxResult("请填写组织机构代码");
}
if (StringUtils.isBlank(userId) && StringUtils.isBlank(userPwd)) {
boolean existUserId = false;
while (!existUserId) {
userId = RandomStringUtils.random(8, Const.RANDOM_STR_UP);
logger.info("生成用户ID:" + userId);
existUserId = queryUserByUserId(userId) == null ? true : false;
}
//生成密码
userPwd = RandomStringUtils.random(8, Const.RANDOM_STR);
}
//添加用户
int addRow = userService.addUser(orgCode, mobileNum, IDCard, contact, userId, userPwd);
DBObject dbObject=new BasicDBObject();
dbObject.put(Const.USER_ID,userId);
dbObject.put(Const.USER_PWD,userPwd);
result.setData(dbObject);
logger.info("返回行数: " + dbObject.toString());
return result;
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.createErrorAjaxResult(e.getMessage());
}
}
private DBObject queryUserByUserId(String userId) {
DBObject dbObject = userService.queryUser(userId, "", "", "");
return dbObject;
}
/**
* 删除用户
*/
@ResponseBody
@RequestMapping("/deleteUser")
public AjaxResult deleteUser(String orgCode, String userId) {
AjaxResult result = AjaxResult.createSuccAjaxResult();
try {
//
int operateRow = userService.deleteUser(orgCode, userId);
result.setData(operateRow);
logger.info("删除用户影响的条数 : " + operateRow);
return result;
} catch (Exception e) {
return AjaxResult.createErrorAjaxResult(e.getMessage());
}
}
/**
* 更新用户信息
*/
@ResponseBody
@RequestMapping("/updateUser")
public AjaxResult updateUser(String userId, String orgCode, String mobileNum, String IDCard, String contact) {
AjaxResult result = AjaxResult.createSuccAjaxResult();
try {
//更新
int updateRow = userService.updateUser(userId, orgCode, mobileNum, IDCard, contact);
result.setData(updateRow);
logger.info("更新影响的条数 : " + updateRow);
return result;
} catch (Exception e) {
return AjaxResult.createErrorAjaxResult(e.getMessage());
}
}
}