SSH、SSM整合一览

一、Hibernate.cfg.xml配置文件:
作用:
1、配置数据源(hibernate提供的实现方法,与spring整合,会用到第三方数据源,比如C3P0)
2、配置hibernate参数

3、配置关系映射文件

<hibernate-configuration>
	<session-factory>
		<!-- 阿里正式库  -->
		<property name="connection.url">jdbc:mysql://127.0.0.1:6666/xxx?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="myeclipse.connection.profile">dooliu</property>
		<property name="connection.username">xxx</property>
		<property name="connection.password">xxxxx</property>
		<!-- 阿里测试库  -->

		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.provider_class">
			org.hibernate.connection.C3P0ConnectionProvider
		</property>
		<property name="hibernate.c3p0.min_size">5</property>
		<property name="hibernate.c3p0.max_size">300</property>
		<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
		<property name="hibernate.c3p0.time_out">1800</property>
		<!-- 最大的PreparedStatement的数量 (先设置为0不缓存测试一下)-->
		<property name="hibernate.c3p0.max_statement">500</property>
		<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
		<property name="hibernate.c3p0.idle_test_period">120</property>
		<!-- 每次都验证连接是否可用 -->
		<property name="hibernate.c3p0.validate">true</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<property name="connection.autocommit">false</property>
		<property name="jdbc.batch_size">30</property>

		<!--映射文件-->
		<mapping resource="xx/xx/xx/xx/xxx.hbm.xml" />
	</session-factory>
</hibernate-configuration>
Hibernate和spring整合:
1、引入jar包spring-web-4.2.4.RELEASE.jar  (spring整合web项目)
2、spring-orm-4.2.4.RELEASE.jar (spring整合hibernate)
3、数据源交给spring配置(c3p0)
4、hibernate的sessionFactory交给spring配置(创建sessionFactory,因此第一次访问很慢,所以交给spring容器初始化创建)
5、在dao里面使用hibernateTemplate模板(注入sessionfactory)
6、配置事务


☞第4步、具体步骤;
hibernate.cfg.xml中数据库的配置:

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///databaseName</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
+++++++++++++
Configuration cfg = new Configuration();
cfg.configure();
//创建sessionFactory
sessionFactory = cfg.buildSessionFactory();
=============
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	<!--引用数据源配置,这里就不写了,有多种技术可以用,比如c3p0,阿里的xxx,忘了-->
	<property name="dataSource" ref="dataSource"></property>
	<!--指定hibernate核心配置文件的位置-->
	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--类org.springframework.orm.hibernate5.LocalSessionFactoryBean就是spring-orm-4.2.4.RELEASE.jar里面的
spring,hibernate整合要用的包,spring封装了hibernate的配置。-->

☞第5步、具体步骤;

<!--这是jdbc模板的配置,hibernate不用-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>

<!--创建hibernateTemplate对象-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
	<注入sessionFactory-->
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
☞第6步、具体步骤;
<!--第一步,配置事务管理器-->
<bean id ="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	<注入sessionFactory-->
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!--第二步,开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>

注意:可以看到,spring和hibernate整合时,数据源,sessionFactory都由spring来创建了,hibernate.cfg.xml就还有
配置hibernate参数和配置关系映射文件的功能,把这些功能都能移到spring中,这样,就可以不写hibernate.cfg.xml配置文件了

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	<!--引用数据源配置,这里就不写了,有多种技术可以用,比如c3p0,阿里的xxx,忘了-->
	<property name="dataSource" ref="dataSource"></property>
	<!--指定hibernate核心配置文件的位置(去掉hibernate.cfg.xml配置,将里面的内容全部在spring配置文件文件中配置)-->
	<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.format_sql">true</prop>
			<prop key="hibernate.hbm2ddl.auto">update</prop>
		</props>
	</property>
	<!--映射文件也引入进来-->
	<property name="mappingResources">
		<list>
			<value>xx/xx/xx/xx/User.hbm.xml</value>
		</list>
	</property>
</bean>

HibernateTemplate常用api:
Serializable save(Object entity);
void update(Object entity);
void delete(Object entity);
<T> T get(class<T> entityClass,Serializable id);
<T> T load(class<T> entityClass,Serializable id);
List find(String queryString,Object...values);



二、struts.xml配置文件:
1、配置一些常量
2、配置Action(拦截器,跳转)

