`

重拾springmvc+hibernate

    博客分类:
  • Java
阅读更多

多年不用java了,最近被迫重拾java,叫喊,还是那么厚重,但是也有些变化和新的感觉,就当是体会记忆的味道吧!

版本介绍:

  • jdk 7
  • springmvc 4.1.5
  • hibernate  4.3.5
  • maven model 4.0.0
  • db2 type4
  • tomcat 7
  • eclipse Luna Release (4.4.0)

使用maven做包管理:pom.xml

Steps:

  1. create a dynamic java web project
  2. convert to a maven project
  3. add springmvc dependency
  4. edit some xml config files: web.xml, applicationContext.xml and so on.

功能介绍:

  1. 首先springmvc,hibernate相关的配置, 使用spring的dispatcherservlet做拦截,根据不同的资源和请求做控制,比如静态资源的拦截,控制器的拦截和请求分派
  2. 连接的数据库使用db2, 驱动的配置,default_schema的配置,事务管理,会话管理等实现数据访问
  3. ajax请求的数据处理,json的数据格式的处理,使用jackson的jar文件

感想:(纯属个人观点,欢迎讨论)

  1. 对于很久不用java的程序员,java的一些思想还是对我有很多影响的,比如封装,继承的方式,分层处理
  2. 关于web开发的方式,java比以前灵活多了,特别是springmvc的dispatcher方式, 现在很多主流的web开发语言在web开发方面的框架路子都差不多,想Ruby on Rails, Django, Thinkphp. 差别在于语言本身的性能和使用场景方面
  3. 相对应其他语言的框架,springmvc的配置还是有点繁琐,还有jar的版本众多,有点不知所措了,有时候一个版本不可以用,但是不知道陷阱在哪里

主要代码文件内容如下:

 

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
 <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <servlet>
      <servlet-name>MVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
              /WEB-INF/config/mvc*.*
          </param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>MVC</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       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-4.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

<!-- 使用注解 -->
    <mvc:annotation-driven />
<!-- 自动扫描 -->
    <context:component-scan base-package="com.alecchyi.*" />  
<!-- 页面拦截相关 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/page/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
<!-- 静态资源相关 -->
    <mvc:resources mapping="/js/**" location="/js/" cache-period="31536000" />
    <mvc:resources mapping="/css/**" location="/css/" cache-period="31536000" />
<!-- 数据库连接相关 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
            <value>com.alecchyi.demo.entry</value>
            <value>com.alecchyi.demo.dao</value>
            <value>com.alecchyi.demo.service</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.default_schema">ruby</prop>
            </props>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
        <property name="url" value="jdbc:db2://localhost:50000/demo" />
        <property name="username" value="db2admin" />
        <property name="password" value="password" />
    </bean>
<!-- 事务相关 --> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config expose-proxy="true">
		<aop:pointcut id="txPointcut" expression="execution(* com.alecchyi.demo.service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>
<!-- 数据操作相关 -->
    <bean id="usersDao" class="com.alecchyi.demo.dao.UsersDao">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="userService" class="com.alecchyi.demo.service.UserService">
        <property name="usersDao" ref="usersDao" />
    </bean>
</beans>

 

dao.java内容:

package com.alecchyi.demo.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.alecchyi.demo.entry.Users;

public class UsersDao {
	private SessionFactory sessionFactory;

	public List<Users> getAllUsers(){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from users");
		return query.list();
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}	
}

 

service.java内容:

package com.alecchyi.demo.service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.alecchyi.demo.dao.UsersDao;

public class UserService {

	private UsersDao usersDao;
	
	public UsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public int userCount(){
		return usersDao.getAllUsers().size();
	}
}

 

entity.java内容:

package com.alecchyi.demo.entry;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name="users")
public class Users {

	public Users(){
		super();
	}
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id")
	private Integer id;
	
	public Integer getId() {
		return id;
	}

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

	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 String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	@Column(name="username")
	private String username;
	
	@Column(name="password")
	private String password;
	
	@Column(name="nickname")
	private String nickname;
	
	@Column(name="age")
	private Integer age;
	
	@Column(name="birth")
	private Date birth;
	
}

 

controller.java内容:

package com.alecchyi.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alecchyi.demo.service.UserService;

@Controller
public class PortalController {

	@Resource(name="userService")
	private UserService userService;

	@RequestMapping("demo")
	public ModelAndView portal(){
		ModelAndView view = new ModelAndView("portal");
		return view;
	}
	
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(HttpServletRequest request){
		String username = request.getParameter("name");
		System.out.println(userService.userCount());
		Map<String,Object> map = new HashMap<String,Object>(1);
		map.put("username", username);
		return map;
	}
}

 

view.jsp内容:

<script type="text/javascript">
jQuery(document).ready(function($){
	$("#link").click(function(){
		$.ajax({
			url:"update",
			dataType:"json",
			data:"name=ruby",
			type:"post",
			success:function(data){
				alert(data.username);
			},
			error:function(){
				alert("error");
			}
		});
	});
});
</script>

 

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SpringDemo</groupId>
  <artifactId>SpringDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Spring</name>
  <description>Spring</description>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<scope>runtime</scope>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>com.fasterxml.jackson.core</groupId>
  		<artifactId>jackson-databind</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>com.fasterxml.jackson.core</groupId>
  		<artifactId>jackson-core</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>com.fasterxml.jackson.core</groupId>
  		<artifactId>jackson-annotations</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-core</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-config</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-core</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-orm</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.aspectj</groupId>
  		<artifactId>aspectjrt</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.aspectj</groupId>
  		<artifactId>aspectjweaver</artifactId>
  	</dependency>
  </dependencies>
  <dependencyManagement>
  	<dependencies>
  		<dependency>
  			<groupId>org.springframework</groupId>
  			<artifactId>spring-context</artifactId>
  			<version>4.1.4.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework</groupId>
  			<artifactId>spring-webmvc</artifactId>
  			<version>4.1.4.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>com.fasterxml.jackson.core</groupId>
  			<artifactId>jackson-databind</artifactId>
  			<version>2.5.1</version>
  		</dependency>
  		<dependency>
  			<groupId>com.fasterxml.jackson.core</groupId>
  			<artifactId>jackson-core</artifactId>
  			<version>2.5.1</version>
  		</dependency>
  		<dependency>
  			<groupId>com.fasterxml.jackson.core</groupId>
  			<artifactId>jackson-annotations</artifactId>
  			<version>2.5.1</version>
  		</dependency>
  		<dependency>
  			<groupId>org.hibernate</groupId>
  			<artifactId>hibernate-core</artifactId>
  			<version>4.3.5.Final</version>
  		</dependency>
  		<dependency>
  			<groupId>mysql</groupId>
  			<artifactId>mysql-connector-java</artifactId>
  			<version>5.1.29</version>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework.security</groupId>
  			<artifactId>spring-security-config</artifactId>
  			<version>3.2.5.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework.security</groupId>
  			<artifactId>spring-security-core</artifactId>
  			<version>3.2.5.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework.security</groupId>
  			<artifactId>spring-security-web</artifactId>
  			<version>3.2.5.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework</groupId>
  			<artifactId>spring-jdbc</artifactId>
  			<version>4.1.4.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework</groupId>
  			<artifactId>spring-orm</artifactId>
  			<version>4.1.4.RELEASE</version>
  		</dependency>
  		<dependency>
  			<groupId>org.aspectj</groupId>
  			<artifactId>aspectjweaver</artifactId>
  			<version>1.7.4</version>
  		</dependency>
  		<dependency>
  			<groupId>org.aspectj</groupId>
  			<artifactId>aspectjrt</artifactId>
  			<version>1.7.4</version>
  		</dependency>
  	</dependencies>
  </dependencyManagement>
</project>

 

 

 

分享到:
评论

相关推荐

    基于Spring+SpringMVC+Hibernate的图书管理系统

    系统采用技术:Spring+SpringMVC+Hibernate+jQuery+Ajax+面向接口编程。 简介:该图书管理项目实现了三种角色的功能,分别是超级管理员,图书管理员和读者。在系统中,实现了图书管理,统计管理,系统管理和系统监控...

    springMVC+maven+hibernate框架

    springMVC+maven+hibernate框架,搭建的过程 可以参考 java进阶(五)------springMVC---springMVC+Hibernate+maven完整搭建运行步骤 http://blog.csdn.net/zzq900503/article/details/49892783

    Spring+SpringMVC+Hibernate框架集成详解

    Spring+SpringMVC+Hibernate 框架集成详解 本文档旨在详细介绍 Spring、SpringMVC 和 Hibernate 框架的集成,旨在帮助开发人员快速了解这三个框架的集成过程。 Spring 框架 Spring 框架是一个 Java 语言的开源...

    idea工具创建的Spring+SpringMVC+Hibernate+maven项目

    标题中的"idea工具创建的Spring+SpringMVC+Hibernate+maven项目"指的是使用IntelliJ IDEA这个集成开发环境(IDE)构建的一个Java Web项目,该项目整合了四个关键的技术框架:Spring、SpringMVC、Hibernate以及Maven...

    springmvc+hibernate+easyui

    在"springmvc+hibernate+easyui"项目中,可能使用EasyUI的DataGrid组件展示数据列表,结合SpringMVC的ModelAndView或Model对象,将后端查询结果转化为JSON格式,通过Ajax请求传递到前端,最终动态渲染数据列表。...

    SpringMVC+Hibernate+annotation+Easyui权限管理系统

    SpringMVC+Hibernate+annotation+Easyui权限管理系统,Session过期控制(包括Ajax请求的过期控制),访问权限控制(权限控制到按钮),hibernate数据库用户名与密码加密,其它的不说了,绝对物有所值

    基于注解的springMVC+hibernate+oracle包含数据库查询

    本文将详细解析基于注解的SpringMVC+Hibernate+Oracle数据库集成的实现过程及其核心知识点。 首先,SpringMVC是Spring框架的一部分,它是一个轻量级的MVC(Model-View-Controller)框架,用于处理HTTP请求和响应。...

    springmvc+hibernate+oracle基础框架

    《构建基于SpringMVC、Hibernate和Oracle的基础框架》 在现代企业级应用开发中,SpringMVC、Hibernate和Oracle的组合被广泛应用于构建强大的后端系统。这些技术各自扮演着关键角色,共同构成了一个高效、稳定的开发...

    新SSH(Spring+SpringMVC+Hibernate)框架结构的java web案例实践

    随着struts的安全问题的暴露,原由Struts2+spring+hibernate构成的SSH2已经被越来越多的开发者所弃用,反而,由Spring+SpringMVC+Hibernate构成的SSH框架越来越受欢迎!这里提供了一个案例代码,希望对大家搭建环境...

    Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA

    在现代Java Web开发中,"Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA"是一个常见的架构组合,被广泛应用于构建企业级应用程序。这个组合通常被称为"SSM",其中"M"代表Maven,"S"代表Spring,包括Spring核心...

    Maven搭建SpringMVC+Hibernate项目源码

    Maven搭建SpringMVC+Hibernate项目源码,包括Hibernate二级缓存Ehcache的搭建等等,博文地址:http://blog.csdn.net/fengshizty/article/details/43635305

    SpringMVC+HIbernate登录程序并进行增删改查

    SpringMVC+Hibernate登录程序并进行增删改查是一个经典的Web开发示例,它整合了两个重要的Java技术框架——SpringMVC和Hibernate,用于构建基于Java的动态网站应用。SpringMVC是Spring框架的一部分,主要负责处理...

    SpringMVC+Hibernate+Spring

    【SpringMVC+Hibernate+Spring】整合框架详解 在当今的Java Web开发中,SpringMVC、Hibernate和Spring是三个非常关键的组件,它们分别负责不同的职责:SpringMVC作为表现层框架,处理HTTP请求与响应;Hibernate作为...

    springmvc+hibernate完整项目

    这个"springmvc+hibernate完整项目"是一个整合了这两者的技术实践,提供了完整的部署和运行环境,对于初学者或者希望快速搭建后台服务的开发者来说非常实用。 首先,SpringMVC作为Spring框架的一部分,主要处理HTTP...

    Spring+SpringMVC+Hibernate+JSP框架搭建

    "Spring+SpringMVC+Hibernate+JSP框架搭建"就是一个经典的Java Web开发解决方案,它整合了四个核心组件来实现全面的后端功能和用户界面交互。下面我们将深入探讨这四个组件以及它们如何协同工作。 1. **Spring框架*...

    springMvc+hibernate Demo

    这个 "springMvc+hibernate Demo" 压缩包文件很可能是为了展示如何将这两个框架整合在一起,创建一个完整的数据驱动的 Web 应用。 **Spring MVC 框架** Spring MVC 是 Spring 框架的一部分,设计用于构建可维护、松...

    SpringMVC+Hibernate整合的登录Demo 导入即可直接运行适合初次接触整合

    SpringMVC和Hibernate是Java开发中常用的两个框架,SpringMVC负责处理Web应用中的Controller层,而Hibernate则是ORM(对象关系映射)框架,用于管理数据库操作。将这两个框架整合在一起,可以构建出高效且易于维护的...

    Spring+SpringMVC+Hibernate+Maven框架搭建及配置文件源码

    在"Spring+SpringMVC+Hibernate+Maven框架搭建及配置文件源码"这个项目中,开发者可能已经包含了这些框架的配置文件,例如Spring的applicationContext.xml、SpringMVC的servlet-context.xml、Hibernate的hibernate....

    spring3+springmvc+jpa+hibernate多数据源

    "spring3+springmvc+jpa+hibernate多数据源"是一个示例项目,它演示了如何在一个应用中集成Spring 3、Spring MVC、JPA 2.0以及Hibernate,以实现对多个数据源的支持。下面将详细介绍这些技术及其集成的关键点。 **...

    基于maven3+spring3+springMVC+hibernate3+mysql的图书管理小系统

    基于maven3+spring3+hibernate3+mysql的图书管理系统demo,主要为了练习在intellij上开发maven项目的体验和重新练习一下配置springmvc+hibernate+spring

Global site tag (gtag.js) - Google Analytics