Maven 整合 SpringMvc Spring Hibernate +oracle

一、实习已经有大半年了 企业的项目都是搭建好的 自己也学习了去搭建一个 基于SSH的项目整合 其中查阅了很多资料 遇到一些问题也解决了。希望大家可以共同进步。

 感谢http://blog.csdn.net/js931178805/article/details/39642743这篇文章。


二、搭建maven项目 


(1)新建maven项目

 <1>我们选Maven Project

Maven 整合 SpringMvc Spring Hibernate +oracle_第1张图片

<2>选择maven-archetype-webapp 

Maven 整合 SpringMvc Spring Hibernate +oracle_第2张图片

<3> 填写项目信息

Maven 整合 SpringMvc Spring Hibernate +oracle_第3张图片

<4> 补全目录 右键--> bulid path 

 Maven 整合 SpringMvc Spring Hibernate +oracle_第4张图片


项目基本结构

Maven 整合 SpringMvc Spring Hibernate +oracle_第5张图片

三、集成jetty  主要在pom 文件 添加配置


pom.xml


  4.0.0
  com
  sms
  war
  0.0.1-SNAPSHOT
  sms Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
  
  
   	 
  		   
			    org.mortbay.jetty  
			    maven-jetty-plugin  
			    6.1.26  
			                   
			        3  
			          
			              
			                8090  
			              
			                             
			          
			              
			                src/main/webapp/WEB-INF  
			                  
			                    **/*.jsp  
			                  
			                  
			                    **/*.properties  
			                    **/*.xml  
			                  
			              
			          
			      
			
		
    sms
  


jetty:run

Maven 整合 SpringMvc Spring Hibernate +oracle_第6张图片



打开浏览器 访问 http://localhost:8090/sms/index.jsp




四、利用maven来下载所有的jar 

pom.xml 


  4.0.0
  com
  sms
  war
  0.0.1-SNAPSHOT
  sms Maven Webapp
  http://maven.apache.org
 
       
      
          
          
            maven  
            Maven Repository Switchboard  
            default  
            http://repo1.maven.org/maven2  
              
                false  
              
          
          
          
            alibaba-opensource  
            alibaba-opensource  
            http://code.alibabatech.com/mvn/releases/  
            default  
          
          
          
            alibaba-opensource-snapshot  
            alibaba-opensource-snapshot  
            http://code.alibabatech.com/mvn/snapshots/  
            default  
          
     
    
      
        UTF-8  
      
    
      
          
          
            junit  
            junit  
            4.11  
            test  
          
          
          
          
            javax.servlet  
            servlet-api  
            3.0-alpha-1  
            provided  
          
          
          
          
            log4j  
            log4j  
            1.2.17  
          
          
          
          
            javax.servlet  
            jstl  
            1.2  
          
          
            javax.servlet.jsp  
            jsp-api  
            2.2.1-b03  
            provided  
          
          
          
          
            commons-fileupload  
            commons-fileupload  
            1.2.2  
          
          
          
          
            com.alibaba  
            fastjson  
            1.1.26  
          
          
          
          
            org.codehaus.jackson  
            jackson-mapper-asl  
            1.9.11  
          
          

        
        
        
		com.oracle
		ojdbc14
		10.2.0.2.0
	
  
          
          
            com.alibaba  
            druid  
            0.2.9  
          
          
          
          
            org.aspectj  
            aspectjweaver  
            1.7.1  
          
          
          
          
            org.hibernate  
            hibernate-core  
            4.2.5.Final  
               
          
            org.hibernate  
            hibernate-entitymanager  
            4.2.5.Final  
          
          
            org.hibernate  
            hibernate-ehcache  
            4.2.5.Final  
          
          
            org.hibernate.javax.persistence  
            hibernate-jpa-2.0-api  
            1.0.1.Final  
          
          
          
          
            org.springframework  
            spring-webmvc  
            3.2.0.RELEASE  
          
          
          
          
            org.springframework  
            spring-core  
            3.2.0.RELEASE  
          
          
            org.springframework  
            spring-context  
            3.2.0.RELEASE  
          
          
            org.springframework  
            spring-jdbc  
            3.2.0.RELEASE  
               
          
            org.springframework  
            spring-beans  
            3.2.0.RELEASE  
          
          
            org.springframework  
            spring-web  
            3.2.0.RELEASE  
          
          
            org.springframework  
            spring-expression  
            3.2.0.RELEASE  
          
          
            org.springframework  
            spring-orm  
            3.2.0.RELEASE  
          
          
            org.springframework  
            spring-test  
            3.2.0.RELEASE  
          
          
      

  
 	 
  		   
			    org.mortbay.jetty  
			    maven-jetty-plugin  
			    6.1.26  
			                   
			        3  
			          
			              
			                8090  
			              
			                             
			          
			              
			                src/main/webapp/WEB-INF  
			                  
			                    **/*.jsp  
			                  
			                  
			                    **/*.properties  
			                    **/*.xml  
			                  
			              
			          
			      
			
		
    sms
  


