SSM入门(案例)
2018-03-02 18:12
489 查看
一概述
SSMspring web mvc(表现层) + spring + mybatis(持久层)
springmvc是spring的一部分,不需要整合包
二实例
目标:将商品信息显示到页面
1.创建一个web工程
2.导入相关jar包
3.创建配置文件
mybatis配置sqlMapConfig.xml(空配置,需要一个根标签)
<?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> </configuration>
· 映射文件逆向工程生成
spring配置
-applicationContext-dao.xml dao层配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- mapper配置 --> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean> <!-- 配置Mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.blueSky.mapper" /> </bean> </beans>
-applicationContext-service.xml service层配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.s 4000 pringframework.org/schema/util/spring-util-4.0.xsd"> <context:component-scan base-package="com.blueSky.service"/> </beans>
-applicationContext-transacation.xml 事务配置(service层方法加事务)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <!-- 若无事务,开启事务 --> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <!-- 当前事务,不开启事务 --> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.blueSky.service.*.*(..))" /> </aop:config> </beans>
springmvc springmvc配置
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描带Controller注解的类 --> <context:component-scan base-package="com.blueSky.controller" /> <!-- 加载注解驱动 --> <mvc:annotation-driven /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- jsp前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- jsp后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
web.xml配置
-配置springmvc前端控制器(DispatchServlet)
-加载spring容器配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSM</display-name> <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> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
4.编写代码
查询从后向前,先想SQL语句。- dao层(mapper目录)
mybatis逆向工程生成代码,mapper接口和mapper映射文件
- service层
创建接口,实现类调用dao层方法
package com.blueSky.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.blueSky.mapper.ItemsMapper; import com.blueSky.pojo.Items; public interface ItemsService { public List<Items> getItemsList(); }
bd62
package com.blueSky.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.blueSky.mapper.ItemsMapper; import com.blueSky.pojo.Items; import com.blueSky.pojo.ItemsExample; import com.blueSky.service.ItemsService; @Service public class ItemServiceImpl implements ItemsService { @Autowired private ItemsMapper itemsMapper; @Override public List<Items> getItemsList() { ItemsExample itemsExample = new ItemsExample(); List<Items> list = itemsMapper.selectByExampleWithBLOBs(itemsExample); return list; } }
view层
调用service层方法获得参数
package com.blueSky.controller; import java.util.List; 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.blueSky.pojo.Items; import com.blueSky.service.ItemsService; @Controller public class ItemsController { @Autowired private ItemsService itemsService; /*1.编写handler方法(配置注解resultmapping) *2.调用service层方法获取数据 *3.创建modelandview对象 *4.将数据存入request域,modelandview.addObject,设置对象名 *5.设置视图解析器路径 *6.返回ModelAndView */ @RequestMapping("itemList.action") public ModelAndView getItemsList(){ List<Items> itemsList = itemsService.getItemsList(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("itemList", itemsList); modelAndView.setViewName("itemList"); return modelAndView; } }
相关文章推荐
- 一个案例带你入门SSM框架开发
- (SSM框架)SpringMVC+Spring4+Mybatis+Maven环境快速搭建开发案例
- JS入门小案例
- 19、SSM框架-Mybatis入门概述及第一个Mybatis实例实现增删改查(1)
- python用字符串操作20行代码简单爬虫入门+案例(爬取一章《三体》小说)
- Spring MVC入门(3)---简单案例(2)
- struts入门案例
- SSM框架(Spring Spring MVC Mybatis)基础入门学习3——Mybits入门实例(实例免费下载!)
- activeMq的入门小案例
- Spring(1)初识Spring+IOC(控制反转)+DI(依赖注入)(附:快速入门案例)
- 墙裂推荐一本案例驱动的PhoneGap入门书,早看早收货
- 使用Ajax技术的一个入门案例
- 06_android入门_采用HttpURLConnection的POST方式实现登陆案例
- Struts2入门案例,自动装配
- Android NDK学习笔记3-入门案例篇
- 嵌入式OS入门笔记-以RTX为案例:八.RTX的进程间通讯(二)
- 【SSM-SpringMVC框架】SpringMVC入门
- Mybatis入门案例
- 关于idea maven工程创建struts2入门配置及案例
- 回溯算法入门及经典案例剖析(初学者必备宝典)