Spring MVC与Mongodb整合开发实例
程序员文章站
2022-06-06 19:39:27
...
Spring MVC与MongoDB整合开发实例:
1、mongodb层
package com.zhao.sys.mongodb; import com.zhao.sys.domain.User; /** * System - 用户 - mongo层 * * @author Dwen * @version v 0.1 2013-3-19 下午03:38:04 */ public interface IUserMongo { /** * 用户登录 * @param user * @return */ public User login(User user); }
package com.zhao.sys.mongodb.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Repository; import com.zhao.sys.domain.User; import com.zhao.sys.mongodb.IUserMongo; @Repository public class UserMongoImpl implements IUserMongo { @Autowired private MongoTemplate mongoTemplate; /** 用户登录 **/ public User login(User user) { return mongoTemplate.findOne(new Query(Criteria.where("userName").is(user.getUserName())), User.class,"User"); } }
2、Service层
package com.zhao.sys.service; import com.zhao.sys.domain.User; /** * System - 用户 - Service层 * * @author Dwen * @version v 0.1 2013-3-19 下午03:38:30 */ public interface IUserService { /** * 用户登录 * @param user * @return */ public User login(User user); }
package com.zhao.sys.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zhao.sys.domain.User; import com.zhao.sys.mongodb.impl.UserMongoImpl; import com.zhao.sys.service.IUserService; @Service public class UserServiceImpl implements IUserService { @Autowired private UserMongoImpl userMongo; /** * 用户登录 * @param user * @return */ public User login(User user) { User user2 = userMongo.login(user); return user2; } }
3、Controller层
package com.zhao.sys.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.zhao.sina.weibo4j.util.WeiboConfig; import com.zhao.sys.domain.User; import com.zhao.sys.service.impl.UserServiceImpl; /** * System - 用户 - Controller * @author Dwen * @version v 0.1 2013-3-17 下午09:00:05 */ @Controller public class UserController extends BaseController{ @Autowired private UserServiceImpl userService; /** * 登录 */ @RequestMapping("/login") public ModelAndView login(HttpServletRequest request,User user){ User user2 = userService.login(user); ModelAndView mav = new ModelAndView(); if (null==user2) { mav.setViewName("sys/index"); }else{ mav.addObject("user", user2); mav.setViewName("sys/marketing"); } return mav; } }
4、web.xml配置 (该文件放在工程的WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="3.0"> <display-name>Spring3MVC</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5、spring-servlet.xml (该文件放在工程的WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- sys Module--> <context:component-scan base-package="com.zhao.sys.mongodb" /> <context:component-scan base-package="com.zhao.sys.service" /> <context:component-scan base-package="com.zhao.sys.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <mongo:mongo host="localhost" port="27017"></mongo:mongo> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg name="databaseName" value="test"/> </bean> </beans>
6、访问地址http://localhost:8090/login.html
上一篇: java读txt
推荐阅读
-
java中Memcached的使用实例(包括与Spring整合)
-
Spring与Web整合实例
-
Spring MVC+FastJson+hibernate-validator整合的完整实例教程
-
详解MongoDB和Spring整合的实例代码
-
Spring MVC+FastJson+hibernate-validator整合的完整实例教程
-
详解MongoDB和Spring整合的实例代码
-
Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
-
Spring MVC与Spring Boot开发步骤
-
redis与spring整合使用的步骤实例教程
-
Spring3MVC+MyBatis+ExtJs3整合开发系列之一:登录模块演示