五、文件配置 

配置文件都放在 src/main/resouces 下面


config.properties

#属性文件  
  
#Oracle 的配置  
hibernate.dialect=org.hibernate.dialect.OracleDialect
driverClassName=oracle.jdbc.driver.OracleDriver
validationQuery=SELECT 1 FROM DUAL
jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc_username=system
jdbc_password=Oracle123

 
#SQLServer 的配置  
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect  
#driverClassName=net.sourceforge.jtds.jdbc.Driver  
#validationQuery=SELECT 1  
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy  
#jdbc_username=  
#jdbc_password=  
  
#Derby 的配置  
#hibernate.dialect=org.hibernate.dialect.DerbyDialect  
#driverClassName=org.apache.derby.jdbc.EmbeddedDriver  
#validationQuery=SELECT 1  
#jdbc_url=jdbc:derby:sy;create=true  
#jdbc_username=  
#jdbc_password=  
  
#MySQL 的配置  
#hibernate.dialect=org.hibernate.dialect.MySQLDialect  
#driverClassName=com.mysql.jdbc.Driver  
#validationQuery=SELECT 1  
#jdbc_url=jdbc:mysql://localhost:3306/sshf?useUnicode=true&characterEncoding=utf-8  
#jdbc_username=root  
#jdbc_password=  

hibernate.properties

hibernate.hbm2ddl.auto=update 
hibernate.show_sql=true
hibernate.format_sql=true
sessionInfoName=sessionInfo  
uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid 
uploadDirectory=attached

log4j.properties

log4j.rootLogger=INFO,A1,R  
  
log4j.appender.A1=org.apache.log4j.ConsoleAppender  
log4j.appender.A1.Target=System.out  
log4j.appender.A1.layout=org.apache.log4j.PatternLayout  
log4j.appender.A1.layout.ConversionPattern=[%c]%m%n  
  
log4j.appender.R=org.apache.log4j.RollingFileAppender   
log4j.appender.R.File=sshf.log  
log4j.appender.R.MaxFileSize=10MB  
log4j.appender.R.Threshold=ALL  
log4j.appender.R.layout=org.apache.log4j.PatternLayout  
log4j.appender.R.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n  


spring-hibernate.xml

  
  
  
      
      
      
      
      
      
          
          
          
  
          
          
          
          
          
          
          
          
          
          
  
          
  
          
          
          
          
  
          
          
          
          
  
          
          
          
          
          
          
  
          
          
          
      
  
      
      
          
          
              
                ${hibernate.hbm2ddl.auto}  
                ${hibernate.dialect}  
                ${hibernate.show_sql}  
                ${hibernate.format_sql}  
              
          
  
          
          
              
                com.sms.model  
              
          
      
  
      
      
          
      
  
      
      
  
      
      
          
              
              
              
              
              
              
              
              
              
  
              
              
              
              
              
  
              
          
      
      
          
          
      
  
  

spring-mvc.xml


  
  
      
      
      
      
  
      
          
          
      
  

spring.xml


  
  
  
      
      
  
      
      
  

配置文件目录

Maven 整合 SpringMvc Spring Hibernate +oracle_第7张图片


web.xml 配置

  
  
      
      
      
        contextConfigLocation  
        classpath:spring.xml,classpath:spring-hibernate.xml  
      
      
      
        openSessionInViewFilter  
        org.springframework.orm.hibernate4.support.OpenSessionInViewFilter  
          
            singleSession  
            true  
          
      
      
      
        sms  
        org.springframework.web.servlet.DispatcherServlet  
          
            contextConfigLocation  
            classpath:spring-mvc.xml  
          
        1  
      
  
      
        sms  
        /  
      
  
  
      
      
        encodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
          
            encoding  
            UTF-8  
          
          
            forceEncoding  
            true  
          
      
      
      
        encodingFilter  
        /*  
      
  
  
      
        org.springframework.web.context.ContextLoaderListener  
      
  
      
        /index.jsp  
      
  


六、实现一个学生表的增删改查

      

新建 controller dao model  service  service.impl包

Maven 整合 SpringMvc Spring Hibernate +oracle_第8张图片


Student.java

package com.sms.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.GenericGenerator;

@javax.persistence.Entity
@javax.persistence.Table(name = "STUDENT")
@AccessType("field")
public class Student  implements Serializable{

private static final long serialVersionUID = 1L;

/**
 *
 */
@Id
@Column(name = "ID", length = 32)
@GeneratedValue(generator = "uuid")  
@GenericGenerator(name = "uuid", strategy = "uuid")  
private String id;
/**
 *
 */
@Column(name = "NAME", length = 10)
private String name;
/**
 *
 */
public String getId(){
	return this.id;
}
/**
 *
 *@param id
 */
