重拾springmvc+hibernate

阅读更多

多年不用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内容如下:



  SpringDemo
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
      org.springframework.web.util.Log4jConfigListener
  
  
      org.springframework.web.context.ContextLoaderListener
  
  
 
      contextConfigLocation
      /WEB-INF/applicationContext.xml
  
  
      MVC
      org.springframework.web.servlet.DispatcherServlet
      
          contextConfigLocation
          
              /WEB-INF/config/mvc*.*
          
      
      1
  
  
  
      MVC
      /
  

 

applicationContext.xml:


 


    

      

      
          
          
      

    
    

    
        
        
            
            com.alecchyi.demo.entry
            com.alecchyi.demo.dao
            com.alecchyi.demo.service
            
        
        
            
                update
                org.hibernate.dialect.DB2Dialect
                true
                ruby
            
        
    
    
        
        
        
        
    
 
    
        
    
    
    
	
		
			
			
			
		
	
	
		
		
	

    
        
    
    
        
    

 

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 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 update(HttpServletRequest request){
		String username = request.getParameter("name");
		System.out.println(userService.userCount());
		Map map = new HashMap(1);
		map.put("username", username);
		return map;
	}
}

 

view.jsp内容:

 

pom.xml 内容如下:(有些并不是必须的)


  4.0.0
  SpringDemo
  SpringDemo
  0.0.1-SNAPSHOT
  war
  Spring
  Spring
  
    src
    
      
        maven-compiler-plugin
        3.1
        
          1.7
          1.7
        
      
      
        maven-war-plugin
        2.4
        
          WebContent
          false
        
      
    
  
  
  	
  		org.springframework
  		spring-context
  		runtime
  	
  	
  		org.springframework
  		spring-webmvc
  	
  	
  		com.fasterxml.jackson.core
  		jackson-databind
  	
  	
  		com.fasterxml.jackson.core
  		jackson-core
  	
  	
  		com.fasterxml.jackson.core
  		jackson-annotations
  	
  	
  		org.hibernate
  		hibernate-core
  	
  	
  		mysql
  		mysql-connector-java
  	
  	
  		org.springframework.security
  		spring-security-config
  	
  	
  		org.springframework.security
  		spring-security-core
  	
  	
  		org.springframework.security
  		spring-security-web
  	
  	
  		org.springframework
  		spring-orm
  	
  	
  		org.aspectj
  		aspectjrt
  	
  	
  		org.aspectj
  		aspectjweaver
  	
  
  
  	
  		
  			org.springframework
  			spring-context
  			4.1.4.RELEASE
  		
  		
  			org.springframework
  			spring-webmvc
  			4.1.4.RELEASE
  		
  		
  			com.fasterxml.jackson.core
  			jackson-databind
  			2.5.1
  		
  		
  			com.fasterxml.jackson.core
  			jackson-core
  			2.5.1
  		
  		
  			com.fasterxml.jackson.core
  			jackson-annotations
  			2.5.1
  		
  		
  			org.hibernate
  			hibernate-core
  			4.3.5.Final
  		
  		
  			mysql
  			mysql-connector-java
  			5.1.29
  		
  		
  			org.springframework.security
  			spring-security-config
  			3.2.5.RELEASE
  		
  		
  			org.springframework.security
  			spring-security-core
  			3.2.5.RELEASE
  		
  		
  			org.springframework.security
  			spring-security-web
  			3.2.5.RELEASE
  		
  		
  			org.springframework
  			spring-jdbc
  			4.1.4.RELEASE
  		
  		
  			org.springframework
  			spring-orm
  			4.1.4.RELEASE
  		
  		
  			org.aspectj
  			aspectjweaver
  			1.7.4
  		
  		
  			org.aspectj
  			aspectjrt
  			1.7.4
  		
  	
  

 

 

 

你可能感兴趣的:(springmvc,hibernate,db2,json)