Spring框架-7(搭建SSM)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Utzw0p0985/article/details/79168355
0?wx_fmt=png

本文作者

0?wx_fmt=png

25

1-2018

zho007

博客:

https://www.jianshu.com/u/e9cb5fd3aaa8

声明:本文由作者 zho007 授权发布,未经原作者允许请勿转载

 看到上面蓝色字了吗,点下吧

系列文章:


  1. Spring框架-1(基础)

  2. Spring框架-2(IOC上)

  3. Spring框架-3(IOC下)

  4. Spring框架-4(AOP)

  5. Spring框架-5(JDBC模板&Spring事务管理)

  6. Spring框架-6(SpringMvc)

  7. Spring框架-7(搭建SSM)

  8. Spring框架-8(SpringMVC2)


日更一篇~

0?wx_fmt=png

前言

前面学习了spring和SpringMvc,掌握了一些基础知识,那么现在我们来整合一下ssm。使用一个简单的小项目来使用一下。下面是整合ssm的一些基础:
0?wx_fmt=png

先附上Demo地址:https://www.jianshu.com/p/1eda98e24ffc

搭建SSM

写好后的目录结构:
0?wx_fmt=png

这里的所有的配置文件我单独创建了一个config。所有的配置文件doub放在这里,当然我在web.xml中这样是无法访问的。我们需要通过idea吧这个目录设置为resource目录。点击config目录右键,如图:

0?wx_fmt=png

这样在web.xml中就能访问到我们的配置文件了

创建项目导入jar包

使用IDEA创建一个Web项目。
0?wx_fmt=png

导入jar包

0?wx_fmt=png

0?wx_fmt=png

Dao层配置文件

数据库文件,使用mybatis逆向工程生成mapper

数据库表结构

0?wx_fmt=png

逆向工程生成所用的东西

0?wx_fmt=png

创建SqlMapConfig.xml

mybatis核心配置文件

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

创建ApplicationContext-dao.xml

<?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:SqlMapConfig.xml"/>
   </bean>
   <!-- 配置Mapper扫描器 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.zhong.dao"/>
   </bean>
</beans>
通配符配置的db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.108.184.253:3306/zTest
jdbc.username=root
jdbc.password=123456

Service层配置文件

ApplicationContext-trans.xml  事务

<?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.zhong.service.*.*(..))"/>

   </aop:config>
</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.springframework.org/schema/util/spring-util-4.0.xsd"
>

   <!-- @Service扫描 -->
   <context:component-scan base-package="com.zhong.service"></context:component-scan>
</beans>

controller层配置文件

SpringMvc.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-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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.zhong.controller"></context:component-scan>
   <!-- 注解驱动:
           替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->

   <mvc:annotation-driven ></mvc:annotation-driven>
   <!-- 配置视图解析器
   作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
   -->

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- 真正的页面路径 =  前缀 + 去掉后缀名的页面名称 + 后缀 -->
       <!-- 前缀 -->
       <property name="prefix" value="/WEB-INF/jsp/"></property>
       <!-- 后缀 -->
       <property name="suffix" value=".jsp"></property>
   </bean>
</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">

   <!-- 加载spring容器 -->
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:ApplicationContext-*.xml</param-value>
   </context-param>
   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <!-- springmvc前端控制器 -->
   <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:SpringMvc.xml</param-value>
       </init-param>
       <!-- 在tomcat启动的时候就加载这个servlet -->
       <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>springMvc</servlet-name>
       <url-pattern>*.action</url-pattern>
   </servlet-mapping>
   <!-- 配置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>
</web-app>

流程

  1. tomcat启动时首先加载web.xml文件

  2. web.xml中加载spring的所有的ApplicationContext开头的配置文件

  3. 在所有的ApplicationContext中再加载所有的db.properties,SqlMapConfig.xml文件

  4. web.xml中加载SpringMvc.xml

加载完成,我们的ssm框架就能跑起来了。

写一个测试

需求

请求http://localhost:8080/list.action查询数据库数据,返回jsp页面显示出来

1.编写jsp

商品列表:
<table width="100%" border=1>
<tr>
   <td>商品名称</td>
   <td>商品价格</td>
   <td>生产日期</td>
   <td>商品描述</td>
   <td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
   <td>${item.name }</td>
   <td>${item.price }</td>
   <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
   <td>${item.detail }</td>
   <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>

2.编写service

使用mybatis查询数据库

@Service
public class ItemsServiceImpl implements ItemsService {
   @Autowired
   private ItemsMapper itemsMapper;
   @Override
   public List<Items> list() throws Exception {
       //如果不需要任何查询条件,直接将example对象new出来即可
       ItemsExample example = new ItemsExample();
       List<Items> list = itemsMapper.selectByExampleWithBLOBs(example);
       return list;
   }
}

3.编写controller

springMvc中默认支持的参数类型:也就是说在controller方法中可以加入这些参数如下的Model,也可以不加,  加不加看自己需不需要,都行.HttpServletRequest,HttpServletResponse,HttpSession,Model

@Controller
public class ItemsController {
   @Autowired
   private ItemsService itmesService;
   @RequestMapping("/list")
   public String itemsList(Model model) throws Exception {
       List<Items> list = itmesService.list();
       //Model模型:模型中放入了返回给页面的数据
       //model底层其实就是用的request域来传递数据,但是对request域进行了扩展.
       model.addAttribute("itemList", list);
       //如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
       return "itemList";
   }
}

4.测试

配置tomcat访问http://localhost:8080/list.action,注意在前面配置了.action所以必须在后面加上.action。

结果:成功

0?wx_fmt=png

0?wx_fmt=jpeg

刘某人程序员

个人微信:Android_LiuGuiLin

新浪微博:@刘某人程序员

0?wx_fmt=jpeg

看看我的分享

  长按二维码关注

0?wx_fmt=png 0?wx_fmt=png


猜你喜欢

转载自blog.csdn.net/Utzw0p0985/article/details/79168355