myeclipse配置springmvc+hibernate+mysql+tomcat 基于maven

环境:

1.myeclipse 2014 下载+破解。。  http://my-eclipse.cn/

2.hibernate 4.2.5.Final

3.mysql 好像是最新的

4.tomcat v8

5.maven 是myeclipse内置的。。

new 一个webapp的maven的项目

先添加依赖项到pom.xml

    <properties>
        <springframework>4.0.5.RELEASE</springframework>
        <servlet>3.1.0</servlet>
        <jstl>1.2</jstl>
        <xstream>1.4.7</xstream>
        <commons-fileupload>1.3.1</commons-fileupload>
        <jackson>1.9.13</jackson>

    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet}</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl}</version>
        </dependency>

        <!-- hibernate4 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

        <!-- springframework and springmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework}</version>
        </dependency>

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

    </dependencies>

修改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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    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>

    <!-- 对applicationContext.xml内容的解析 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置springmvc 的servlet -->
    <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springservlet-config.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 对从tomcat获得的请求数据进行utf-8编码 -->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--hibernate4 single session -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--springmvc 具体的配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

</web-app>

在项目下添加scource类型的文件夹resource

添加applicationContext.xml(配置spring的bean内容)和springservlet-config.xml(配置对路由的处理和注册controller的路由)

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

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

    <!-- 数据源连接,连接到mysql -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/helloworld"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>

    <!-- hibernate的sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
        <!-- 扫描注解的pojo -->
        <property name="annotatedClasses">
            <list>
                <value>com.hello.domain.Holloworld</value>
            </list>
        </property>
    </bean>

    <!-- 事务处理 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 允许注解形式进行事任处理的dao -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 扫描注解的类包括 dao service 这样才能进行注入 -->
    <context:annotation-config />
    <context:component-scan base-package="com.hello.dao"></context:component-scan>
    <context:component-scan base-package="com.hello.service"></context:component-scan>
</beans>

springservlet-config.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 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/util
  http://www.springframework.org/schema/util/spring-util.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 当直接查找资源时找不到才由这个servlet处理 -->
    <mvc:default-servlet-handler />
    <!-- 如果是"/"则到转发到/hello/index -->
    <mvc:view-controller path="/"
        view-name="forward:/helloworld/index" />

    <!-- 默认的注解 -->
    <mvc:annotation-driven />

    <!-- 开启controller注解支持 -->
    <!-- use-default-filters="false" 只扫描指定的注解 -->
    <context:component-scan base-package="com.hello.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 name="contentType" value="text/html" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

在com.hello.domain下的pojo

package com.hello.domain;

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

/**
 * Holloworld entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "holloworld", catalog = "helloworld")
public class Holloworld implements java.io.Serializable {

    // Fields

    private Integer id;
    private String say;
    private String sayAgain;

    // Constructors

    /** default constructor */
    public Holloworld() {
    }

    /** full constructor */
    public Holloworld(String say, String sayAgain) {
        this.say = say;
        this.sayAgain = sayAgain;
    }

    // Property accessors
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "Say", length = 45)
    public String getSay() {
        return this.say;
    }

    public void setSay(String say) {
        this.say = say;
    }

    @Column(name = "sayAgain", length = 45)
    public String getSayAgain() {
        return this.sayAgain;
    }

    public void setSayAgain(String sayAgain) {
        this.sayAgain = sayAgain;
    }

}

com.hello.dao下的BaseDao和HelloDao

package com.hello.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 * dao层基类,实现增,删,改,查等常用功能。*/
//事务处理注册自动进行事务处理
@Transactional
public class BaseDao<T> {

    private Class<T> entityClass;

    @Resource
    private SessionFactory sessionFactory;

    /**
     * 通过反射获取子类确定的泛型类
     */
    public BaseDao() {
        Type genType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        entityClass = (Class) params[0];
    }

    /**
     * 根据ID加载PO实例
     *
     * @param id
     * @return 返回相应的持久化PO实例
     */
    public T load(Serializable id) {
        return (T) getSession().load(entityClass, id);
    }

    /**
     * 根据ID获取PO实例
     *
     * @param id
     * @return 返回相应的持久化PO实例
     */
    public T get(Serializable id) {
        return (T) getSession().get(entityClass, id);
    }

    public void save(T entity) {
        getSession().save(entity);
    }

    public void remove(T entity) {
        getSession().delete(entity);
    }

    public void removeByID(Serializable id) {
        getSession().delete(get(id));
    }

    public void update(T entity) {
        getSession().update(entity);
    }

    public List<T> getAll() {
        Criteria criteria=getSession().createCriteria(entityClass);
        return criteria.list();
    }

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
}
package com.hello.dao;

import org.springframework.stereotype.Repository;

import com.hello.domain.Holloworld;
//自动注册到bean容器
@Repository
public class HelloDao extends BaseDao<Holloworld>{

}

还有com.hello.service下的HelloService

package com.hello.service;

import java.util.List;

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

import com.hello.dao.HelloDao;
import com.hello.domain.Holloworld;

@Service
public class HelloService {
    //从bean容器查找并注入
    @Autowired
    HelloDao helloDao;

    public List<Holloworld> getHolloworlds() {
        return helloDao.getAll();
    }
}