<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<package name="delloDemo" extends="struts-default" namespace="/">
		<interceptors>
			<interceptor name="exceInterceptor"
				class="com.douliu.star.common.interceptors.ExceptionInterceptor"></interceptor>
			<interceptor-stack name="myStacks">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="exceInterceptor"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<!-- 定义默认拦截器 -->
		<default-interceptor-ref name="myStacks"></default-interceptor-ref>

		<!-- 定义全局变量 -->
		<global-results>
			<result name="index" type="redirect">/index.jsp</result>
			<result name="error">/views/share/error.jsp</result>
			<!-- 分享页面 找不到作品 -->
			<result name="nfound">/views/share/error.jsp</result>
		</global-results>

		<action name="hello" class="com.dhh.action.HelloAction" method="属性占位">
			<result name="ok" type="属性占位">/hello.jsp</result>
		</action>
		<action name="hello_*" class="com.dhh.action.HelloAction"
			method="{1}">
			<result name="success" type="redirectAction">orders</result>
		</action>
	</package>
</struts> 
struts2和spring整合:
1、引入struts2-spring-plugin-2.3.24.jar 整合jar包
2、struts2的action在spring中配置(多实例)

<bean id="" class="" scope="prototype"></bean>
3、既然action在spring中创建了,那么struts.xml要相应变化:
<!--class属性里面不写action的全路径,因为写,会创建两次,spring配置的action的bean也会创建一次,
	所以写class写spring的bean的id值,那么为什么这样写可以运行呢?因为我们要引入spring和struts整合
	的包struts2-spring-plugin-2.3.24.jar,这个包解析到了action,就不会直接拿class的值反射来创建
	action对象了,而是到context.getBean("bean id"); -->
<action name="userAction class="userAction"></action>

Struts2多模块开发:
<struts>
	<!--struts2多模块开发-->
	<include file=""></include>
</struts>

spring多模块开发:

<beans>
	<!--spring多模块开发,其他模块也都要有约束文件-->
	<import resource="classpath:config/user.xml"/>
</beans>



三、SqlMapConfig.xml

1、加载数据源

2、setting 配置,配置缓存,别名等

3、mapper映射文件

<configuration>
	<typeAliases>
		<!-- 查询 -->
		<typeAlias alias="page" type="com.wlx.commons.web.mybatis.Page" />
		<!-- 公用参数 -->
		<typeAlias alias="dataParam" type="com.wlx.vo.params.DataParam" />
	</typeAliases>
	
	<!-- 分页插件 -->
	<plugins>
 		<plugin interceptor="com.wlx.commons.web.mybatis.PagePlugin"></plugin>
	</plugins>
	 
	<mappers>
		<!-- 操作用户 -->
		<mapper resource="mybatis/wlx/system/OperatorMapper.xml" />
		<mapper resource="mybatis/wlx/common/DictionaryMapper.xml" />
		<mapper resource="mybatis/wlx/live/AnchorMapper.xml" />
		<mapper resource="mybatis/wlx/live/TimeMapper.xml" />
		<mapper resource="mybatis/wlx/live/BalanceMapper.xml" />
		<mapper resource="mybatis/wlx/stat/StatUserMapper.xml" />
	</mappers>
</configuration>
spring整合mybatis(mapper代理开发方式)

1、配置数据源(第三方:c3p0,dbcp,hikari,bonecp,druid)
2、spring通过单例方式管理SqlSessionFactory

3、spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession(spring和mybatis整合自动完成)
4、持久层的mapper都需要由spring管理(配置mapper扫描器)

整合包:
mybatis-spring-1.2.2.jar
早起ibatis和spring整合是由spring官方提供--> mybatis提供

具体实现:

1、加载数据源

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

<!--数据源,使用dbcp,据说性能没c3p0好-->
<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>
2、配置SqlSessionfactory:

<!--sqlSessionFactory配置-->
<!--实现类,肯定是spring-mybatis整合包提供,打开整合包,结构一目了然-->
<bean id ="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 配置mybatis核心配置文件配置-->
	<property name="configLocation" value="config/SqlMapConfig.xml"/>
	<!-- 配置数据源-->
	<property name="dataSource" ref ="dataSource"/>
	<!--还可以注入很多属性,看下面SqlSessionFactoryBean的接口,当把改用的属性值都注入完,就可以删除SqlMapConfig.xml文件了-->
</bean>

原始dao开发:
之前的做法:

public class UserDaoImpl implements UserDao{
	//通过构造方法注入
	private SqlSessionFactory sqlSessionFactory;

	public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}

	Pojo findUserById{
		SqlSession sqlSession = sqlSessionFactory.openSession();
		sqlSession.crud();
		sqlSession.close();
		return pojo;
	}
}
整合后:
public class UserDaoImopl extends SqlSessionDaoSupport implements UserDao{
	Pojo findUserById{
		SqlSession sqlSession = this.getSqlSession();
		sqlSession.crud();
		sqlSession.close();
		return pojo;
	}
}
applicationContext.xml配置:

<!--感觉配置太麻烦,在spring-mybatis包中应该有提供批量扫描的类,原始方法现在mybatis用的不多,就不研究了,用的基本都是mapper代理方法-->
<bean id="userDao" classs="xx.xx.xxx.UserDaoImpl">
	<property name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>

