环境:
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