Spring5.0.3 + Hibernate5.0.7 + Struts2.5.13全注解整合(SSH全注解整合)

本文详细介绍了一个基于Java最新框架的SSH(Spring+Hibernate+Struts2)全注解开发项目,包括环境搭建、框架配置、面向接口编程的三层架构实现及前端显示。

欢迎访问个人博客:打开链接

项目下载地址:打开

本项目是基于最新的Java开发框架使用全注解来开发的SSH工程项目。

1. SSH搭建环境

jdk1.8 + tomcat8.5 + eclipse oxygen
Spring5.0.3 + Hibernate5.0.7 + Struts2.5.13

2. 工程总体框架结构

这里写图片描述

3. 所需jar包

这里写图片描述这里写图片描述

4. SSH整合配置

  • (1)JdbcConfig
package com.jinglisen.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import com.mchange.v2.c3p0.ComboPooledDataSource;


@PropertySource(value="classpath:jdbc.properties")
public class JdbcConfig {

    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driverClass}")
    private String driverClass;
    @Value("${jdbc.user}")
    private String user;
    @Value("${jdbc.password}")
    private String password;


    @Bean(name="dataSource")
    public DataSource getDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setJdbcUrl(url);
            ds.setDriverClass(driverClass);
            ds.setUser(user);
            ds.setPassword(password);

            return ds;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean(name="sessionFactory")
    public LocalSessionFactoryBean getSessionFactory(@Qualifier("dataSource")DataSource dataSource){
        LocalSessionFactoryBean factory = new LocalSessionFactoryBean();

        factory.setDataSource(dataSource);

        Properties props = new Properties();
        props.setProperty("hibernate.show_sql", "true");
        props.setProperty("hibernate.format_sql", "true");
        props.setProperty("hibernate.hbm2ddl.auto", "update");
        factory.setHibernateProperties(props);
        factory.setPackagesToScan("com.jinglisen.domain");

        return factory;
    }


}
  • (2)SpringConfig
package com.jinglisen.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

@Configurable
@ComponentScan(value="com.jinglisen")
@Import(value={JdbcConfig.class,TransactionConfig.class})
public class SpringConfig {

}
  • (3)TransactionConfig
package com.jinglisen.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
public class TransactionConfig {

    @Bean(name="transactionManager")
    public HibernateTransactionManager getTransactionManager(@Qualifier("sessionFactory")SessionFactory sessionFactory){
        HibernateTransactionManager manager = new HibernateTransactionManager();
        manager.setSessionFactory(sessionFactory);
        return manager;
    }
}
  • (4)WebStart
package com.jinglisen.config;

import org.springframework.web.context.AbstractContextLoaderInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

/**
 * web启动
 * @author JasonJing
 *  */
public class WebStart extends AbstractContextLoaderInitializer{

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(SpringConfig.class);
        return ac;
    }
}
  • (5)jdbc.properties
jdbc.url=jdbc:mysql:///ssh
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
  • (6)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSH_Anotation</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <welcome-file-list>
    <welcome-file>save.jsp</welcome-file>

  </welcome-file-list>
</web-app>

5. 面向接口编程三层架构文件

  • (1)CustomerDao
package com.jinglisen.dao;

import java.util.List;

import com.jinglisen.domain.Customer;

public interface CustomerDao {

    public void save(Customer customer);
}
  • (2)CustomerDaoImpl
package com.jinglisen.dao.impl;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.jinglisen.dao.CustomerDao;
import com.jinglisen.domain.Customer;
/**
 * HibernateDaoSupport的写�?
 * @author JasonJing
 *
 */
@Repository
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

    //注入SessionFactory,创建HibernateTemplate
    @Resource
    public void setMySessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public void save(Customer customer) {
        this.getHibernateTemplate().save(customer);
    }

}
  • (3)Customer
package com.jinglisen.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_customer")
public class Customer implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @Column(name="cust_name")
    private String custName;

    @Column(name="cust_phone")
    private String custPhone;

    @Column(name="cust_address")
    private String custAddress;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    public String getCustAddress() {
        return custAddress;
    }
    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

}
  • (4)CustomerService
package com.jinglisen.service;

import com.jinglisen.domain.Customer;

public interface CustomerService {

    public void save(Customer customer);

}
  • (5)CustomerServiceImpl
package com.jinglisen.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jinglisen.dao.CustomerDao;
import com.jinglisen.domain.Customer;
import com.jinglisen.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    //注入CustomerDao
    @Resource
    private CustomerDao customerDao;

    @Override
    public void save(Customer customer) {
        customerDao.save(customer);
    }

}
  • (6)CustomerAction
package com.jinglisen.web.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jinglisen.domain.Customer;
import com.jinglisen.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/customer")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
    //模型驱动接收页面参数
    private Customer customer = new Customer();
    @Override
    public Customer getModel() {
        return customer;
    }

    @Resource
    private CustomerService custService;

    @Action(value="save",results={@Result(name="success",location="/succ.jsp")})
    public String save(){
        System.out.println("执行CustomerAction的save");
        custService.save(customer);
        return SUCCESS;
    }
}

6.前端显示

save.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>客户添加页面</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>
    <form action="customer/save.action" method="post">
        客户姓名:<input type="text" name="custName"/><br/>
        客户电话:<input type="text" name="custPhone"/><br/>
        客户地址:<input type="text" name="custAddress"/><br/>
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

succ.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>成功提示页面</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>
    添加成功!
  </body>
</html>

项目下载地址:打开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的凯尔森同学

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值