ssh maven mysql_基于Maven的SSH框架搭建

1.工程介绍

工程是结合了Spring+struts2+hibernate,实现了一个简单的form表单提交的功能,可能需要对spring,struts2,hibernate有一个基础的了解才好理解。

2.工程结构图

首先来看一下整个工程的结构

c8c990ed1f2c4196ad3f70999d9337ae.png

3.java文件

User.java  -  用于往数据库中存入数据

package com.ssh.bean;

public class User {

private int userId;

private String username;

private String password;

private int gender;

public int getUserId() {

return userId;

}

public void setUserId(int userId) {

this.userId = userId;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getGender() {

return gender;

}

public void setGender(int gender) {

this.gender = gender;

}

}

UserForm.java  -  用于接收页面传来的表单数据

package com.ssh.forms;

public class UserForm {

private String username;

private String password;

private int gender;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getGender() {

return gender;

}

public void setGender(int gender) {

this.gender = gender;

}

}

UserDao.java  -  用于实现数据接口层

package com.ssh.dao;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

@Repository

public class UserDao{

@Autowired

private SessionFactory sessionFactory;

private Session getCurrentSession() {

return sessionFactory.openSession();

}

public void saveObject(Object obj) throws HibernateException {

this.getCurrentSession().save(obj);

}

}

UserService.java  -  用于实现业务层逻辑

package com.ssh.service;

import org.hibernate.HibernateException;

import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.ssh.bean.User;

import com.ssh.dao.UserDao;

import com.ssh.forms.UserForm;

@Service

public class UserService {

@Autowired

private UserDao dao;

public void regUser(UserForm userForm) throws HibernateException {

User user = new User();

BeanUtils.copyProperties(userForm, user);

dao.saveObject(user);

}

}

RegisterAction.java  -  struts2的Action

package com.ssh.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import com.ssh.forms.UserForm;

import com.ssh.service.UserService;

public class RegisterAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private UserForm user;

@Autowired

private UserService userService;

public UserForm getUser() {

return user;

}

public void setUser(UserForm user) {

this.user = user;

}

public String execute() {

try {

userService.regUser(user);

return SUCCESS;

} catch (Exception e) {

e.printStackTrace();

return ERROR;

}

}

}

4.配置信息

如第一张工程结构图可知,为了结构清晰,ssh的所有配置信息都放在resource路径下面。

struts.xml  -  struts2配置

struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

/Register.jsp

/success.jsp

/fail.jsp

hibernate.cfg.xml  -  hibernate的一些配置信息

hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

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

org.hibernate.dialect.MySQLDialect

true

true

create

在本工程中将原来在hibernate中的数据源信息和关联的*.hbm.xml文件配置到了spring配置文件中由spring容器管理。

为了让工程结构调理清晰保留了hibernate.cfg.xml文件,并且把hibernate的基本属性配置写在了里面,也方便以后添加其他的配置信息。(hibernate的基本属性也可以配置到spring的配置文件当中,然后删除掉hibernate.cfg.xml文件,后面会具体讲述如何配置)

db.properties  - 抽取了数据源的基本信息,方便以后修改

jdbc.user=root

jdbc.password=123456

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql:///ssh1

jdbc.initPoolSize=5

jdbc.maxPoolSize=10

applicationContext.xml  -  spring配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

上面提到可以删除hibernate.cfg.xml文件,可以将spring配置文件中的  id=sessionFactory 的bean 替换如下即可:

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

org.hibernate.dialect.MySQL5InnoDBDialect

true

true

update

web.xml  -  额外指定了 spring 和 struts2配置文件的路径

web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

config

../../resources/struts.xml

contextConfigLocation

classpath:applicationContext.xml

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

org.springframework.web.context.ContextLoaderListener

Register.jsp

pom.xml  -  配置了一些ssh框架所必需的jar包,有maven自动下载到本地仓库

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com

ssh1

war

0.0.1-SNAPSHOT

ssh1 Maven Webapp

http://maven.apache.org

junit

junit

4.10

test

org.apache.struts

struts2-core

2.3.24

org.apache.struts

struts2-spring-plugin

2.3.24.1

org.hibernate

hibernate-core

4.2.2.Final

org.springframework

spring-core

4.2.4.RELEASE

org.springframework

spring-beans

4.2.4.RELEASE

org.springframework

spring-orm

4.2.4.RELEASE

org.springframework

spring-web

4.2.4.RELEASE

org.springframework

spring-context

4.2.4.RELEASE

mysql

mysql-connector-java

5.1.26

c3p0

c3p0

0.9.1.2

commons-dbcp

commons-dbcp

1.4

log4j

log4j

1.2.16

org.slf4j

slf4j-api

1.6.1

org.slf4j

slf4j-nop

1.6.4

javassist

javassist

3.11.0.GA

ssh1

5.页面文件

Register.jsp

pageEncoding="UTF-8"%>

html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

User Register Page

name="user.gender" label="性别" value="1">

success.js

pageEncoding="UTF-8"%>

html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

User Register Page

SUCCESS

6.配置中遇到的一些问题和解决方法

1.hibernate配置了自动建表,但是程序运行之后不仅没有自动建表而且还总是提示无法找到表。

解决方法:hbm2ddl.auto 改成hibernate.hbm2ddl.auto 当然值也要改成create才可以自动建表。

.......

源代码在此:点击此处下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值