亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring+MongoDB實(shí)現(xiàn)登錄注冊(cè)功能

 更新時(shí)間:2017年07月10日 08:54:37   作者:Coder_py  
這篇文章主要為大家詳細(xì)介紹了Spring+MongoDB實(shí)現(xiàn)登錄注冊(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Spring,Spring MVC,MongoDB實(shí)現(xiàn)登錄注冊(cè) 的具體代碼,供大家參考,具體內(nèi)容如下

工程目錄: 

Spring配置文件:

 <beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
   xsi:schemaLocation="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">  
  <!-- 開(kāi)啟自動(dòng)注入,進(jìn)行包掃描 -->
  <context:component-scan base-package="com" />
  <mongo:mongo host="127.0.0.1" port="27017"/> 
  
  <!-- 配置MongoTemplate-->
  <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">  
  <constructor-arg ref="mongo"/> 
  <!-- 指定數(shù)據(jù)庫(kù)名字--> 
  <constructor-arg name="databaseName" value="data1"/>  
  </bean> 
  
  <!-- 映射轉(zhuǎn)換器,掃描back-package目錄下的文件,根據(jù)注釋?zhuān)阉鼈冏鳛閙ongodb的一個(gè)collection的映射 --> 
  <mongo:mapping-converter base-package="com.model" /> 
  
  <!--普通注入--> 
  <bean id="userDaoImpl" class="com.userdaoimpl.UserDaoImpl">  
  <property name="mongoTemplate" ref="mongoTemplate"></property>  
 </bean>  
  
  <context:annotation-config /> 
  
  <bean
 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
 <!-- 配置視圖解析器,將ModelAndView及字符串解析為具體的頁(yè)面 -->
 <bean id="viewResolver"
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/" />
 <property name="suffix" value=".jsp" />
 </bean>
 
 
 
</beans> 

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name> 
 <welcome-file-list>
 <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>
 
 <servlet>
 <servlet-name>spring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>spring</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 <!-- 統(tǒng)一字符編碼 -->
 <filter>
 <filter-name>encoding</filter-name>
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 <init-param>
 <param-name>encoding</param-name>
 <param-value>UTF-8</param-value>
 </init-param>
 </filter>
 
 <filter-mapping>
 <filter-name>encoding</filter-name>
 <url-pattern>*.do</url-pattern>
 </filter-mapping>
</web-app> 

Model層:

package com.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.document.mongodb.mapping.Document;


@Document(collection = "p3") 
public class User {
 @Id
 String name;
 String password;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getPassword() {
 return password;
 }
 public void setPassword(String password) {
 this.password = password;
 }
 
 
 
} 

UserDao:

 package com.userdao;

import com.model.User;

public interface UserDao {
 public void insert(User user);
 public boolean find(String name,String password);
 public void save(User user);
 
 
} 

UserDaoImpl:

package com.userdaoimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;

import com.model.User;
import com.userdao.UserDao;

public class UserDaoImpl implements UserDao{
 
 @Autowired
 MongoTemplate mongoTemplate;
 
 @Override
 public void insert(User user) {
 getMongoTemplate().insert(user);
 
 }
 
 
 
 @Override
 public boolean find(String name, String password) {
 Criteria criteria=Criteria.where("name").is(name).and("password").in(password);
 User user = getMongoTemplate().findOne(new Query(criteria), User.class);
 if (user==null) {
 return false;
 }
 return true;
 }

 @Override
 public void save(User user) {
 getMongoTemplate().save(user);
 
 }


 public MongoTemplate getMongoTemplate() {
 return mongoTemplate;
 }

 public void setMongoTemplate(MongoTemplate mongoTemplate) {
 this.mongoTemplate = mongoTemplate;
 }
 
 
 
} 

LoginController:

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.userdaoimpl.UserDaoImpl;


@Controller
public class LoginController {
 @Autowired
 UserDaoImpl userDaoImpl;
 
 
 @RequestMapping("/login")
 public String login(String name,String password,Model model) {
 boolean rs = userDaoImpl.find(name, password);
 if (rs==true) {
 return "index";
 }
 return "erro";
 
 }


