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> </