ssm 和 ssh 整合(核心思路)

ssm

需要的配置文件:

web.xml
Springmvc框架   src/springmvc.xml
Mybatis框架   src/SqlMapConfig.xml  在dao层有跟接口XxxMapper.java对应的 XxxMapper.xml(sql语句)
Spring框架   src/applicationContext.xml
关于日志   log4j.properties
关于数据库连接   db.properties


Dao层:

  1. SqlMapConfig.xml,空文件即可(不需要配置别名)。需要文件头。
  2. applicationContext-dao.xml。
    1. 数据库连接池
    2. SqlSessionFactory对象,需要spring和mybatis整合包下的。
    3. 配置mapper文件扫描器。

Service层:

  1. applicationContext-service.xml包扫描器,扫描@service注解的类。
  2. applicationContext-trans.xml配置事务。

表现层:

Springmvc.xml

  1. 包扫描器,扫描@Controller注解的类。
  2. 配置注解驱动。
  3. 视图解析器

Web.xml

配置前端控制器,并使用监听器加载Spring配置文件。

 

事例:

sqlMapConfig.xml

在classpath下创建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>

applicationContext-dao.xml

配置数据源、配置SqlSessionFactory、mapper扫描器。

<?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="cn.itcast.springmvc.mapper"/>

     </bean>

 

</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

applicationContext-service.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:component-scan base-package="cn.itcast.springmvc.service"/>

 

</beans>

applicationContext-transaction.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(* cn.itcast.springmvc.service.*.*(..))" />

     </aop:config>

</beans>

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: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="cn.itcast.springmvc.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

<?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_2_5.xsd"

     id="WebApp_ID" version="2.5">

     <display-name>springmvc-web</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容器 -->

     <!-- spring配置文件-->

     <context-param>

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

           <param-value>classpath:spring/applicationContext-*.xml</param-value>

     </context-param>

    <!-- 监听器加载spring配置文件 -->

     <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:spring/springmvc.xml</param-value>

           </init-param>

     </servlet>

     <servlet-mapping>

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

           <!-- 配置所有以action结尾的请求进入SpringMVC -->

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

     </servlet-mapping>

</web-app>

 

ssh

xml配置文件方式整合,需要的配置文件:

web.xml
Struts2框架   src/struts.xml
Hibernate框架   src/hibernate.cfg.xml  在domain有 Xxx.hbm.xml
Spring框架   src/applicationContext.xml
关于日志   log4j.properties
关于数据库连接   db.properties

进化,annotation方式整合:(推荐)

web.xml
Struts2框架   src/struts.xml,空文件即可。
Hibernate框架   src/hibernate.cfg.xml  在domain有 Xxx.hbm.xml ,不需要这两个文件了。前者是配置连接数据库之类的信息,已经交给spring去管理,在applicationContext.xml中配置;后者是描述domain实体类和数据库表对应关系,这个直接在domain实体类中运用注解配置。
Spring框架   src/applicationContext.xml
关于日志   log4j.properties
关于数据库连接   db.properties

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"
	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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 加载properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 开启注解扫描  @Respostory  @Service  @Controller-->
	<context:component-scan base-package="cn.itheima"/>

	<!-- 配置连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 声明sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 加载连接池 ,hibernate.cfg.xml部分-->
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<value>
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.format_sql=true
			</value>
		</property>
		<!-- 加载注解类 -->
		<property name="packagesToScan">
			<list>
				<value>cn.itheima.domain</value>
			</list>
		</property>
	</bean>
	
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 事务注解驱动 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

web.xml

<?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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ssh-annotation</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:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- openSessionInViewFilter -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- struts2 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>


</struts>

db.properties,参照上面ssm。

domain实体类(User.java)

package cn.itheima.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_user",catalog="sshtest")
public class User {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	//@Column可以省略
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}

可以看出,ssh 的xml方式整合跟 ssm需要的配置文件每层都是很对应相似的。方便记忆,在相似的基础上,ssh注解形式有两个文件不需要。

猜你喜欢

转载自blog.csdn.net/hello_world123456789/article/details/90696418