您的位置:首页 > 编程语言 > Java开发

简单搭建注解ssh项目(一)

2017-01-07 11:12 323 查看
1.使用的IDE:Myeclipse2013
2.使用数据库:mysql
3.使用hibernate:hibernate4.1.4,spring:spring3.1,struts2:struts2-2.3.16
4.jdk1.6    tomcat6

OK,环境准备好之后就可进行框架搭建了

1.在myeclipse中新建一个web项目



2.添加框架支持(为了不用写xml配置文件,一会儿就删掉),分别添加spring,hibernate,struts2,版本就用自带最新的即可

2-1:添加spring框架:下一步下一步即可



2-2:添加hibernate框架之前我们先添加数据源的配置,以后就不需要再次配置了。

1.切换一个工作台到hibernate



2.创建数据源



3.我们使用mysql,保存密码



4.点击Test  Driver,如果出现以下内容表示连接成功



5.下一步直到结束为止。

2-3:添加hibernate框架:注意如图

1.不需要SessionFactory Class.因为我们将SessionFactory交给Spring管理



2.选择刚刚创建的数据源,下一步到结束为止



2-4:添加Struts2的框架支持:下一步即可:注意如图,将核心控制器的过滤规则改为/*



3.添加完框架支持之后,将三个框架的支持在remove掉(右击builder path:remove掉即可),删除之后就是这个样子



4.添加项目所需要的jar包:

sshjar包下载地址

还缺少三个包:下载地址(一定要加入这三个jar包。struts注解使用)



5.编辑spring的配置文件

5-1.导入context命名空间



5-2.开启注解自动扫描:会扫描com.yy包下所有包中所有带注解的类。在spring中将这些类管理起来

<context:component-scan base-package="com.yy"></context:component-scan>
5-3.开启持久化类的自动装配:<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 这一段就不需要了 -->
<!-- <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property> -->
<!-- 实体类自动扫面装配 -->
<property name="packagesToScan" value="com.yy.po">
</property>
</bean>5-4.配置声明式事务
<!-- 配置声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 导入tx命名空间 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
5-5.spring配置文件的内容目前:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/company">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 这一段就不需要了 -->
<!-- <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property> -->
<!-- 实体类自动扫面装配 -->
<property name="packagesToScan" value="com.yy.po">
</property>
</bean>
<!-- 开启注解自动扫描 -->
<context:component-scan base-package="com.yy"></context:component-scan>

<!-- 配置声明式事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 导入tx命名空间 --> <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
6.struts2的配置文件内容:
<struts>
<!-- 将spring的action创建交给spring管理 -->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java spring ssh 注解ssh