SSM整合开发完整步骤

SSM编程,即SpringMVC + Spring + MyBatis整合,是当前最为流行的JavaEE开发技术架构。其实SSM整合的实质,仅仅就是将MyBatis整合入Spring。因为SpringMVC原本就是Spring的一部分,不用专门整合。

SSM整合的实现方式可分为两种:基于XML配置方式,基于注解方式。在如今的实际项目中,大多数是xml和注解方式结合使用。

一、步骤


1.新建Dynamic Web Project项目
2.新建mysql的数据库 springdb, 表student ,有列
        id int 自动增长的。
        name varchar 50 姓名
        age int 年龄
3.导入jar包
  1.spring的核心jar包:spring-beans.jar,spring-core.jar,spring-context.jar,spring-expression.jar
  2.spring-aop.jar
  3.数据库相关的jar包:spring-jdbc.jar,spring-tx.jar
  4.web相关的jar包:spring-web.jar,spring-webmvc.jar
  5.mybatis的核心jar包:mybatis-3.4.5.jar
  6.mybatis和spring的整合jar包: mybatis-spring-1.3.1.jar
  7.Jackson的jar包: 三个(用来解析json格式的对象)
  8.其他jar包: mysql的驱动, druid的连接池, log4j.jar, commons-logging.jar
 
4.配置web.xml文件
  1)注册spring的监听器, 创建spring的容器对象, 加载spring的配置文件(创建Service对象和Dao对象)
  2)注册springmvc的中央调度器, 创建springmvc的容器对象, 加载springmvc的配置文件(创建Controller对象)
  3)注册字符集过滤器,解决post请求乱码的问题

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>25-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>
  	<!-- 自定义 spring的配置文件-->
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:conf/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <!-- spring-web.jar -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 注册springmvc的中央调度器 -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<!-- spring-webmvc.jar -->
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:conf/dispatcherServlet.xml</param-value>
  	</init-param>
  	<!-- 指定中央调度器在什么时候创建,数字越小,创建时间越早 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- 注册字符集过滤器 -->
  <filter>
  	<filter-name>characterEncodingFilter</filter-name>
  	<!-- spring-web.jar -->
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>utf-8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceRequestEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


5.定义程序中的包结构: 实体类包  , Dao包名, Service包, Controller包
6.编写配置文件
  1)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: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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	 <!-- springmvc的配置文件 -->
	 <!-- 声明组件扫描器 -->
	 <context:component-scan base-package="com.wkcto.controllers" />
	 
	 <!-- 声明视图解析器 -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="prefix" value="/WEB-INF/jsp/" />
	 	<property name="suffix" value=".jsp" />
	 </bean>

     <!-- 声明注解驱动 -->
     <mvc:annotation-driven />
</beans>


  2)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:context="http://www.springframework.org/schema/context"
	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.xsd">

	  <!-- spring的配置文件 -->
	  <!-- 声明组件扫描器 -->
	  <context:component-scan base-package="com.wkcto.service" />
	  
	  <!-- 加载属性配置文件 -->
	  <context:property-placeholder location="classpath:conf/jdbc.properties"/>
	  
	  <!-- 声明数据源DataSource -->
	  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
	                           init-method="init" destroy-method="close">
	      <property name="url" value="${jdbc.url}" />
	      <property name="username" value="${jdbc.username}" />
	      <property name="password" value="${jdbc.passwd}" />
	      
	  </bean>
	  
	  <!-- 声明SqlSessionFactoryBean -->
	  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	  	<property name="dataSource" ref="dataSource" />
	  	<property name="configLocation" value="classpath:conf/mybatis.xml" />
	  </bean>
	  
	  <!-- 声明MyBatis的扫描器 -->
	  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	  	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	  	<property name="basePackage" value="com.wkcto.dao" />
	  </bean>
	  

</beans>


  3)数据库的属性配置文件(一般为:jdbc.properties)

jdbc.url=jdbc:mysql://localhost:3306/springdb
jdbc.username=root
jdbc.passwd=root


  4)mybatis的主配置文件(一般为:myBatis.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>
	<!-- 别名 -->
	<typeAliases>
		<package name="com.wkcto.beans"/>
	</typeAliases>
	<!-- 指定sql映射文件 -->
	<mappers>
		<package name="com.wkcto.dao"/>
	</mappers>
</configuration>


7.定义实体类, Dao接口和Sql映射文件, Service接口和实现类, Controller类

在Dao层中还需要配一个mapper.xml文件,用来访问数据库。名字和Dao层中的类名相同。例如:Dao层有一个类叫做StudentDao.java,那么mapper.xml文件的名字就叫做StudentDao.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.wkcto.dao.StudentDao">
	<!-- insert、select标签对中的内容就是对数据库的操作-->
	<insert id="insertStudent">
		insert into student(name,age) values(#{name},#{age})
	</insert>
	
	<select id="selectStudents" resultType="Student">
		select id,name,age from student order by id desc
	</select>
</mapper>


8.定义视图文件(jsp)

 

猜你喜欢

转载自blog.csdn.net/qq_35355937/article/details/81094559
今日推荐