ssh 初步整合

Struts2+Spring+Hibernate
Struts2.3.15 + spring 2.5.6 +hibernate 3.610
开始的时候就加了有一些包基础包、对于那些是必须要的包、也是分不清楚(还得请大神指教) 但多是却哪些包在补、

web 配置:
<!-- Struts2 web配置 过滤器 -->
<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>

<!-- spring 监听器 扫描 config 文件下的applicationContext.xml文件 -->
<!-- 可以是 *.xml 表示扫描所有的xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Spring 扫描注解的路径 -->
<context:annotation-config />
<context:component-scan base-package="com" />

<!-- 将数据库连接的基本信息填写在properties文件夹里 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:config/jdbc.properties</value>
</property>
</bean>

<!-- 调用jdbc.properties里的信息 -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>


<!-- 使用注解的形式将model里面的对象交给hibernate来管理 -->
<bean id="sf"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="packagesToScan">
<list>
<value>com.Model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- Mysql方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 打印出sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- 通过spring来管理实务 具体是怎么管理的????-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean>
</beans>

Struts.xml 配置:

<struts>
<package name="" extends="struts-default" >
<!-- userAction 是通过spring来管理的 -->
<action name="index" class="userAction" method="select">
<result name="success">/login.jsp</result>
</action>
</package>
</struts>

action层 UserAcrion 类:
/**
* action类 相当于一个control层、、
* spring通过注解得到 IUserService的一个对象
* 注解写在getter上
*/
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {
private IUserService userService;
// getter 和setter @Resource写在getter上
public String select(){
User user;
user=userService.selectUser();
return SUCCESS;
}
}

service 层: UserServiceImp
/**
* service层对数据逻辑的操作
* IUserMapper里面对数据库操作
* 通过注解实例
*/
@Service("userService")
public class UserServiceImp implements IUserService {
// getter 和setter @Resource写在getter上
private IUserMapper userMapper;
//操作业务逻辑
public User selectUser() {
User user=userMapper.select();
return user;
}
}

dao层 UserMapperImp
/**
* Mapper层管理数据库
* 增删改查
* 已经将hibernate交给spring来管理
* 所以可以通过注入得到HibernateTemplate对象
*/
@Component("userMapper")
public class UserMapperImp implements IUserMapper {
// getter 和setter @Resource写在getter上
private HibernateTemplate hibernateTemplate;
//查找 find("hql") hql查询语句
public User select() {
//具体操作语句见文档、为了能将小框架打通、简单得到一个对象
List<User> u= hibernateTemplate.find("from User u where u.name='nie'");
User user=u.get(0);
return user;
}
}
对于model层、就只有设置:类前面@entity 主键前面@Id和@GeneratedValue 只要名称跟数据库里面相同就可以
对于SSH配置还不熟悉、部署项目效率不高、而且很多地方不完善、欢迎小伙伴来提意见来改进、
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值