public void setId(String id){
	this.id=id;
}

/**
 *
 */
public String getName(){
	return this.name;
}
/**
 *
 *@param name
 */
public void setName(String name){
	this.name=name;
}

}


StudentDao.java

package com.sms.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sms.model.Student;

@Repository
public class StudentDao {
	
	@Autowired
	 private SessionFactory sessionFactory;  
	
	/**
	 * 
	 * 获取studentList
	 */
	public List getstudentList(){
		
		Session session = sessionFactory.openSession();
		String hql = "from Student";      
		Query query = session.createQuery(hql);      
		List studentList = query.list(); 
		session.close();
		return studentList;
	}
	
	
	/**
	 * 
	 * 根据id删除学生
	 */
	public void deleteStudent(String sid){
		Session session = sessionFactory.openSession();
		String hql = "delete from Student where id = ? ";
		Query query =  session.createQuery(hql);
		query.setString(0, sid);
		query.executeUpdate();
		session.close();
		
	}
	
	/**
	 * 
	 * 保存学生
	 */
	@Transactional
	public void saveOrUpdate(Student student,String sid){
		
		if(!"".equals(sid)&&null!=sid){
			student.setId(sid);
		}
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(student);
		tx.commit();
	}
	
	/**
	 * 获取学生
	 */
	public Student getStudent(String sid){
		Session session = sessionFactory.openSession();
		return (Student) session.get(Student.class, sid);
	}
	
	
	
}


IStudentService.java


package com.sms.service.impl;

import java.util.List;

import com.sms.model.Student;

public interface IStudentService {
	
	public List getStudentList();
	
	public void deleteStudent(String sid);
	
	public void saveOrUpdateStudent(Student student,String sid);
	
	public Student getStudent(String sid);
	

}


StudentService.java


package com.sms.service;

import java.util.List;

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

import com.sms.dao.StudentDao;
import com.sms.model.Student;
import com.sms.service.impl.IStudentService;

@Repository 
public class StudentService implements IStudentService{
     
	@Autowired
	private StudentDao studentDao;
	
	@Override
	public List getStudentList() {
	
		
		return studentDao.getstudentList();
	}

	@Override
	public void deleteStudent(String sid) {
	
		studentDao.deleteStudent(sid);
	}

	@Override
	public Student getStudent(String sid) {
		
		return studentDao.getStudent(sid);
	}

	@Override
	public void saveOrUpdateStudent(Student student,String sid) {
	
		studentDao.saveOrUpdate(student,sid);
	}

}


StudentController.java

package com.sms.controller;

import java.util.List;

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

import com.sms.model.Student;
import com.sms.service.StudentService;

@Controller
public class StudentController {
	
	@Autowired
	private StudentService service;
	
	/**
	 * 获取学生列表
	 */
	@RequestMapping("/getstudentList.do")
	public String getStudentList(Model model){
		
		
		List list = service.getStudentList();
		model.addAttribute("slist", list);
		
		return "studentList";
	}
	
	/**
	 * 根据id删除学生
	 */
	@RequestMapping("/deleteStudent.do")
	public String deleteStudent(String sid){
		
		service.deleteStudent(sid);
		return "redirect:/getstudentList.do";
	}
	
	/**
	 * 
	 * 跳转到新增 编辑学生界面
	 */
	@RequestMapping("/addOreditStudent.do")
	public String toAddStudentPage(String sid,Model model){
		
		Student student = new Student();
		
		if(!"".equals(sid)&&null!=sid){
			student =  service.getStudent(sid);
		}else{
			student = null;
		}
		model.addAttribute("student", student);
		return "addStudentPage";
	}
	
	/**
	 *保存学生 
	 */
	@RequestMapping("/saveOrupdateStudent.do")
	public String saveOrUpdateStudent(Student student,String sid){
		
		service.saveOrUpdateStudent(student,sid);
		return "redirect:/getstudentList.do";
	}
}



七、前台界面


Maven 整合 SpringMvc Spring Hibernate +oracle_第9张图片


index.jsp

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




Insert title here



学生列表




studentList.jsp

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





Insert title here


新增
${slist.id} ${slist.name} 编辑 删除


addStudentPage.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


学生姓名:

八、完成


首页:

Maven 整合 SpringMvc Spring Hibernate +oracle_第10张图片


学生列表

Maven 整合 SpringMvc Spring Hibernate +oracle_第11张图片

删除


Maven 整合 SpringMvc Spring Hibernate +oracle_第12张图片

新增

Maven 整合 SpringMvc Spring Hibernate +oracle_第13张图片



编辑

Maven 整合 SpringMvc Spring Hibernate +oracle_第14张图片



九、刚到企业里实习 公司的框架都有很好的封装 很多配置文件都没有好好去看 希望大家多了解配置 不要一味的写代码 
















你可能感兴趣的:(Maven 整合 SpringMvc Spring Hibernate +oracle)