spring、spring mvc、hibernate框架集成(ssh搭建,简单实例)

spring、spring mvc、hibernate三大框架集成

开发工具:Eclipse
jdk版本:1.8
tomcat:8.5
前段框架:bootstrap
项目结构:
spring、spring mvc、hibernate框架集成(ssh搭建,简单实例)_第1张图片
第一步:pom.xml 文件导包
各种包可以去maven公共库


  4.0.0

  com.hxzy
  ssh
  0.0.1-SNAPSHOT
  jar

  ssh
  http://maven.apache.org

  
    UTF-8
    
    4.0.0.RELEASE
  

  
	
	
    	org.slf4j
    	slf4j-api
    	1.7.25
	
	
	
    	org.slf4j
    	slf4j-log4j12
    	1.7.25
    	test
	
	
	
	
    	javax.servlet
    	javax.servlet-api
   	 	3.0.1
    	provided
	
	
	
    	javax.servlet
    	jstl
    	1.2
	
	
	
	
    	com.oracle
    	ojdbc6
    	11.2.0.1
	
	
	
    	org.springframework
    	spring-jdbc
    	4.1.0.RELEASE
	
	
	
    	commons-dbcp
    	commons-dbcp
    	1.4
	
	
	
	
    	org.hibernate
    	hibernate-core
    	3.3.2.GA
	
	
	
    	org.springframework
    	spring-hibernate
    	1.2.9
	
	
	
    	org.javassist
    	javassist
    	3.22.0-GA
	
	
	
	
    	org.springframework
    	spring-webmvc
    	${spring-version}
	
	
	
    	org.springframework
    	spring-web
    	${spring-version}
	
	
	
	
    	org.springframework
   	 	spring-context
    	${spring-version}
	
	
	
    	org.springframework
   	 	spring-tx
    	4.1.0.RELEASE
	
	
	
    	org.springframework
    	spring-beans
    	${spring-version}
	
	
	
    	org.springframework
    	spring-aop
    	${spring-version}
	
	
	
    	org.aspectj
    	aspectjweaver
    	1.8.14
	
  


第二步:配置spring—mvc.xml文件:



    
	
	
	
		
	
   	 	
        
	

第三步:定义实体类,写java分层代码,运用aop面向切面注入对象,映射关系文件配置。
1、我们定义一个双向一对多的映射关系,一个作者对应多本书籍,多本书籍同样可以对应一个作者。

/**
 * *作者实体类
 * @author Administrator
 *
 */
public class Author {
	private int aid;
	private String name;
	private String password;
	private Set books = new HashSet();//保存书籍的集合
	此处省略get()和set()方法...
/**
 * *书籍实体类
 * @author Administrator
 *
 */
public class Book {
	private int bid;
	private String bookname;
	private Author author;//保存作者对象
	同样省略get()和set()方法

Author.xml文件:




	
		
			
				
					seq_author_id
				
			
		
		
		
		
			
			
		
	

Book.xml文件:




	
		
			
				
					seq_book_id
				
			
		
		
		
			
		
	

AuthorDao:

public interface AuthorDao {
	public Author getAuthorByName(Author author);//通过名字查找作者
}

AuthorDaoImpl:

@Component("authorDaoImpl")//把authorDaoImpl实例化到spring容器中
public class AuthorDaoImpl implements AuthorDao{
	
	private SessionFactory sessionFactory;
	@Resource//装配bean,默认取属性名进行装配
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public Author getAuthorByName(Author author) {
		// TODO Auto-generated method stub
		Session session = null;
		Author a = null;
		try {
			session = sessionFactory.openSession();
			String hql = "from Author where name =:name and password =:password";
			Query query = session.createQuery(hql);
			query.setString("name", author.getName());
			query.setString("password", author.getPassword());
			a = (Author) query.uniqueResult();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if(session!=null) {
				session.close();
			}
		}
		return a;
	}
	
}

AuthorService:

public interface AuthorService {
	public Author getAuthorByName(Author author);
}

AuthorServiceImpl:

@Component("authorServiceImpl")//把authorServiceImpl实例化到spring容器中
public class AuthorServiceImpl implements AuthorService{
	private AuthorDao authorDao;
	
	@Resource(name="authorDaoImpl")
	public void setAuthorDao(AuthorDao authorDao) {
		this.authorDao = authorDao;
	}


	@Override
	public Author getAuthorByName(Author author) {
		// TODO Auto-generated method stub
		return authorDao.getAuthorByName(author);
	}

}

LoginController:

@Controller
public class LoginController {
	private AuthorService authorService;
	@Resource(name="authorServiceImpl")//将authorServiceImpl实例化到spring容器中
	public void setAuthorService(AuthorService authorService) {
		this.authorService = authorService;
	}
	@RequestMapping(method=RequestMethod.GET,value="index")//定义一个请求路径
	public String index() {
		return "login";	
	}
	@RequestMapping(method=RequestMethod.POST,value="login")
	public ModelAndView AuthorLogin(@ModelAttribute ModelAndView mav,Author author) {
		Author a1 = authorService.getAuthorByName(author);
		if(a1!=null) {
			mav.addObject(a1);
			mav.setViewName("list");
		}else {
			mav.setViewName("redirect:index");
		}
		return mav;	
	}
}

第四步:配置spring—hibernacle.xml 文件:
1、先导入jdbc和log4j的properties文件
jdbc:

jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username = scott
jdbc.password = sa

log4j:

log4j.rootLogger=info,a,b

log4j.appender.a=org.apache.log4j.ConsoleAppender
log4j.appender.a.Target=System.err
log4j.appender.a.layout=org.apache.log4j.SimpleLayout


log4j.appender.b = org.apache.log4j.FileAppender
log4j.appender.b.File = E:/gz.log
log4j.appender.b.layout =  org.apache.log4j.PatternLayout
log4j.appender.b.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n

spring—hibernacle.xml:



    
	
	
	
	
		
			
				classpath:jdbc.properties
			
		
	
	
	
		
		
		
		
	
	
	
		
		
			
				classpath:hbm/*.hbm.xml
			
		
		
			
				hibernate.dialect=org.hibernate.dialect.OracleDialect
				hibernate.hbm2ddl.auto = update
				hibernate.show_sql =true
			
		
	
	
	
	
        
    
    
    
    	
    		
    		
    		
    		
    	
    
    
    
    
        
        
    


第五步:配置web.xml文件:



  ssh
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
 
 	dispatcherServlet
 	org.springframework.web.servlet.DispatcherServlet
 	
 		contextConfigLocation
 		classpath*:spring-mvc.xml
 	
 
 
 	dispatcherServlet
 	*.html
 
 
 
 
 	org.springframework.web.context.ContextLoaderListener
 
 
 	contextConfigLocation
 	classpath*:spring-*.xml
 

jsp页面:
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Insert title here


	


list.jsp

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






Insert title here`在这里插入代码片`



	
${b.bookname}

login.jsp:

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







Insert title here


	

你可能感兴趣的:(ssh,spirng,mvc,spring,hibernacle,ssh)