Spring,SpringMVC和hibernate整合小demo
2017-10-14 18:42
344 查看
DeptController
@Controller
public class DeptController {
//植入service
@Resource
private IDeptService deptService;
@RequestMapping("/addDept")
public String add(Dept dept){
deptService.addDept(dept);
return "index";
}
}
IDeptDAO
public interface IDeptDAO {
//添加部门的方法
public int addDept(Dept dept, Session session);
}
DeptDAOImpl
public class DeptDAOImpl implements IDeptDAO{
public int addDept(Dept dept,Session session) {
Serializable count = session.save(dept);
return (Integer) count;
}
}
Dept持久类
public class Dept {
private Integer deptno;
private String deptname;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
}Dept.hbm.xml
public interface IDeptService {
public int addDept(Dept dept);
}
DeptServiceImpl
public interface IDeptService {
public int addDept(Dept dept);
}HibernateUtil工具类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--包扫描器-->
<context:component-scan base-package="cn.happy.controller"/>
<!--mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.SessionFactory 类:Local-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!--hibernate.xxxxxx必须以hibernate-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<!--with current thread bind session-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>
</bean>
<!--dao-->
<bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl"/>
<!--service-->
<bean id="deptService" class="cn.happy.service.DeptServiceImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="dao" ref="deptDAO"></property>
</bean>
<!-- 5.事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--6.事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>jdbc.properties
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=wcj
jdbc.password=cj663300
log4j.properties
zlog4j.rootLogger=info,Console,R
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
#log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.Console.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/tomcat.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss} %5p %c{1}(%L):? %m%n
log4j.logger.org.apache=info,R
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
log4j.logger.org.apache.catalina.core=info,R
log4j.logger.org.apache.catalina.session=info,R add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加部门</title>
</head>
<body>
<form method="post" action="/addDept">
部门名称: <input name="deptname"/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
@Controller
public class DeptController {
//植入service
@Resource
private IDeptService deptService;
@RequestMapping("/addDept")
public String add(Dept dept){
deptService.addDept(dept);
return "index";
}
}
IDeptDAO
public interface IDeptDAO {
//添加部门的方法
public int addDept(Dept dept, Session session);
}
DeptDAOImpl
public class DeptDAOImpl implements IDeptDAO{
public int addDept(Dept dept,Session session) {
Serializable count = session.save(dept);
return (Integer) count;
}
}
Dept持久类
public class Dept {
private Integer deptno;
private String deptname;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
}Dept.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity"> <class name="Dept" table="Dept"> <id name="deptno" column="deptno"> <generator class="native"></generator> </id> <property name="deptname" /> </class> </hibernate-mapping>IDeptService
public interface IDeptService {
public int addDept(Dept dept);
}
DeptServiceImpl
public interface IDeptService {
public int addDept(Dept dept);
}HibernateUtil工具类
public class HibernateUtil { private static ThreadLocal<Session> tl; private static Configuration cfg; private static SessionFactory factory; static { tl=new ThreadLocal<Session>(); cfg=new Configuration().configure(); factory=cfg.buildSessionFactory(); } //获取和当前线程绑定的session public static Session getSession(){ //尝试着从线程中看看有没有线程变量 Session session = tl.get(); if (session==null){ //线程中没有session对象,创建一个 session = factory.openSession(); tl.set(session); } return session; //没有和当前线程绑定 } public static void closeSession(){ Session session = tl.get(); tl.set(null); session.close(); } }applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--包扫描器-->
<context:component-scan base-package="cn.happy.controller"/>
<!--mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.SessionFactory 类:Local-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!--hibernate.xxxxxx必须以hibernate-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<!--with current thread bind session-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>
</bean>
<!--dao-->
<bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl"/>
<!--service-->
<bean id="deptService" class="cn.happy.service.DeptServiceImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="dao" ref="deptDAO"></property>
</bean>
<!-- 5.事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--6.事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>jdbc.properties
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=wcj
jdbc.password=cj663300
log4j.properties
zlog4j.rootLogger=info,Console,R
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
#log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.Console.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/tomcat.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss} %5p %c{1}(%L):? %m%n
log4j.logger.org.apache=info,R
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
log4j.logger.org.apache.catalina.core=info,R
log4j.logger.org.apache.catalina.session=info,R add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加部门</title>
</head>
<body>
<form method="post" action="/addDept">
部门名称: <input name="deptname"/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
相关文章推荐
- springMVC,spring和Hibernate整合
- ssh(spring springMVC hibernate)的整合
- springmvc,spring,hibernate整合-分离配置参数
- Java 常用框架(Spring,SpringMVC,Hibernate,mybatis)整合
- Spring+JPA+Hibernate+Junit Test + maven Demo 整合配置
- SpringMVC-Spring-Hibernate项目搭建之三-- freemarker & 静态资源整合
- Spring、SpringMVC、Hibernate详细整合实例,包含所有步骤
- SpringMVC-Spring-Hibernate项目搭建之一-- 搭建maven 项目 & servlet的demo
- spring-springmvc-hibernate整合之配置文件
- 整合 SpringMVC 5.0 SpringDataJPA 2.0 Hibernate 5
- springmvc、jpa、spring、mongodb和ehcache整合框架demo
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- springMVC整合spring和hibernate4(适合于框架的搭建)
- SSH框架——整合Demo(Struts、Spring、Hibernate)
- Spring Struts2 Hibernate - - 注解版 demo整合
- SpringMVC、Spring、Hibernate整合基础配置
- 03SpringMVC,Spring,Hibernate整合(Date时间转换)
- SpringMVC与Spring、Hibernate整合
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- 学习整合hibernate springmvc spring的 心得(3)