SSM框架整合图解,配置清楚


我用的是SQL Server,项目搭建的时候引入的数据库包是不一样的。

1.创建项目,添加JAR包

  1. 创建实体
  2. 创建dao及daomapper配置文件
  3. 创建service和serviceimpl
  4. 创建controller


    


  1. 创建页面 jsp需要引入 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    
        7.配置web.xml

        配置springmvc总调度器DispatcherServlet contextConfigLocation
     <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <!-- 配置spring监听器  开始 -->
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
   <!-- 配置spring监听器  结束 -->
   <!-- spring 中文乱码过滤器 开始 -->
  <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>
  <!-- spring 中文乱码过滤器 结束-->
  <!-- 配置springmvc总控制器  开始 -->
   <servlet> 
      <servlet-name>springMvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 如果没有init-param这个配置,默认情况下必须有一个与这个servlet同名的(helloMvc), 并且以helloMvc-servlet.xml结尾的配置文件
      并且命名空间必须是springbean配置一样的 -->
      <!-- 如果想自定义文件路径 ,需要加入init-param-->
      <init-param>
           <!-- contextConfigLocation是固定,不能更改 -->
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring-mvc.xml</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>springMvc</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
  <!-- 配置springmvc总控制器  结束-->
  <!-- 静态资源免过滤  开始-->
  <servlet-mapping>
     <servlet-name>default</servlet-name>
     <url-pattern>*.jpg</url-pattern>
     <url-pattern>*.png</url-pattern>
  </servlet-mapping>
  <!-- 静态资源免过滤  结束-->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
8.配置数据库连接 dbcp.properties
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc\:sqlserver\://192.168.1.111\:1433;databaseName\=abc
jdbc.username=aaa
jdbc.password=bbb
initialSize=30
maxActive=30
maxIdle=10
minIdle=5
maxWait=1000

        9.配置 applicationContext.xml   整合mybatis
         <?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 开启包扫描,指定扫描包的路径,如果想分包扫描,可以用,隔开,分别进行扫描,*通配符,也可以使用 -->
    <context:component-scan base-package="com.ssm.service"></context:component-scan>
    <!-- 加载配置文件 -->
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location" value="classpath:dbcp.properties"></property>
   </bean>
   <!-- 配置数据源(使用dbcp连接池技术) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <!-- 初始化连接大小 -->
   <property name="initialSize" value="${initialSize}"></property>
   <!-- 连接池最大数量 -->
   <property name="maxActive" value="${maxActive}"></property>
   <!-- 连接池最大空闲 -->
   <property name="maxIdle" value="${maxIdle}"></property>
   <!-- 连接池最小空闲 -->
   <property name="minIdle" value="${minIdle}"></property>
   <!-- 获取连接最大等待时间 -->
   <property name="maxWait" value="${maxWait}"></property>
    </bean>
     <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 扫描mapper文件 -->
        <property name="mapperLocations" value="classpath:com/ssm/dao/mapper/*.xml"></property>
    </bean>
     <!-- 开启dao接口扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描接口包路径,不需要加classpath,如果多个可以用“,”隔开 -->
        <property name="basePackage" value="com.ssm.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
     <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 加载数据源 -->
      <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启事务扫描 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
10.配置  spring-mvc.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: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.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.0.xsd">  
        <!-- 开启包扫描 -->
         <context:component-scan base-package="com.ssm.controller"></context:component-scan>
         <!-- 开启springmvc的注解驱动 -->
         <mvc:annotation-driven/>
  <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置前缀(也就是文件夹名称)  prefix suffix是固定值,不能改变   value值,会默认取WebRoot作为根目录-->
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
        11.配置 mapper映射 foodMapper.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.ssm.dao.FoodDao">
<select id="getList" resultType="com.ssm.entity.FoodType">
select type_id,type_name from tb_Foodtype
</select>
<insert id="addFood" parameterType="com.ssm.entity.FoodType" >
insert into tb_FoodType(type_name) values(#{type_name})
</insert>
</mapper>

扫描加载配置文件PropertyPlaceholderConfigurer location
配置数据源(使用dbcp连接池,连接池技术参看连接池技术word文档) BasicDataSource
  配置SqlSessionFactory SqlSessionFactoryBean dataSource mapperLocations (用到数据和配置文件,结合mybatis主配置和Mapper文件讲解)
开启Dao接口扫描MapperScannerConfigurer basePackage sqlSessionFactoryBeanName(用的value不是ref)
配置事务DataSourceTransactionManager dataSource
开启事务扫描
    12. 加载项目,运行测试

如果想下载项目代码,请进入https://download.csdn.net/download/qq_40393093/10279297


----------------------------------------------------------------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值