设为首页 收藏本站
查看: 926|回复: 0

[经验分享] 使用MyEclipse对MongoDB数据库 进行增删改查操作

[复制链接]

尚未签到

发表于 2015-11-11 09:21:55 | 显示全部楼层 |阅读模式
  前面写了一篇MongoDB的下载与安装,接下来此篇写的是MongoDB数据库在JAVA程序中的基本功能:
  使用MyEclipseMongoDB数据库进行增删改查操作

1.导入JAR包
DSC0000.jpg
  
  使用了Spring3.0;此类包,可以上网查询“MongoDB相关JAR包”

2.创建一个Person实体类
  源代码如下:
  

public class Person(){
//属性
Private String id; //id
Private String name; //name
Private int age; //age
//构造方法
public Person(){
}
public Person(String name,int age){
this.name = name;
this.age  = age;
}
public Person(String id,String name,int age){
this.id  = id;
this.name = name;
this.age  = age;
}
public String toString(){
return “Person[ id = ”+id+“name=”+name+“age=”+age +”]”;
}
/**以下为属性的get/set方法**/
.................................
}
/**以上代码中一共有三个构造方法,用于传递参数数据**/

  

DSC0001.jpg



3.创建一个方法接口
  
  1. 创建一个名为AbstractRepository 的接口,源代码如下:
  

public interface AbstractRepository(){
//增加方法
public void insert(Person person);
//按ID查询对象
public Person findOne(String id);
//查询所有
public List<Person> findAll();
//按ID删除
public void removeOne(String id);
//删除所有
public void removeAll();
//查询并修改
public void findAndModfy(String id);
}
DSC0002.jpg
  

4.接口方法实现类
  1.创建一个名为PersonRepository的类并现实AbstractRepository 接口
  源代码如下:
  

public class PersonRepository implements AbstractRepository{
Private MongoTemplate mongoTemplate;
/**mongoTemplate的get/set方法**/
public MongoTemplate getMongoTemplate(){
return mongoTemplate;
}
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
//查询所有
public List<Person> findAll(){
return getMongoTemplate().find(new Query, Person.class);
}

//查询修改
public void findAndModify(){
getMongoTemplate().updateFirst(new Query(Criteria.where(“id”)).is(id) ,
new Update().inc(“age” , 3));
}

//按条件查询
public List<Person> findAll(){
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Criteria criteria = new Criteria(“name”).regex(pattern.toString());
return getMongoTemplate().find(new Query(criteria), Person.class)
}

//按ID查询对象
public Person findOne(String id){
return getMongoTemplate().findOne(new
Query(Criteria.where(“id”).is(id), Person.class));
}

//增加
public void insert(Person person){
getMongoTemplate().insert(person);
}

//删除所有
public void removeAll(){
List<Person>list = this.finAll();
If(list != null){
for(Person person:list){
getMongoTemplate().remove(person)
}
}
}

//按ID删除
public void removeOne(String id){
Criteria criteria = Criteria.where(“id”).in(id);
if(criteria != null){
Query query = new Query(criteria);
if(query != null && getMongoTemplate().findOne(query,Person.class)){
getMongoTemplate().remoev(getMongoTemplate().findOne(query,
Person.class));
}
}
}
}
  

5.配置ApplicationContext.xml
  我们使用了Spring,自然要配置ApplicationContext;因为各版本不同,所以ApplicationContext.xml中的系统默认头代码请复制我以下的来使用,要不然会代码错误:
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;          xmlns:context=&quot;http://www.springframework.org/schema/context&quot;            xmlns:mongo=&quot;http://www.springframework.org/schema/data/mongo&quot;  xsi:schemaLocation=&quot;http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd        http://www.springframework.org/schema/data/mongo         http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd  
http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;>  
<!--以下为Spring配置-->
<!--设置MongoDB的连接端口-->
<mongo:mongo host=&quot;localhost&quot; port=&quot;27017&quot;></mongo:mongo>
<bean
id=&quot;mongoTemplate&quot; class=&quot;org.springframework.data.document.mongodb.MongoTemplate&quot;>
<constructor-arg ref=&quot;mongo&quot;/>
<constructor-arg name=&quot;databaseName&quot; value=&quot;db&quot;/>
<constructor-arg name=&quot;defaultCollectionName&quot; value=&quot;person&quot;/ >
</bean>
<!--配置bean,把PersonReposiory的实现类注入-->
<bean
id=&quot;personRepository&quot; class=&quot;com.mongo.repository.PersonRepository&quot;>
<property name=&quot;mongoTemplate&quot; ref=&quot;mongoTemplate&quot;/>
</bean>
</beans>
  
  ApplicationContext.xml代码如图,虽然左上角出现红X的错误提示。提示的
  信息大概是有重复的注释。不用去管他,只要代码里面没有红线就好。
DSC0003.jpg



6.写测试类进行测试
  创建一个有mian方法的测试类Test:部分方法的源代码如下:
  

public class Test{
private static log log = logFactory.getlog(Test.class.getName());
private AbstractRepository pr = null;
//初始方法
public void init(){
log.debug(“开始启动”);
ApplicationContext ac = new ClassPathXmlApplicationContext(
“applicationContext.xml”);
pr = (PersonRepository) ac.getbean(“personRepository”);
}
//添加方法
public void insert(){
Person p = new Person(“id1”,“cuiran”,27);
pr.insert(p);
log.debug(“添加成功!”);
}
//按ID查询对象
public void finOne(){
String id = “id1”;
Person p =  pr.findOne(id);
log.debug(p);
}
}
//查询所有
public void findAll(){
List<Person> list = pr.findAll();
log.debug(“查询结果:”);
for(Person p:list){
log.debug(p.toString);
}
}
//测试方法
public void start(){
init(); //执行初始化
insert(); //执行添加方法
}
//mian方法
public static void main(String args []){
Text text = new Text();
test.start();
}

  

版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-137754-1-1.html 上篇帖子: java代码中find()函数访问MongoDB数据库 下篇帖子: MongoDB无法启动(Error: couldn't connect to server 127.0.0.1:27017 src/mongo/ shell/m
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表