springmvc教程系列

springmvc史上最好教程(2)

springmvc史上最好教程(1)

springmvc史上最好教程(3)

springmvc史上最好教程(4)


一、整合mybatis

为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合。

整合目标:控制层采用springmvc、持久层使用mybatis实现。

1.1需求

实现商品查询列表,从MySQL数据库查询商品信息。

1.2jar包

包括:spring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、第三方连接池。

1.3Dao

目标:

1、spring管理SqlSessionFactory、mapper

1.3.1sqlMapConfig.xml

在classpath下创建mybatis/sqlMapConfig.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEconfiguration

PUBLIC"-//mybatis.org//DTDConfig3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<!—使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers-->

<mappers>

<packagename="com.sihai.ssm.mapper"/>

</mappers>

</configuration>



1.3.2applicationContext-dao.xml

配置数据源、事务管理,配置SqlSessionFactory、mapper扫描器。

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-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/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!--加载配置文件-->

<context:property-placeholderlocation="classpath:db.properties"/>

<!--数据库连接池-->

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

<propertyname="driverClassName"value="${jdbc.driver}"/>

<propertyname="url"value="${jdbc.url}"/>

<propertyname="username"value="${jdbc.username}"/>

<propertyname="password"value="${jdbc.password}"/>

<propertyname="maxActive"value="30"/>

<propertyname="maxIdle"value="5"/>

</bean>

<!--让spring管理sqlsessionfactory使用mybatis和spring整合包中的-->

<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<!--数据库连接池-->

<propertyname="dataSource"ref="dataSource"/>

<!--加载mybatis的全局配置文件-->

<propertyname="configLocation"value="classpath:mybatis/SqlMapConfig.xml"/>

</bean>

<!--mapper扫描器-->

<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

<propertyname="basePackage"value="com.sihai.springmvc.mapper"></property>

<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>

</bean>

</beans>



1.3.3ItemsMapper.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEmapper

PUBLIC"-//mybatis.org//DTDMapper3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mappernamespace="com.sihai.ssm.mapper.ItemsMapper">

<!--sql片段-->

<!--商品查询条件-->

<sqlid="query_items_where">

<iftest="items!=null">

<iftest="items.name!=nullanditems.name!=''">

anditems.namelike'%${items.name}%'

</if>

</if>

</sql>

<!--查询商品信息-->

<selectid="findItemsList"parameterType="queryVo"resultType="items">

select*fromitems

<where>

<includerefid="query_items_where"/>

</where>

</select>

</mapper>



1.3.4ItemsMapper.java

publicinterfaceItemsMapper{

//商品列表

publicList<Items>findItemsList(QueryVoqueryVo)throwsException;

}



1.4Service

目标:

1、Service由spring管理

2、spring对Service进行事务控制。

1.4.1applicationContext-service.xml

配置service接口。

1.4.2applicationContext-transaction.xml

配置事务管理器。

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-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/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!--事务管理器-->

<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!--数据源-->

<propertyname="dataSource"ref="dataSource"/>

</bean>

<!--通知-->

<tx:adviceid="txAdvice"transaction-manager="transactionManager">

<tx:attributes>

<!--传播行为-->

<tx:methodname="save*"propagation="REQUIRED"/>

<tx:methodname="insert*"propagation="REQUIRED"/>

<tx:methodname="delete*"propagation="REQUIRED"/>

<tx:methodname="update*"propagation="REQUIRED"/>

<tx:methodname="find*"propagation="SUPPORTS"read-only="true"/>

<tx:methodname="get*"propagation="SUPPORTS"read-only="true"/>

</tx:attributes>

</tx:advice>

<!--切面-->

<aop:config>

<aop:advisoradvice-ref="txAdvice"

pointcut="execution(*com.sihai.springmvc.service.impl.*.*(..))"/>

</aop:config>

</beans>



1.4.3OrderService


publicinterfaceOrderService{

//商品查询列表

publicList<Items>findItemsList(QueryVoqueryVo)throwsException;

}

@Autowired

privateItemsMapperitemsMapper;

@Override

publicList<Items>findItemsList(QueryVoqueryVo)throwsException{

//查询商品信息

returnitemsMapper.findItemsList(queryVo);

}

}



1.5Action

1.5.1springmvc.xml

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-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/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!--扫描controller注解,多个包中间使用半角逗号分隔-->

<context:component-scanbase-package="com.sihai.ssm.controller"/>

<!--注解映射器-->

<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!--注解适配器-->

<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!--ViewResolver-->

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<propertyname="viewClass"

value="org.springframework.web.servlet.view.JstlView"/>

<propertyname="prefix"value="/WEB-INF/jsp/"/>

<propertyname="suffix"value=".jsp"/>

</bean>

</beans>



1.5.2web.xml

加载spring容器,配置springmvc前置控制器。

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID"version="2.5">

<display-name>springmvc</display-name>

<!--加载spring容器-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--解决post乱码-->

<filter>

<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!--springmvc的前端控制器-->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--contextConfigLocation不是必须的,如果不配置contextConfigLocation,springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml"-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>

1.5.3OrderController


@Controller

publicclassOrderController{

@Autowired

privateOrderServiceorderService;

@RequestMapping("/queryItem.action")

publicModelAndViewqueryItem()throwsException{

//商品列表

List<Items>itemsList=orderService.findItemsList(null);

//创建modelAndView准备填充数据、设置视图

ModelAndViewmodelAndView=newModelAndView();

//填充数据

modelAndView.addObject("itemsList",itemsList);

//视图

modelAndView.setViewName("order/itemsList");

returnmodelAndView;

}

}

1.6测试

http://localhost:8080/springmvc_mybatis/queryItem.action