您的位置:首页 > 其它

SSH整合笔记2016-3-1

2016-03-01 22:25 260 查看
集成Struts,hibernate,spring三大框架,

先集合Spring 和 Hibernate

新建项目java项目SSH2

① 导入Spring和hibernate和数据库的架包

该架包分别存放在Spring lib 和Hibernate lib中

② 项目src目录下新建beans.xml文件

1、 导入Spring配置模板

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

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:component-scanbase-package="cn.luo"/>
采用扫描的方式,扫描该包所有类

2、 将sessionFactory交于Spring管理

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<propertyname="dataSource" ref="dataSource"/>

<propertyname="mappingResources">

<list>

<value>com/luo/bean/Employee.hbm.xml</value>

</list>

</property>

<propertyname="hibernateProperties"> 实体类对应的映射文件的位置

<value>

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

hibernate.hbm2ddl.auto=update

hibernate.show_sql=false

hibernate.format_sql=false

</value>

</property>

</bean> 数据库名

3、 配置c3p0数据源

<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">

<property name="driverClass"value="org.gjt.mm.mysql.Driver"/>

<propertyname="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh2?useUnicode=true&characterEncoding=UTF-8"/>

<property name="user"value="root"/>

<propertyname="password" value="1234"/>

<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->

<propertyname="initialPoolSize" value="1"/>

<!--连接池中保留的最小连接数。-->

<propertyname="minPoolSize" value="1"/>

<!--连接池中保留的最大连接数。Default:15 -->

<propertyname="maxPoolSize" value="300"/>

<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

<propertyname="maxIdleTime" value="60"/>

<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->

<propertyname="acquireIncrement" value="5"/>

<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->

<propertyname="idleConnectionTestPeriod" value="60"/>

</bean>

同时将数据源注入到SessionFactory中

4、 配置事务管理器

<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<propertyname="sessionFactory" ref="sessionFactory"/>

</bean>

5、 开启事物管理

<!--使用基于注解方式配置事务-->

<tx:annotation-driventransaction-manager="txManager"/>

注意:如果上述显示错误,则必须引入对应的.xsd文件

上述则就将Spring和Hibernate集成了,测试如下

新建测试类,实例化Spring容器,数据库自动创建表,则成功

③ 新建实体类,并提供getter setter

由于实体类中采用枚举类型,即性别,故建枚举类

④ 写实体类的映射文件.hb.xml

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEhibernate-mapping PUBLIC

"-//Hibernate/Hibernate MappingDTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="com.luo.bean">

<class name="Employee"table="employee">

<id name="username"length="20"/>

<property name="password"length="20" not-null="true"/> 枚举类的位置

<property name="gender"not-null="true" length="5">

<typename="org.hibernate.type.EnumType">

<param name="enumClass">com.luo.bean.Gender</param>

<!-- 12为java.sql.Types.VARCHAR常量值,即保存枚举的字面值到数据库。如果不指定type参数,保存枚举的索引值(从0开始)到数据库-->

<paramname="type">12</param>

</type>

</property>

</class>

</hibernate-mapping>

⑤ 开发业务bean

1、 新建com.luo.service

新建EmployeeService.java

2、 新建service的实现类,采用注解的方式实现Spring的管理

在spring配置文件中,配置了事物,和,Sessionfactory,由于采用扫描的方式

在实现类中

@Service指注解业务类,交于Spring容器管理

@Transactional指开启事物,由于在实现类中,采用Hibernate对数据库进行操作,故有此注解

@Transactional(propation=Propagation.NOT_SUPPPRTED)指此方法不使用事务

具体的增加删除修改的实现方法如下

由于在实现类中需要SessionFactory,故先将spring配置的SessionFactory注入到实现类中:

codesmith
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: