Flask接口签名sign原理与实例代码浅析
293
2023-04-28
Spring+MongoDB实现登录注册功能
本文实例为大家分享了Spring,Spring MVC,MongoDB实现登录注册 的具体代码,供大家参考,具体内容如下
工程目录:
Spring配置文件:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mongo="http://springframework.org/schema/data/mongo" xsi:schemaLocation="http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/data/mongo http://springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd"> class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mongo="http://springframework.org/schema/data/mongo"
xsi:schemaLocation="http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/data/mongo
http://springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd">
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
Web.xml:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://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">
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://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">
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;
importhttp:// 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.sehttp://tPassword(password);
userDaoImpl.save(user);
return "login";
}
public UserDaoImpl getUserDaoImpl() {
return userDaoImpl;
}
public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
this.userDaoImpl = userDaoImpl;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~