您的位置:首页 > 编程语言 > Java开发

Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目

2016-04-08 16:45 1081 查看
原文:Java web 项目搭建

Java web 项目搭建

简介

在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring+Hibernate的架构搭建一个java web项目


Spring MVC+Spring+Hibernate结构介绍如下:




Spring MVC
Spring MVC的职责是作为web层框架,实现了我们常见的MVC模式

Spring
Spring 作为服务层,利用自身特性IOC、AOP,提供数据从DAO层到Web层的桥梁作用。

Hibernate
Hibernate作为熟知的ORM框架,提供了从数据库读取数据映射到实体的功能,这里我们将Hibernate服务于DAO层


项目结构

基于Spring MVC+Spring+Hibernate架构,面向接口编程,搭建项目,分为Model、DAO、Service、ViewModel、Web层,具体介绍如下



Model
分为数据库实体和逻辑model两块,提供DAO和Service的数据实体和业务逻辑model

DAO
基于Hibernate,提供数据库读取,提供基本的增删改查和数据访问功能,供Service调用

Service
业务逻辑层,通过调用DAO的提供的数据访问接口,整合业务逻辑,将数据提供给Web层,基于Spring的依赖注入和切面编程,提供面向接口的服务

ViewModel
页面展现model,提供给web层

Web
提供一个用户界面,调用Service提供的数据接口,实现用户交互操作


项目搭建

基于Intellij IDEA开发工具,搭建Web项目,依赖了Hibernate,Spring ,Spring MVC等框架,具体搭建如下

创建整体项目

1.打开IntellijFile-->New Project,选择Maven,下一步,输入Group IDArtifactId,如下图所示



2.输入项目名称com.ganji.demo,点击finish,项目创建完成

添加Model模块

1.添加model模块
打开IntellijFile-->New Maven,选择Maven,下一步,输入GroupIDArtifactId,如下图所示,注意Parent隶属于com.ganji.demo,继续model name填写com.ganji.demo.model



2.添加实体
可以通过安装hibernate自动将数据库表映射成实体类,


添加实体的办法如下




在resources下配置数据库连接文件,gcrm.cfg.xml,具体内容如下


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://ip地址:端口号/数据库名</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">username</property>
<property name="connection.password">pwd</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

添加后,右上角会提示Add to hibernate facet configuration,点击添加,下面生成实体会用到。



在项目名称上,右键添加Add Framework Support,左侧勾选Hibernate,然后ok即可(最新版Intellij IDEA则为项目名称上,添加Module模块中 选择 Spring Hibernate)。

菜单栏View-->Tool Window-->Persistence,打开Persistence。

在Persistence model项目上右键,Generate Persistence Mapping-->By Database Schema,

配置生成数据库实体类,具体配置如下





添加DAO模块

1.添加dao模块
打开IntellijFile-->New Maven,选择Maven,下一步,输入GroupIDArtifactId,注意Parent隶属于com.ganji.demo,继续model name填写com.ganji.demo.dao

2.添加dao对model模块的引用
在dao模块上,右键Open Model Settings,点击右侧绿色+号,选择Module Dependency,选择model模块即可

3.添加对Hibernate框架的引用
在dao模块上,右键Add Framework Support,选择hibernate,然后确定即可。

添加Service模块

1.添加service模块
打开IntellijFile-->New Maven,选择Maven,下一步,输入相应GroupIDArtifactId,注意Parent隶属于com.ganji.demo,继续model name填写com.ganji.demo.service

2.添加model、dao模块的引用
同dao模块添加model引用

添加ViewModel模块

1.添加viewmodel模块
打开IntellijFile-->New Maven,选择Maven,下一步,输入相应GroupIDArtifactId,注意Parent隶属于com.ganji.demo,继续model name填写com.ganji.demo.viewmodel

添加Web模块

1.添加web模块
打开IntellijFile-->New Maven,选择Maven,勾选create from archetype,选择maven -archetype-webapp,下一步,输入相应GroupIDArtifactId,注意Parent隶属于com.ganji.demo,继续model name填写com.ganji.demo.web

2.添加对service、viewmodel、model模块的引用
同dao模块添加model引用

3、添加对spring mvc framework的引用
在dao模块上,右键Add Framework Support,选择spring-->spring mvc,然后确定即可。

4、配置web.xml文件
配置web.xml,添加servlet属性,作为web容器,进行url的分发操作
配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocatin</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

5、配置dispatcher-servlet.xml
webappweb-inf目录下,添加dispatcher-servlet.xml,上述web.xml中servlet指定的分发配置文件,配置如下内容

<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >

<!--开启注解映射支持-->
<mvc:annotation-driven/>

<!-- 开启controller注解支持 -->
<!-- use-default-filters="false" 只扫描指定的注解 -->
<context:component-scan base-package="com.ganji.demo.web.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 开启视图解析支持 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!--开启依赖注入-->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="demoUserService" class="com.ganji.demo.service.user.DemoUserServiceImpl"></bean>

</beans>


参考资料

IntelliJ IDEA 12 与 Tomcat 集成并运行Web项目
使用Intellij IDEA从零使用Spring MVC
Intellij IDEA创建Maven Web项目
IntelliJ IDEA 12创建Maven管理的Java Web项目(图解)
SpringMVC学习系列(2) 之 经典的HelloWorld实现
SpringMVC入门教程
spring mvc ModelAndView的 Model 值 在jsp中不显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: