Current location - Health Preservation Learning Network - Slimming men and women - How to create an application by connecting to a data store using spring data or a local java adapter?
How to create an application by connecting to a data store using spring data or a local java adapter?
Spring Data provides a set of API for accessing MongoDB data with Java, which enables us to operate MongoDB in a way similar to Spring's JdbcTemplate class, greatly simplifying our development steps.

I. Preparatory work

As usual, we need to prepare a series of jar packages first. I cut a picture here, as shown below.

Which jar bag? Commons-beans- 1.8.3,commons-lang-2.6,Commons-collections-3.2 . 1,Commons-logging- 1. 1,Ezmorph- 1。

Second, the database connection configuration

There are two ways to connect MongoDB database with Spring data: one is through JDBC connection, and the other is through Spring reading XML configuration file connection.

The first connection mode is introduced here, and the second connection mode will be introduced later.

1. Create the configuration file "DataSource.properties" to store the database information for later modification. The content is as follows:

Host = 127.0.0. 1

Port=270 17

Database = King

User name = King

Password = King

The above configuration is: server address where the target database is located, database connection port, database name, user name and password. * Please configure according to the actual situation.

2. Get a database connection

The interfaces and classes responsible for database connection are not introduced here. Interested parties can click on the link to view the official document: Interface? MongoOperations? And class? MongoTemplate. Among them, the MongoTemplate class implements the interface MongoOperations.

Below I attach the source code of the MongoUtil class I wrote. This class mainly encapsulates the code to get an instance of MongoTemplate.

Parcel? com . report . util;

Import? Java . net . unknown host exception;

Import? Java . util . locale;

Import? Java . util . resource bundle;

Import? org . spring framework . data . authentic ation . user credentials;

Import? org . spring framework . data . MongoDB . core . mongooperations;

Import? org . spring framework . data . MongoDB . core . mongotemplate;

Import? org . spring framework . data . MongoDB . core . simplemongodbfactory;

Import? com . MongoDB . mongo;

Public? Class? Mongotil? {

Private? Variable? Static electricity MongoOperations? mongoOperation

Private? Static electricity Final? String? Profile name? =? "data source";

Private? Static electricity Final? String? KEY_HOST? =? "host";

Private? Static electricity Final? String? KEY_PORT? =? "port";

Private? Static electricity Final? String? KEY_DATABASE? =? "database";

Private? Static electricity Final? String? KEY_USERNAME? =? "User name";

Private? Static electricity Final? String? KEY_PASSWORD? =? "password";

Private? MongoUtil()? {

}

Public? Static electricity MongoOperations? getMongoOperation()? {

What if? (mongoOperation? ==? null)? {

Resource bundle? Resource bundle? =? ResourceBundle.getBundle(

Profile name? locale . get default()); //Get the configuration file "DataSource.properties"

String? hostAddr? =? resource bundle . getstring(KEY _ HOST)? +? ":"

+? resource bundle . getstring(KEY _ PORT);

User credentials? User credentials? =? New? User credentials (

resource bundle . getstring(KEY _ USERNAME),

resource bundle . getstring(KEY _ PASSWORD));

Try it? {

mongoOperation? =? New? MongoTemplate(

New? SimpleMongoDbFactory (new? Mongo(hostAddr),

resource bundle . getstring(KEY _ DATABASE),

user credentials));

}? Catch? (UnknownHostException? e)? {

e . printstacktrace();

}

}

Return? mongoOperation

}

}3. Create test data databases, tables and users in MongoDB.

When creating a database in MongoDB, the "Use XXX" command is used, so some extra operations are needed before creating the database. Here, you choose to create the table "king", if not, you can use "db.king.drop ()" to delete the table. Click here to get the detailed instruction document of MongoDB, or you can enter "help" for help.

4. Write test code to verify the database connection.

Create a test class TestMongoDB.java.

Parcel? com.report.Tests

Import? org . spring framework . data . MongoDB . core . query . criteria;

Import? org . spring framework . data . MongoDB . core . query . query;

Import? com . report . util . mongoutil;

Public? Class? TestMongoDB? {

Private? String? Name;

Private? String? Surname;

TestMongoDB (string? Name? String? Last name? ){

This. Name? =? Name;

This last name? =? Surname;

}

Public? String? getFirstName()? {

Return? Name;

}

Public? Invalid? setFirstName(String? Name)? {

This. Name? =? Name;

}

Public? String? getLastName()? {

Return? Surname;

}

Public? Invalid? SetLastName (string? Last name)? {

This last name? =? Surname;

}

Public? Static electricity Invalid? main(String[]? args){

TestMongoDB? Testing? =? New? TestMongoDB("John ","Connor ");

MongoUtil.getMongoOperation()。 Insert (test);

TestMongoDB? Storage? =? MongoUtil.getMongoOperation()。 FindOne (new? Query (Criteria.where("firstName "). is(test.getFirstName()),

testmongodb . class);

What if? (storage? ! =? Empty)

system . out . println(stored . get first name()? +? "?" ? +? stored . get last name());

}

}Eclipse console output information:? John Connor

Verify database records: