您的位置:首页 > 其它

SSH配置流程 (基于注解方式)

2017-09-21 22:05 381 查看
1.新建一个web project, 记得勾选生成web.xml(以便后期修改)

2.选择到项目右键选择Configure facets/Install Apache Struts(2.x)facet 加载struts的依赖包

3.如上..Configure facets/Install Spring facet 加载spring依赖包

4.如上..Configure facets/Install Hibernate facet 加载hibernate依赖包

如上234点事配置SSH的依赖包, 如果发现有重复出现的依赖包, 挑出版本高的剩下的可以选择右击项目选择Build Path / Configure Build Path将该框架包中的重复包勾选掉

至此, SSH三个框架的依赖包基本配置完毕

5.在src文件夹下面创建jdbc.properties(数据源参数加载文件夹)

模板 : 

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/数据库名

jdbc.user=xxx

jdbc.password=xxx

c3p0.initialPoolSize=10

c3p0.maxIdleTime=30

c3p0.maxPoolSize=100

c3p0.minPoolSize=10

6.打开src文件夹下的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:mvc="http://www.springframework.org/schema/mvc"

  xmlns:p="http://www.springframework.org/schema/p"

  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-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/aop
  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 加载jdbc配置文件的bean(上面创建的jdbc.properties文件) -->
<bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<!--配置数据源(这里使用的数据源是c3p0)-->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value = "${jdbc.driverClass}"></property>
<property name="jdbcUrl"
value="${jdbc.jdbcUrl}">
</property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 初始化数据连接池的连接数 -->
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<!-- 初始化连接最大空闲时间 -->
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<!-- 初始化连接池的最大连接数 -->
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<!-- 初始化连接池的最少连接数 -->
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
</bean>

<!-- 创建sessionFactory(数据库会话工厂)bean, 用于管理数据库连接管理-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</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>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com/xxx/pojo</value>
</list>

</property>

<!--加载逆向工程生成的数据库的实体类-->
<property name="annotatedClasses">
<list>
<value>com.xxx.pojo.Scenicalert</value>
<value>com.xxx.pojo.Goodkind</value>
</property>
</bean>

<!-- 配置注解组件(代表声明开启注解模式) -->
<context:annotation-config/>
<!-- 配置注解自动扫描的dao, service.impl, action包 -->
<context:component-scan base-package="com.xxx.test.dao.impl"/>
<context:component-scan base-package="com.xxx.test.service.impl"/>
<context:component-scan base-package="com.xxx.test.action"/>

<!-- 配置hibernateTemplate bean (dao.impl层的模板)-->
<bean id = "hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 配置事务管理器的bean -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 加入注解事务管理器 -->
<tx:annotation-driven transaction-manager="txManager"/>

</beans>

7.配置日志打印

(1)检查SSH依赖包中是否有log4j-x.x.x.jar和slf4j-log4j12-x.x.x.jar包(如果没有如上依赖包重复出现操作类似, 只不过现在是添加进来)

(2)同时在src文件夹下面创建log4j.properties日志打印配置文件

模板 :

#输出的登记为INFO, 输出类型是console, 输出的目的地是logfile\

log4j.rootCategory=INFO,console,logfile

#配置控制台的日志

#输出的目的地是控制台

log4j.appender.console=org.apache.log4j.ConsoleAppender

#设置输出时候的端的布局是哪种布局

log4j.appender.console.layout=org.apache.log4j.PatternLayout

#指定输出的具体信息, 以及具体格式

log4j.appender.console.layout.ConversionPattern= %p %d{yyyy-MM-dd HH:mm:ss} - %t - [%c] : %m %n

#配置指定日志打印日志文件

#配置日志文件的路径

log4j.appender.logfile.File=logs\\xxxx_log\\xxx.log

#配置日志文件每天产生一个

log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender

#配置日志文件的日期格式

log4j.appender.logfile.DatePattern=.yyyy-MM-dd

