Spring MVC+hibernate 开发系统(一)

       第一次写博客,不好之处见谅。
       这篇文章主要讨论实际项目从系统设计到系统实现过程中涉及到的问题,因为项目用了很多企业开发公用的技术,所以用文字的形式记录分享给大家。本系统显示 Hibernate 的多对多实例,在 Spring MVC CRUD Web应用程序中连接表。我将同时讨论管理多对多关系在后端。 我们将使用应用程序的Web界面创建,更新,删除和查询。本系统是利用 Spring 的 org.springframework.core.convert.converter.Converter 接口,它帮助我们在项目的数据库中实现实体的映射标识。对于dao层的处理是使用hibernate完成基本Dao的封装;对于数据库的连接采用的是阿里巴巴druid连接池;系统中还增加了自定义异常类和工具类;同时为极大的提升开发效率,系统还利用了热加载技术。
      使用以下技术:
  • Spring 4.3.2.RELEASE
  • Hibernate Core 4.3.10.Final
  • validation-api 1.1.0.Final
  • hibernate-validator 5.1.3.Final
  • Oracle 11g
  • Maven 3
  • JDK 1.8
  • Tomcat 7.0.21
  • eclipse-jee-neon
  • JRebel
  • Log4j
  • JUnit
  • druid

第一步:环境搭建

       Maven安装:Maven安装配置

       SpringMVC +Hibernate开发环境搭建:SpringMVC +hibernate开发环境

      Log4j安装配置:Log4j安装配置

      jerbel插件安装: jerbel插件安装

     按照上述步骤搭建环境,新建一个java Dynamic Web Project,可以尝试运行项目;

   

第二步:创建多对多关联表(由于实际项目表太多,举例几张简单表做演示)

      
      

      APP_USER :包含用户。一个用户可以有多个配置[USER,ADMIN,DB·A]。

     USER_PROFILE : 包含用户配置文件。配置文件可以链接到多个用户。

     APP_USER_USER_PROFILE : 这是一个连接表连接APP_USER&USER_PROFILE中的多对多关系。

第三步:创建目录结构

         

第四步:更新pom.xml,包括所需的依赖关系

          

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>china-soa.com</groupId>
  <artifactId>Mdm</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name></name>
  <url>http://maven.apache.org</url>
  
  <properties>
  		<springframework.version>4.3.2.RELEASE</springframework.version> 
  		 <hibernate.version>5.2.2.Final</hibernate.version> 
  </properties>
  	
  <dependencies>
  
  		<!-- 添加junit支持 -->
  		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		</dependency>
  		
   		<!-- 添加spring支持 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${springframework.version}</version>
		</dependency>
   		
   		<!-- 添加hibernate支持 -->
   		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-core</artifactId>
		    <version>${hibernate.version}</version>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-orm</artifactId>
		    <version>${springframework.version}</version>
		</dependency>
		
		<!-- <dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-hibernate</artifactId>
		    <version>1.2.9</version>
		</dependency> -->
		
		<dependency>
		    <groupId>org.hibernate.common</groupId>
		    <artifactId>hibernate-commons-annotations</artifactId>
		    <version>5.0.1.Final</version>
		</dependency>  
		
		<dependency>
		    <groupId>org.hibernate.javax.persistence</groupId>
		    <artifactId>hibernate-jpa-2.1-api</artifactId>
		    <version>1.0.0.Final</version>
		</dependency>

		
		<!-- apache公共包 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.4</version>
		</dependency>

		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.9.2</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.5</version>
		</dependency>

		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
			<version>1.0.6</version>
		</dependency>
		<dependency>
		    <groupId>commons-fileupload</groupId>
		    <artifactId>commons-fileupload</artifactId>
		    <version>1.3.1</version>
		</dependency>
		
   		
   		<!-- 添加servlet+Jsp+jstl支持 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		
		<!-- 数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>6.0.3</version>
		</dependency>
		 <!-- 添加oracle jdbc driver -->  
	    <dependency>    
	        <groupId>com.oracle</groupId>    
	        <artifactId>ojdbc6</artifactId>    
	        <version>11.2.0</version>
	    </dependency>
		
		
		<!-- 添加阿里巴巴连接池druid支持 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.25</version>
		</dependency>
   		
   		<!-- JSON支持 -->
	    <dependency> 
			<groupId>net.sf.json-lib</groupId> 
			<artifactId>json-lib</artifactId> 
			<version>2.4</version> 
		</dependency> 
		
		<dependency>
		    <groupId>org.codehaus.jackson</groupId>
		    <artifactId>jackson-mapper-asl</artifactId>
		    <version>1.9.13</version>
		</dependency>
		
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.5.3</version>
		</dependency>
   		
   		<!-- 添加日志log4j支持 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.logging.log4j</groupId>
		    <artifactId>log4j-core</artifactId>
		    <version>2.6.2</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.logging.log4j</groupId>
		    <artifactId>log4j-api</artifactId>
		    <version>2.6.2</version>
		</dependency>
		
   		<dependency>
		    <groupId>ognl</groupId>
		    <artifactId>ognl</artifactId>
		    <version>3.1.12</version>
		</dependency>
   		
   		<!-- http客户端工具 -->
		<dependency>
	    	<groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpclient</artifactId>
		    <version>4.5.2</version>
		</dependency>
		
		<dependency>
		    <groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpmime</artifactId>
		    <version>4.5.2</version>
		</dependency>
		   		
  </dependencies>
  
  
  <build>
    <finalName>Mdm</finalName>
    <plugins>  
    <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <version>2.3.2</version>  
        <configuration>  
            <source>1.8</source>  
            <target>1.8</target>  
        </configuration>  
    </plugin>  
