如何基于sping、springmvc、hibernate三大框架写一个maven项目

最近写了一个基于sping、springmvc、hibernate三大框架的maven项目,讲解一下框架的搭建,文件配置等,希望能帮助到大家。

1.新建一个maven项目类型是war包(项目比较小,这里就不采用打jar包的分模块形式开发)

2.导入项目所需包的依赖到pom.xml文件


  	3.0
  		4.2.4.RELEASE
		5.0.7.Final
		1.0.9
		2.4.2
  
  	
	
		
			
				org.springframework
				spring-context
				${spring.version}
			
			
				org.springframework
				spring-aspects
				${spring.version}
			
			
				org.springframework
				spring-orm
				${spring.version}
			
			
				org.springframework
				spring-test
				${spring.version}
			
			
				org.springframework
				spring-web
				${spring.version}
			
			
			
				org.hibernate
				hibernate-core
				${hibernate.version}
			
			 
			   org.springframework 
			   spring-aspects 
			   ${spring.version} 
			
			
		
	
	
	
		
		
			org.springframework
			spring-context
		
		
			org.springframework
			spring-aspects
		
		
			org.springframework
			spring-orm
		
		
			org.springframework
			spring-test
		
		
			org.springframework
			spring-web
		
		
		
			org.hibernate
			hibernate-core
		

		
		
			mysql
			mysql-connector-java
			5.1.6
			runtime
		
		
			
				com.alibaba
				druid
				${druid.version}
		
	
			
				com.fasterxml.jackson.core
				jackson-databind
				${jackson.version}
			
			  
  				net.sf.json-lib  
 				 json-lib  
  				2.4  
  				jdk15  
			  
			  
   				 com.google.code.gson  
   				 gson  
    			2.3.1  
  
		
		
			javax.servlet
			servlet-api
			2.5
			provided
		
		
			javax.servlet
			jsp-api
			2.0
			provided
		
		
		
	    
	      org.springframework
	      spring-web
	      
	    
	    
	      org.springframework
	      spring-webmvc
	      ${spring.version}
	    
		
		
		
			org.slf4j
			slf4j-log4j12
			1.7.2
		
		
		
			junit
			junit
			4.9
			test
		
		
		
			javax.servlet
			jstl
			1.2
		
	

	
		
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
					UTF-8
				
			

		   
                
                org.apache.tomcat.maven    
                tomcat7-maven-plugin    
                  
	                UTF-8  
            	
                
          

		
	

3.配置文件

    3.1 db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///pickmeup?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

    3.2 log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    3.3 applicationContext.xml



		
		
		
		
	
	
	
	
	
		
		
		
		
		
		
	
		
	
	
	
	
	
	
	
	
	
		
		
			
				true
				true
				update
				org.hibernate.dialect.MySQLDialect
			
		
		
		 
			
				pickmeup.entity
			
		
	   

    3.4 springmvc.xml




	
		
	
		
		
	

4.下面来写个测试例子

4.1 entity

package pickmeup.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User {
	private String u_id;
	private String u_name;
	private String u_phone;
	private int u_orderState=0;
	
	

	@Id
	@Column(name = "u_id", nullable = false, unique = true,length = 32)
	public String getU_id() {
		return u_id;
	}
	
	public void setU_id(String u_id) {
		this.u_id = u_id;
	}
	@Column(name = "u_name", nullable = true, length = 32)
	public String getU_name() {
		return u_name;
	}
	public void setU_name(String u_name) {
		this.u_name = u_name;
	}
	@Column(name = "u_phone", nullable = true, length = 32)
	public String getU_phone() {
		return u_phone;
	}
	public void setU_phone(String u_phone) {
		this.u_phone = u_phone;
	}
	@Column(name = "u_orderState", nullable = false, length = 1)
	public int getU_orderState() {
		return u_orderState;
	}
	public void setU_orderState(int u_orderState) {
		this.u_orderState = u_orderState;
	}
	
}

4.2 Dao&DaoImpl

package pickmeup.dao;

import java.util.List;
import pickmeup.entity.User;

public interface UserDao {
	/**
	 * 添加用户
	 * @param user
	 */
	public void addUser(User user);
}

package pickmeup.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import pickmeup.entity.User;

@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao   {
	
	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {  
        super.setSessionFactory(sessionFactory);  
    }
	
	@Resource
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.getCurrentSession();
		@Override
	public void addUser(User user) {
		this.getSession().save(user);
	}
}

4.3 Service&ServiceImpl

package pickmeup.service;

import java.util.List;

import pickmeup.entity.User;

public interface UserService {
	public void addUser(User user);
}
package pickmeup.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pickmeup.dao.UserDao;
import pickmeup.entity.User;
import pickmeup.service.UserService;

@Service
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDaoImpl;

	@Override
	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	public void addUser(User user) {
		this.userDaoImpl.addUser(user);
	}
}

4.4 Controller

package pickmeup.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;
import pickmeup.entity.User;
import pickmeup.service.UserService;
@Controller
@RequestMapping(value="/user")
public class UserController {
	
	@Autowired
	private UserService userServiceImpl;
	@RequestMapping(value="/addUser" ,method=RequestMethod.GET)
	@ResponseBody
	public String addUser(){
		User user=new User();
		user.setU_id("abc01");
		user.setU_name("裕少");
		user.setU_phone("123456");
		userServiceImpl.addUser(user);
		return "添加成功";
	}
}

以上便是三大框架的整合,非常适合新手去熟悉搭建三大框架,谢谢大家。

你可能感兴趣的:(如何基于sping、springmvc、hibernate三大框架写一个maven项目)