com.hello.controller的HelloController

package com.hello.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hello.service.HelloService;

@Controller
@RequestMapping(value="/helloworld")
public class HellowordController {
    @Autowired
    HelloService helloService;

    @RequestMapping(value="index",method={RequestMethod.GET})
    public String index(HttpServletRequest request) {
        request.setAttribute("hellos", helloService.getHolloworlds());
        return "index";
    }
}

index.jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
  <head>
  </head>

  <body>
 <c:forEach var="hello" items="${hellos}">
         ${hello.say}<br>
</c:forEach>

  </body>
</html>

mysql的表为

最后目录情况

时间: 2024-11-10 14:18:51

myeclipse配置springmvc+hibernate+mysql+tomcat 基于maven的相关文章

在MyEclipse配置自己安装的Tomcat(2014.08.18)

今天因为要在tomcat下运行一个java项目,第一次嘛,遇到了不少问题,总结分享一下: 第一次,我直接将 MyEclipse 生成的项目放到 tomcat 安装目录的 webapps 目录下,运行出现404,无法访问. 然后想了想,发现这是个错误的做法.应该先通过 MyEclipse 将项目部署到 Tomcat 下: 然后, MyEclipse 自带了 tomcat,我要配置自己安装的 TomCat : (请看参考资料:http://jingyan.baidu.com/article/4853

MyEclipse配置进行Hibernate逆映射

MyEclipse中配置MyEclipse Database Explorer 方法(以mysql 数据库为例) 前言: 之前看到同学转了一篇帖子,就是关于在MyEclipse中配置mysql的,今天自己也试了一试,不得不让我感叹 MyEclipse能力的强大,以前每次要使用mysql,一定会安装一个navicat,,现在可以只用MyEclipse搞定了,呵呵~不要嘲笑我 哈~言归正传,我们开始配置.第一步:打开MyEclipse,在右上角视图切换按钮处,找到MyEclipse DataBase

MyEclipse配置启动多个Tomcat

在实际开发中常常会遇到多个项目同一时候进行,来回切换不太方便,这时可分别部署在多个tomcat下. 改动一些配置可同一时候启动多个tomcat 一開始查阅相关文档,看到一篇文章一个Myeclipse同一时候配置多个Tomcat 试了一下没成功,感觉好复杂.而且此文也到处被转载,也不知道谁是原创 MyEclipse中配置tomcat就不多介绍 假设有两个tomcat改动当中一个就可以 改动tomcat\conf下的server.xml文件 一. 找到 <Server port="8005&q

SpringMVC + Hibernate + MySQL 的简易网页搭建(Control实现篇)

在完成Dao层的实现之后,接下来我们需要继续开发我们网页的Control层以及View层. 从开发网页的角度来说: 1. Control层之下的Dao层和Service层可以看做是一个网页的底层负责与数据库交互,做数据持久化,实现一些逻辑业务等 2. Control层自身则是一个网页的中间层,负责联通Dao层以及View层,将View层的请求传向Dao层,并从Dao层获取数据,再返回到View层进行显示 3. View层则是网页的上层,负责直接与用户进行交互,获取用户的需求,将用户的需求转变成一

SpringMVC + Hibernate + MySQL 的简易网页搭建

首先在搭建一个网站后台前,需要明确出一个合理的网页搭建的结构框架,即从DB  ==>  Dao层 ==>Service层 ==>Control层 ==>View层(该层严格意义上不属于后台)当中,各层的概念和设计思路. Dao层(Data Acess Object):所谓的Dao层,主要是网页的数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,包括网页数据当中的增删改查,DAO层的设计首先是设计DAO的接口的抽象,然后在Spring的配置文件中定义此接口的实现类,之后对于

myeclipse配置maven

1.首先配置好java的运行环境(JDK要1.7及以上版本),网上有详细资料. 2.下载maven,具体下载链接http://maven.apache.org/download.html 3.下载apache-maven-3.2.1-bin.zip后解压到指定位置. 4.新建一个系统变量:M2_HOME , 路径格式是:D:\maven\apache-maven-3.2.1(你的解压路径). 5.配置path环境变量,在path值的末尾添加"%M2_HOME%\bin",注意(之前的p

配置SpringMVC

这一篇我们进行简单的基础的SpringMVC框架的搭建与初步认识,我以一名初学者的身份,一点一点进行Spring框架的搭建. 我介绍下我所使用的编程环境,个人见解,不喜勿喷,仅做参考. 首先,请确定您对jsp或其他网页页面有一点了解,对jstl有一点点了解,理解servlet中的request.response的含义和使用,做过jsp与servlet的直接对接小demo. 我在电脑上已经装好了eclipse-jee-kepler,我用的是eclipse,自己安装好了JDK7,我认为MyEclip

springMVC用法 以及一个简单的基于springMVC hibernate spring的配置

替代struts 1  web.xml中配置springmvc中央控制器 <?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="

SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本

获取[下载地址]   QQ: 313596790   [免费支持更新] 支持三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] A 代码生成器(开发利器);       增删改查的处理类,service层,mybatis的xml,SQL( mysql   和oracle)脚本,   jsp页面 都生成    就不用写搬砖的代码了,生成的放到项目里,可以直接运行 B 阿里巴巴数据库连接