Mapper代理开发(配置基于mapper接口和mapper配置文件在同一个包中):
<!--mapper代理配置-->
<bean id="userMapper" clas="????"/></bean>
如果是mapper代理,class该配置啥值呢??

-----打开mybatis和spring的整合包,里面结构非常清楚,他提供了mapper代理工厂来解决class的配值!!

<!--spring,mybatis整合包提供-->
<bean id="userMapper" clas="org.mybatis.spring.mapper.MapperFactoryBean"/>
	<!--打开这个类,会发现里面要注入的参数-->
	<property name="mapperInterface" value="xx.xx.xxx.mapper.UserMapper"/>
	<property name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>
这样配置发现,每个mapper要一个配置,太麻烦!!!

3、持久层的mapper都需要由spring管理(实现方法)

<!--mapper批量扫描,从mapper包中扫描出mappr接口,自动创建对象注入sqlSessionFactory并且在spring中注册,spring-mybatis整合包提供
	规矩:mapper.java和mapper.xml要在同一个包中-->
<!--如果配置了自动扫描类,把mapper代理对象都在spring中创建了,那id叫什么名字呢??怎么找到mapper代理类??
	答:自动扫描出来的mapper的id为mapper类名(首字母小写)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!--打开会发现要注入的参数-->
	<!--配置扫描的包名,多个包逗号相隔-->
	<property name="basePackage" value="xx.xx.xx.mapper"/>
	<!--这里有一个坑,打开这个类,发现有一个参数叫sqlSessionFactory,所以千万不能按照注释的配置去注入sqlSessionFactory-->
	<!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>  -->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

配置了mapper扫描包后:
sqlMapConfig.xml中的加载映射文件配置就不需要了

<mappers>
	<!--指定mapper接口的包名,mybatis自动扫描包下所有mapper接口进行加载
		规矩:mapper.java和mapper.xml要在同一个包中-->
	<package name="xx.xxx.xx.xx.mapper"/>
</mappers>

另外,如果我们想把所有的sqlMapConfig.xml配置都移过来,然后删除SqlMapConfig,那要配置如下:

走到这一步,SqlMapConfig.xml中仅仅还有<setting>配置--别名alias,类型转换器typeHandle

这些都可以配置到SqlSessionFactoryBean中:

一个简单案例:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="configLocation" value="classpath:mybatis.xml" />
	<property name="dataSource" ref="dataSource" />
	<property name="typeAliasesPackage" value="com.wlx.domain" />  
</bean>
SqlSessionFactoryBean的结构如下:

可以看出可以注入很多配置值。

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class);

  private Resource configLocation;   //mybatis核心配置文件的路径

  private Resource[] mapperLocations;  //如果有多个核心配置文件,提供一个数组供配置

  private DataSource dataSource;  //数据源

  private TransactionFactory transactionFactory;

  private Properties configurationProperties;   //属性配置

  private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

  private SqlSessionFactory sqlSessionFactory;

  //EnvironmentAware requires spring 3.1
  private String environment = SqlSessionFactoryBean.class.getSimpleName();

  private boolean failFast;

  private Interceptor[] plugins;  //插件配置

  private TypeHandler<?>[] typeHandlers; //类型转换器

  private String typeHandlersPackage;  //类型转换器包

  private Class<?>[] typeAliases;  //别名配置

  private String typeAliasesPackage;  //别名包

  private Class<?> typeAliasesSuperType;

  //issue #19. No default provider.
  private DatabaseIdProvider databaseIdProvider;

  private ObjectFactory objectFactory;

  private ObjectWrapperFactory objectWrapperFactory;
}

四、dispacher-servlet.xml

springmvc属于spring模块,无需整合

创建springmvc.xml文件,配置处理器映射器,适配器,视图解析器

核心配置:

<!--注解扫描类-->
<context:component-scan base-package="xx.xx.xx.controller"></context:component-scan>
<!--配置注解处理器映射器和处理器适配器-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/jsp/"/>
	<property name="sufix" value=".jsp"/>
</bean>

详细配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
	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-3.2.xsd  
		 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
		 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
		 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean id="jsonHttpMessageConverter"
				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="objectMapper" ref="objectMapper" />
				<property name="supportedMediaTypes">
					<list>
						<value>*/*;charset=UTF-8</value>
					</list>
				</property>
			</bean>
			<bean id="stringHttpMessageConverter"
				class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>*/*;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

	<!-- 把标记了@Controller注解的类转换为bean -->
	<context:component-scan base-package="com.xx.xxx.xxxx.controller" />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/backend/" p:suffix=".html" />
		
	<!-- 文件上传 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- set the max upload size 100MB -->
		<property name="defaultEncoding">
			<value>utf-8</value>
		</property>
		<property name="maxUploadSize">
			<value>204857600</value>
		</property>
		<property name="maxInMemorySize">
			<value>204857600</value>
		</property>
	</bean>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值