#配置日志文件的布局格式

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

# 配置日志文件日志打印的格式

log4j.appender.logfile.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss} - %t - [%c] : %m %n

(3)在web.xml中加入如下配置

模板 : 

<!-- 配置日志打印监听器-->

  <context-param>

  <param-name>log4jConfigLocation</param-name>

  <param-value>classpath:log4j.properties</param-value>

</context-param>

<context-param>

  <param-name>log4jRefreshInterval</param-name>

  <param-value>3600000</param-value>

</context-param>

<!-- 需要添加spring-web.jar包,否则用发生错误信息 -->

<listener>

  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>

8.修改src文件夹下的struts.xml文件(无需配置action, 使用的是注解方式)

模板 : 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<!-- 启用开发者模式,页面报错信息会更加详细 -->

<constant name="struts.devMode" value="true"/>

<!-- 指定由spring负责action对象的创建(必选) -->

<constant name="struts.objectFactory" value="spring"/>

<!-- 国际化设置,请求参数指定为utf-8 -->

<constant name="struts.i18n.encoding" value="utf-8"/>

<!-- 指定每次请求到达,重新加载资源文件 -->    

<constant name="struts.i18n.reload" value="false" />    

    

<!-- 指定每次配置文件更改后,自动重新加载 -->    

<constant name="struts.configuration.xml.reload" value="false" /> 

</struts>  

至此配置文件基本完成

9.对数据库的表进行逆向工程

(1)转换到数据库连接视图(同时创建一个连接连接到目标数据库)

(2)打开数据库连接(逐层打开)

选到数据库表右键选择Hibernate Reverse Enginering

如下图勾选

至此逆向工程生成实体类完成

10.可以建包写代码

包的结构:

dao层接口 :

模板 :

package com.xxx.text.dao;

public interface UseraccountDao {
public void addUseraccount();

}

dao实现层

模板 :

package com.xxx.test.dao.impl;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;

import org.springframework.orm.hibernate3.HibernateTemplate;

import org.springframework.stereotype.Repository;

import com.xxx.pojo.Role;

import com.xxx.pojo.Useraccount;

import com.xxx.text.dao.UseraccountDao;

@Repository(value = "useraccountDaoImpl")

public class UseraccountDaoImpl implements UseraccountDao{

@Resource(name = "hibernateTemplate")
private HibernateTemplate hibernateTemplate;

@Override
public void addUseraccount() {
// TODO Auto-generated method stub
代码.....
}

}

Service 接口层 :

模板 : 

package com.xxx.test.service;

public interface RegisterService {
public void RegisterAccount();

}

service实现层 :

模板:

package com.xxx.test.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.xxx.test.service.RegisterService;

import com.xxx.text.dao.UseraccountDao;

@Service(value = "registerServiceImpl")

public class RegisterServiceImpl implements RegisterService{

@Resource(name = "useraccountDaoImpl")
private UseraccountDao useraccountdao;

@Transactional
@Override
public void RegisterAccount() {
// TODO Auto-generated method stub
useraccountdao.addUseraccount();
}

}

action层 :

模板 :

package com.xxx.test.action;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.struts2.convention.annotation.Namespace;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import org.apache.struts2.convention.annotation.Results;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

import com.xxx.test.service.RegisterService;

@Controller(value = "register.action")

@Scope("prototype")//单例模式

@ParentPackage("struts-default")

@Namespace("/")

@Results({
@Result(name = "success", location="/index.jsp"),
@Result(name = "false", location="/error.jsp")

})

public class RegisterAction {

@Resource(name = "registerServiceImpl")
private RegisterService registerService;

private Log logger = LogFactory.getLog(getClass());

@Action(value = "register")
public String execute(){
registerService.RegisterAccount();
return "success";
}

}

前端页面 :

模板 :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>

  

  <body>

    This is index JSP page. <br>

    <form action="register.action">

    <input type = "submit" value = "点击我">

    </form>

  </body>

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