</plugins>  
    
    
  </build>
</project>


第五步:准备实体类(model)

/**
 * 
 */
package com.soa.mdm.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;


@Entity
@Table(name = "code_segment")
public class CodeSegment {

	private String segmentId; 
	private String parentId;
	private String rulesName; 
	private String segmentName;
	private Integer segmentSequence; 
	private String outputName; 
	private String segmentAlias; 
	private Integer minLength;
	private Integer maxLength;
	private String segmentSource; 
	private String sourceContent; 
	private String validateSwitch;
	private String statusSwitch;
	private String linkageSwitch; 
	private String description;
	private String remark;
	private String creator;
	private Date createtime;
	private Date updatetime;
	private String version;
	private String extendOne;
	private String extendTwo;
	private String extendThree;
	private String extendFour;
	private String extendFive;

	private CodeRules codeRules; 

	@ManyToOne
	@JoinColumn(name = "rulesId")
	public CodeRules getCodeRules() {
		return codeRules;
	}

	public void setCodeRules(CodeRules codeRules) {
		this.codeRules = codeRules;
	}

	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	public String getSegmentId() {
		return segmentId;
	}

	public void setSegmentId(String segmentId) {
		this.segmentId = segmentId;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getRulesName() {
		return rulesName;
	}

	public void setRulesName(String rulesName) {
		this.rulesName = rulesName;
	}

	public String getSegmentName() {
		return segmentName;
	}

	public void setSegmentName(String segmentName) {
		this.segmentName = segmentName;
	}

	public Integer getSegmentSequence() {
		return segmentSequence;
	}

	public void setSegmentSequence(Integer segmentSequence) {
		this.segmentSequence = segmentSequence;
	}

	public String getOutputName() {
		return outputName;
	}

	public void setOutputName(String outputName) {
		this.outputName = outputName;
	}

	public String getSegmentAlias() {
		return segmentAlias;
	}

	public void setSegmentAlias(String segmentAlias) {
		this.segmentAlias = segmentAlias;
	}

	public Integer getMinLength() {
		return minLength;
	}

	public void setMinLength(Integer minLength) {
		this.minLength = minLength;
	}

	public Integer getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(Integer maxLength) {
		this.maxLength = maxLength;
	}

	public String getSegmentSource() {
		return segmentSource;
	}

	public void setSegmentSource(String segmentSource) {
		this.segmentSource = segmentSource;
	}

	public String getSourceContent() {
		return sourceContent;
	}

	public void setSourceContent(String sourceContent) {
		this.sourceContent = sourceContent;
	}

	public String getValidateSwitch() {
		return validateSwitch;
	}

	public void setValidateSwitch(String validateSwitch) {
		this.validateSwitch = validateSwitch;
	}

	public String getStatusSwitch() {
		return statusSwitch;
	}

	public void setStatusSwitch(String statusSwitch) {
		this.statusSwitch = statusSwitch;
	}

	public String getLinkageSwitch() {
		return linkageSwitch;
	}

	public void setLinkageSwitch(String linkageSwitch) {
		this.linkageSwitch = linkageSwitch;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	public Date getUpdatetime() {
		return updatetime;
	}

	public void setUpdatetime(Date updatetime) {
		this.updatetime = updatetime;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getExtendOne() {
		return extendOne;
	}

	public void setExtendOne(String extendOne) {
		this.extendOne = extendOne;
	}

	public String getExtendTwo() {
		return extendTwo;
	}

	public void setExtendTwo(String extendTwo) {
		this.extendTwo = extendTwo;
	}

	public String getExtendThree() {
		return extendThree;
	}

	public void setExtendThree(String extendThree) {
		this.extendThree = extendThree;
	}

	public String getExtendFour() {
		return extendFour;
	}

	public void setExtendFour(String extendFour) {
		this.extendFour = extendFour;
	}

	public String getExtendFive() {
		return extendFive;
	}

	public void setExtendFive(String extendFive) {
		this.extendFive = extendFive;
	}

	@Override
	public String toString() {
		return "CodeSegment [segmentId=" + segmentId + ", parentId=" + parentId + ", rulesName=" + rulesName
				+ ", segmentName=" + segmentName + ", segmentSequence=" + segmentSequence + ", outputName=" + outputName
				+ ", segmentAlias=" + segmentAlias + ", minLength=" + minLength + ", maxLength=" + maxLength
				+ ", segmentSource=" + segmentSource + ", sourceContent=" + sourceContent + ", validateSwitch="
				+ validateSwitch + ", statusSwitch=" + statusSwitch + ", linkageSwitch=" + linkageSwitch
				+ ", codeRules=" + codeRules + ", description=" + description + ", remark=" + remark + ", creator="
				+ creator + ", createtime=" + createtime + ", updatetime=" + updatetime + ", version=" + version
				+ ", extendOne=" + extendOne + ", extendTwo=" + extendTwo + ", extendThree=" + extendThree
				+ ", extendFour=" + extendFour + ", extendFive=" + extendFive + "]";
	}

}

       本章主要介绍该系统用到的工具、技术以及环境的搭建,对于技术选型、表现层(UI)、业务逻辑层(BLL)、数据访问层(DAL)没有做任何描述。我将在接下来的章节进行详细的说明。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值