Springboot +Mongodb配置
1. MongoDB在pom.xml檔案中的依賴
springboot 版本 2.0.7
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.application.properties配置
mongodb.primary.database= admin//資料庫名
mongodb.primary.port= 27017//埠
mongodb.primary.host=127.0.0.1//ip地址
3. 配置AbstractMongoConfig類
獲取mongodbDB地址(ServerAddress)->獲取mongoDB證書(MongoCredential,使用者名稱、資料庫名、密碼)->連線mongoDB服務(MongoClient)->建立MongoDbFactory(SimpleMongoDbFactory)
連線到MongoDB服務
如果是遠端連線可以替換“localhost”為伺服器所在IP地址
ServerAddress()兩個引數分別為 伺服器地址 和 埠
ServerAddress serverAddress = new ServerAddress(host, port);
獲取mongoDB證書
List mongoCredentialList = new ArrayList<>();
mongoCredentialList.add(MongoCredential.createCredential(username, database, password.toCharArray()));
三個引數分別為 使用者名稱 資料庫名稱 密碼
連線mongoDB服務
MongoClient mongoClient = new MongoClient(serverAddress, mongoCredentialList);
//MongoClient Springboot2.1.0版本不可用
建立MongoDbFactory
SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoClient, database);
public abstract class AbstractMongoConfig {
private String database,host,username,password;
private int port;
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public MongoDbFactory primaryMongoDbFactory() {
ServerAddress serverAddress = new ServerAddress(host, port);
List<MongoCredential> mongoCredentialList = new ArrayList<>();
mongoCredentialList.add(MongoCredential.createCredential(username, database, password.toCharArray()));
MongoClient mongoClient = new MongoClient(serverAddress, mongoCredentialList);
SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoClient, database);
return simpleMongoDbFactory;
}
abstract public MongoTemplate getMongoTemplate() throws Exception;
}
4.配置primaryMongoConfig
@ConfigurationProperties(prefix="mongodb.primary")
public class PrimaryMongoConfig extends AbstractMongoConfig{
@Bean(name="primaryMongoTemplate")
public MongoTemplate getMongoTemplate() throws Exception {
// TODO Auto-generated method stub
return new MongoTemplate(primaryMongoDbFactory());
}
}
5.配置repository類
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private MongoTemplate primaryMongoTemplate;
private final static String COLLECTION_NAME = "user"
@Override
public User findOneUser(String username, String pwd) throws Exception {
// TODO Auto-generated method stub
Query query = new Query(Criteria.where("username").is(username));
query.addCriteria(Criteria.where("pwd").is(pwd));
return primaryMongoTemplate.findOne(query, User.class, COLLECTION_NAME);
}
}
注意:COLLECTION_NAME 為MongoDB表名
當有多個數據源時,就要加上@Qualifier(bean=“secondaryMongoTemplate”)
@Autowired
@Qualifier(bean="secondaryMongoTemplate")
private MongoTemplate secondaryMongoTemplate;
6 controller類
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping("login.do")
public String login(Model model,@RequestParam String username,@RequestParam String pwd) throws Exception{
User u = userRepository.findOneUser(username, pwd);
if(!StringUtil.isEmpty(u)) {
model.addAttribute("user", u);
return "success";
}
else {
return "index";
}
}
}
第一次寫部落格,權當給正在學習的知識做個筆記,記錄下自己的學習過程
知識不僅需要學習,更需要總結。