 public UserDaoImpl getUserDaoImpl() {
 return userDaoImpl;
 }


 public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
 this.userDaoImpl = userDaoImpl;
 }
} 

RegisterController:

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.User;
import com.userdaoimpl.UserDaoImpl;


@Controller
public class RegisterController {
 @Autowired 
 UserDaoImpl userDaoImpl;
 @RequestMapping("/register")
 public String register(String name,String password,Model model) {
 User user = new User();
 user.setName(name);
 user.setPassword(password);
 userDaoImpl.save(user);
 return "login";
 }
 public UserDaoImpl getUserDaoImpl() {
 return userDaoImpl;
 }
 public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
 this.userDaoImpl = userDaoImpl;
 }
 
 
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名

    基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名

    這篇文章主要介紹了基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • spring單元測(cè)試之@RunWith的使用詳解

    spring單元測(cè)試之@RunWith的使用詳解

    這篇文章主要介紹了spring單元測(cè)試之@RunWith的使用詳解,@RunWith 就是一個(gè)運(yùn)行器,@RunWith(JUnit4.class) 就是指用JUnit4來(lái)運(yùn)行,
    @RunWith(SpringJUnit4ClassRunner.class),讓測(cè)試運(yùn)行于Spring測(cè)試環(huán)境,需要的朋友可以參考下
    2023-12-12
  • MyBatis如何調(diào)用存儲(chǔ)過(guò)程

    MyBatis如何調(diào)用存儲(chǔ)過(guò)程

    這篇文章主要介紹了MyBatis如何調(diào)用存儲(chǔ)過(guò)程問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Springboot2.0自適應(yīng)效果錯(cuò)誤響應(yīng)過(guò)程解析

    Springboot2.0自適應(yīng)效果錯(cuò)誤響應(yīng)過(guò)程解析

    這篇文章主要介紹了Springboot2.0自適應(yīng)效果錯(cuò)誤響應(yīng)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java設(shè)計(jì)模塊系列之書(shū)店管理系統(tǒng)單機(jī)版(三)

    Java設(shè)計(jì)模塊系列之書(shū)店管理系統(tǒng)單機(jī)版(三)

    這篇文章主要為大家詳細(xì)介紹了Java單機(jī)版的書(shū)店管理系統(tǒng)設(shè)計(jì)模塊和思想第三章,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 四個(gè)Java常見(jiàn)分布式鎖的選型和性能對(duì)比

    四個(gè)Java常見(jiàn)分布式鎖的選型和性能對(duì)比

    當(dāng)涉及到分布式系統(tǒng)中的并發(fā)控制和數(shù)據(jù)一致性時(shí),分布式鎖是一種常見(jiàn)的解決方案,本文將對(duì)幾種常見(jiàn)的分布式鎖實(shí)現(xiàn)原理、實(shí)現(xiàn)示例、應(yīng)用場(chǎng)景以及優(yōu)缺點(diǎn)進(jìn)行詳細(xì)分析,需要的可以參考一下
    2023-05-05
  • Spring中@Value設(shè)置默認(rèn)值問(wèn)題解決

    Spring中@Value設(shè)置默認(rèn)值問(wèn)題解決

    本文主要介紹了Spring中@Value設(shè)置默認(rèn)值問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java遍歷機(jī)制性能的比較詳解

    java遍歷機(jī)制性能的比較詳解

    這篇文章主要給大家介紹了關(guān)于java遍歷機(jī)制性能比較的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Elasticsearch查詢(xún)及聚合類(lèi)DSL語(yǔ)句寶典示例詳解

    Elasticsearch查詢(xún)及聚合類(lèi)DSL語(yǔ)句寶典示例詳解

    這篇文章主要為大家介紹了Elasticsearch查詢(xún)及聚合類(lèi)DSL語(yǔ)句寶典示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • @Scheduled在springboot中的使用方式

    @Scheduled在springboot中的使用方式

    這篇文章主要介紹了@Scheduled在springboot中的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評(píng)論