springmvc+mybatis+sitemesh3主要配置

转自:http://blog.csdn.net/sust2012/article/details/8464458

第一步

配置web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app>
<display-name>Archetype Created Web Application</display-name>




<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>


<!-- 增加对sitemesh的支持 -->
<filter> 
<filter-name>sitemesh3</filter-name> 
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> 
</filter> 






<!-- spring对字符集进行过滤 -->
<filter>
<filter-name>encodingFilter</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>sitemesh3</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping> 

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- 加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>






<!-- spring mvc -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>






<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>


<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


</web-app>


第二步  

对springmvc 的支持(dispatch-servlet.xml)

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


<!-- 增加对annotation的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>


<!-- 自动扫描controll -->
<context:component-scan base-package="com.web.frontpage.controller"></context:component-scan>


<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>




<!-- 增加对json的支持 -->
<bean id="convereJson"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>


<bean id="anotationAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="convereJson" />
</list>
</property>
</bean>


<!-- 增加对上传的支持 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1024000"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>


</beans>


第三步  sitemesh3 的配置(/WEB-INF/sitemesh3.xml)

<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
<mapping path="*/redirect/*.html" decorator="/WEB-INF/common/decorator.jsp"></mapping>
<mapping path="*/productList/*.html" decorator="/WEB-INF/common/productListDecorator.jsp"></mapping>
</sitemesh>


第四步  mybatis在spring的配置 (applicationContext.xml)

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
default-autowire="byName">


<!-- 加载properties文件中的数据库配置信息 -->
<bean id="loadProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:property/jdbc.properties</value>
</list>
</property>


</bean>


<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxWait" value="${maxWait}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="maxActive" value="${maxActive}"></property>
</bean>


<!-- sqlsessionFacotory的配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/web/frontpage/pojo/*.xml"></property>


</bean>


<!-- mybatis模板的配置 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"></constructor-arg>
</bean>






<import resource="springBeans.xml" />
<import resource="springJMS.xml" />
</beans>




第五步 mbatis-config.xml的配置

里面主要配置所有po的别名

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.web.frontpage.pojo.Brand" alias="Brand" />
<typeAlias type="com.web.frontpage.pojo.Catalog" alias="Catalog" />
<typeAlias type="com.web.frontpage.pojo.ItemMapping" alias="ItemMapping" />
<typeAlias type="com.web.frontpage.pojo.ProductList" alias="ProductList" />
<typeAlias type="com.web.frontpage.pojo.ProductMeta" alias="ProductMeta" />
<typeAlias type="com.web.frontpage.pojo.Website" alias="Website" />


</typeAliases>
</configuration>


第六步  po的配置(pojo+mapper.xml)

(1) pojo

package com.web.frontpage.pojo;


import java.io.Serializable;


public class ItemMapping implements Serializable {


/**

*/
private static final long serialVersionUID = 1L;

private Integer id ;
private ProductMeta productMeta ;
private ProductList productList;
private Catalog catalog ;
private Brand brand ;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public ProductMeta getProductMeta() {
return productMeta;
}
public void setProductMeta(ProductMeta productMeta) {
this.productMeta = productMeta;
}
public ProductList getProductList() {
return productList;
}
public void setProductList(ProductList productList) {
this.productList = productList;
}
public Catalog getCatalog() {
return catalog;
}
public void setCatalog(Catalog catalog) {
this.catalog = catalog;
}
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
}


(2) itemMappingMapper.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper    
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.web.frontpage.pojo.ItemMapping">


<resultMap type="ProductMeta" id="metaMap">
<id column="id" property="id" />
<result column="product_name" property="productName" />
<result column="product_url" property="productUrl" />
<result column="product_image" property="productImage" />
</resultMap>


<resultMap type="ProductList" id="pListMap">
<id column="id" property="id" />
<result column="product_name" property="productName" />
<result column="product_url" property="productUrl" />
<result column="product_price" property="productPrice" />
<result column="product_image" property="productImage" />
</resultMap>


<resultMap type="ItemMapping" id="itemMapping">
<id column="id" property="id" />
<association property="productMeta" column="meta_id"
javaType="ProductMeta" select="productMetaSelect"></association>
<association property="productList" column="p_id"
javaType="ProductList" select="productListSelect"></association>
</resultMap>


<select id="productMetaSelect" parameterType="int" resultMap="metaMap">
select * from match_productMeta where id = #{meta_id}
</select>


<select id="productListSelect" parameterType="int" resultMap="pListMap">
select * from match_productList where id = #{p_id}
</select>


<select id="catalogSelect" parameterType="int" resultType="Catalog">
select * from match_catalog where id = #{catalog_id}
</select>


<select id="brandSelect" parameterType="int" resultType="Brand">
select *
from match_brand where id = #{brand_id}
</select>








<select id="catalogIdSelect" resultMap="itemMapping"
parameterType="int">
select top 4 * from match_itemMapping where catalog_id = #{id}
</select>




<select id="findByProperty" parameterType="Map" resultMap="itemMapping">
select * from match_itemMapping
</select>

</mapper>


第七步  controll中对json的支持实例

package com.web.frontpage.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.web.frontpage.IService.IService;
import com.web.frontpage.bean.CatalogCountBean;
import com.web.frontpage.bean.Pager;
import com.web.frontpage.pojo.ItemMapping;


@Controller
public class ProductListController extends BaseController {


@Autowired
private IService<?> productListService;


@SuppressWarnings("unchecked")
@ResponseBody
@RequestMapping(value = "/search/fuzzy/pkword.html", method = RequestMethod.GET)
public List<CatalogCountBean> fuzzySearch(@RequestParam String q) {
List<CatalogCountBean> list = null;
System.out.println("q:" + q);
list = (List<CatalogCountBean>) productListService.fuzzySearch(
productListNamespace, q);
return list;
}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值