spring mvc+spring data+mongodb实例1_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > spring mvc+spring data+mongodb实例1

spring mvc+spring data+mongodb实例1

 2014/5/19 9:28:18  lihao312  程序员俱乐部  我要评论(0)
  • 摘要:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www
  • 标签:MVC Spring 实例 MongoDB
class="java" name="code">
<?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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/data/mongo 
       http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd">

	<context:component-scan base-package="com.lance" />

	<mongo:mongo host="localhost" port="27017" />
	<mongo:db-factory dbname="test" />

	<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
	
	<!-- 通过配置typeKey null 用来处理save记录的_class字段不保存 -->
	<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
		<constructor-arg name="typeKey">
			<null />
		</constructor-arg>
	</bean>

	<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
		<constructor-arg name="mappingContext" ref="mappingContext" />
		<property name="typeMapper" ref="defaultMongoTypeMapper" />
	</bean>
	
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
		<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
	</bean>
	
	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>


mongo模糊查询
/**
  * 根据Name模糊查询用户
  * 2014年5月16日 下午5:45:03
  * @return
*/
	public List<UserInfo> findByLikeName(String name){
		Query query = new BasicQuery("{'name': {$regex : '" + name + "'} }");
		return mongoTemplate.find(query, UserInfo.class, COLLECTION_NAME);
	}
  • lance.zip (24.2 KB)
  • 下载次数: 0
发表评论
用户名: 匿名