1、开发环境
JDK:1.6
ssm框架关联jar包:
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
commons-beanutils-1.9.2.jar
commons-codec-1.9.jar
commons-collections-3.2.1.jar
commons-dbcp-1.4.jar
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-logging-1.2.jar
commons-net-3.1.jar
commons-pool-1.6.jar
commons-pool2-2.2.jar
druid-1.0.9.jar
fastjson-1.1.39.jar
freemarker-2.3.19.jar
hamcrest-core-1.3.jar
jackson-all-1.9.5.jar
jboss-logging-3.1.0.CR2.jar
jettison-1.0.1.jar
jstl-1.1.2.jar
junit-4.11.jar
log4j-1.2.17.jar
log4j-over-slf4j-1.7.7.jar
mybatis-3.2.6.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.30-bin.jar
servlet-api.jar
slf4j-api-1.7.7.jar
slf4j-ext-1.7.7.jar
spring-aop-4.0.2.RELEASE.jar
spring-aspects-4.0.2.RELEASE.jar
spring-beans-4.0.2.RELEASE.jar
spring-context-4.0.2.RELEASE.jar
spring-context-support-4.0.2.RELEASE.jar
spring-core-4.0.2.RELEASE.jar
spring-expression-4.0.2.RELEASE.jar
spring-jdbc-4.0.2.RELEASE.jar
spring-oxm-4.0.2.RELEASE.jar
spring-test-4.0.2.RELEASE.jar
spring-tx-4.0.2.RELEASE.jar
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.2.RELEASE.jar
standard-1.1.2.jar
mongo-java-driver-2.10.1.jar
spring-data-commons-core-1.4.0.RELEASE.jar
spring-data-mongodb-1.1.0.RELEASE.jar
项目结构图:
2、配置文件和实列代码
web.xml文件
contextConfigLocation
classpath:spring/spring-context.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springMVC
/
spring-mvc.xml
text/html;charset=UTF-8
package com.wlsq.oauth.dao.support;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.mongodb.core.MongoTemplate;
public class AbstractBaseMongoTemplete implements ApplicationContextAware {
protected MongoTemplate mongoTemplate;
/**
* 设置mongoTemplate
* @param mongoTemplate the mongoTemplate to set
*/
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
MongoTemplate mongoTemplate = applicationContext.getBean("mongoTemplate", MongoTemplate.class);
setMongoTemplate(mongoTemplate);
}
}
package com.wlsq.oauth.dao.impl;
import java.util.List;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.wlsq.oauth.dao.IUserDao;
import com.wlsq.oauth.dao.support.AbstractBaseMongoTemplete;
import com.wlsq.oauth.entity.User;
public class UserDao extends AbstractBaseMongoTemplete implements IUserDao {
/**
* 新增
*
------------------------------
* @param user
*/
public void insert(User user) {
// TODO Auto-generated method stub
mongoTemplate.insert(user);
}
/**
* 批量新增
*
------------------------------
* @param users
*/
public void insertAll(List users) {
// TODO Auto-generated method stub
mongoTemplate.insertAll(users);
}
/**
* 删除,按主键id, 如果主键的值为null,删除会失败
*
------------------------------
* @param id
*/
public void deleteById(String id) {
// TODO Auto-generated method stub
User user = new User(id, null, 0);
mongoTemplate.remove(user);
}
/**
* 按条件删除
*
------------------------------
* @param criteriaUser
*/
public void delete(User criteriaUser) {
// TODO Auto-generated method stub
Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());;
Query query = new Query(criteria);
mongoTemplate.remove(query, User.class);
}
/**
* 删除全部
*
------------------------------
*/
public void deleteAll() {
// TODO Auto-generated method stub
mongoTemplate.dropCollection(User.class);
}
/**
* 按主键修改,
* 如果文档中没有相关key 会新增 使用$set修改器
*
------------------------------
* @param user
*/
public void updateById(User user) {
// TODO Auto-generated method stub
Criteria criteria = Criteria.where("id").is(user.getId());
Query query = new Query(criteria);
Update update = Update.update("age", user.getAge()).set("name", user.getName());
mongoTemplate.updateFirst(query, update, User.class);
}
/**
* 修改多条
*
------------------------------
* @param criteriaUser
* @param user
*/
public void update(User criteriaUser, User user) {
// TODO Auto-generated method stub
Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());;
Query query = new Query(criteria);
Update update = Update.update("name", user.getName()).set("age", user.getAge());
mongoTemplate.updateMulti(query, update, User.class);
}
/**
* 根据主键查询
*
------------------------------
* @param id
* @return
*/
public User findById(String id) {
// TODO Auto-generated method stub
return mongoTemplate.findById(id, User.class);
}
/**
* 查询全部
*
------------------------------
* @return
*/
public List findAll() {
// TODO Auto-generated method stub
return mongoTemplate.findAll(User.class);
}
/**
* 按条件查询, 分页
*
------------------------------
* @param criteriaUser
* @param skip
* @param limit
* @return
*/
public List find(User criteriaUser, int skip, int limit) {
// TODO Auto-generated method stub
Query query = getQuery(criteriaUser);
query.skip(skip);
query.limit(limit);
return mongoTemplate.find(query, User.class);
}
/**
* 根据条件查询出来后 再去修改
*
------------------------------
* @param criteriaUser 查询条件
* @param updateUser 修改的值对象
* @return
*/
public User findAndModify(User criteriaUser, User updateUser) {
// TODO Auto-generated method stub
Query query = getQuery(criteriaUser);
Update update = Update.update("age", updateUser.getAge()).set("name", updateUser.getName());
return mongoTemplate.findAndModify(query, update, User.class);
}
/**
* 查询出来后 删除
*
------------------------------
* @param criteriaUser
* @return
*/
public User findAndRemove(User criteriaUser) {
// TODO Auto-generated method stub
Query query = getQuery(criteriaUser);
return mongoTemplate.findAndRemove(query, User.class);
}
/**
* count
*
------------------------------
* @param criteriaUser
* @return
*/
public long count(User criteriaUser) {
// TODO Auto-generated method stub
Query query = getQuery(criteriaUser);
return mongoTemplate.count(query, User.class);
}
/**
*
*
------------------------------
* @param criteriaUser
* @return
*/
private Query getQuery(User criteriaUser) {
if (criteriaUser == null) {
criteriaUser = new User();
}
Query query = new Query();
if (criteriaUser.getId() != null) {
Criteria criteria = Criteria.where("id").is(criteriaUser.getId());
query.addCriteria(criteria);
}
if (criteriaUser.getAge() > 0) {
Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());
query.addCriteria(criteria);
}
if (criteriaUser.getName() != null) {
Criteria criteria = Criteria.where("name").regex("^" + criteriaUser.getName());
query.addCriteria(criteria);
}
return query;
}
}
package com.wlsq.oauth.dao;
import java.util.List;
import com.wlsq.oauth.entity.User;
public interface IUserDao {
/**
* 新增
*
------------------------------
* @param user
*/
void insert(User user);
/**
* 新增
*
------------------------------
* @param users
*/
void insertAll(List users);
/**
* 删除,主键id, 如果主键的值为null,删除会失败
*
------------------------------
* @param id
*/
void deleteById(String id);
/**
* 按条件删除
*
------------------------------
* @param criteriaUser
*/
void delete(User criteriaUser);
/**
* 删除全部
*
------------------------------
*/
void deleteAll();
/**
* 修改
*
------------------------------
* @param user
*/
void updateById(User user);
/**
* 更新多条
*
------------------------------
* @param criteriaUser
* @param user
*/
void update(User criteriaUser, User user);
/**
* 根据主键查询
*
------------------------------
* @param id
* @return
*/
User findById(String id);
/**
* 查询全部
*
------------------------------
* @return
*/
List findAll();
/**
* 按条件查询
*
------------------------------
* @param criteriaUser
* @param skip
* @param limit
* @return
*/
List find(User criteriaUser, int skip, int limit);
/**
* 根据条件查询出来后 在去修改
*
------------------------------
* @param criteriaUser 查询条件
* @param updateUser 修改的值对象
* @return
*/
User findAndModify(User criteriaUser, User updateUser);
/**
* 查询出来后 删除
*
------------------------------
* @param criteriaUser
* @return
*/
User findAndRemove(User criteriaUser);
/**
* count
*
------------------------------
* @param criteriaUser
* @return
*/
long count(User criteriaUser);
}
package com.wlsq.oauth.entity;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2634064977259616340L;
private String id;
private String name;
private int age;
public User() {
}
public User(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}