项目说明:
使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能
项目结构
1.导入依赖jar包
<!--单测-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<!--spring配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--aop使用的jar-->
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency> <!--SpringWeb-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.8.RELEASE</version>
</dependency> <!--JavaEE-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency> <!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> <!--hibernate jar包-->
<!--jta的jar包-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency> <!--Spring-ORM-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version> 4.2.2.RELEASE</version>
</dependency> <!--SpringWebMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.8.release</version>
</dependency> <!--Oracle驱动的jar-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
2.创建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"></context:component-scan>
<!--mvc注解驱动 作用:创建7个HttpMessaeingConvert-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源放行-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!--1.Datasource-->
<!--1.配置数据源c3p0-->
<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> <!--jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--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>
<!--with current thread bind session和线程绑定的session-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!--扫描小配置文件 所有的hbm文件-->
<property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>
<!--去扫描指定包下带有注解的类-->
<!--<property name="packagesToScan">
<list>
<value>cn.happy.bean</value>
</list>
</property>-->
</bean> <!--3.dao-->
<bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!--4.service-->
<bean id="deptService" class="cn.happy.service.DeptServiceImpl">
<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>
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <aop:config>
<!–配置了切点Pointcut–>
<aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
<!– 顾问–>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
</aop:config>-->
</beans>
3.搭建分层
4.创建实体类Dept
/**
* Created by Administrator on 2017/12/24.
* 部门表
*/
public class Dept {
private Integer deptno; //部门编号
private String dname; //部门名称
private String loc; //部门位置
}
5.创建映射文件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">
<!--实体 name=实体端内容 column=DB端内容-->
<class name="Dept" table="DEPT" dynamic-update="true">
<!--底层数据表对应的主键-->
<id name="deptno" column="DEPTNO">
<!--主键生成策略: assigned 程序员手动给值-->
<generator class="native"/>
</id>
<property name="dname" column="DNAME"></property>
<property name="loc" column="LOC"></property>
</class> </hibernate-mapping>
6.dao层
IDeptDAO
public interface IDeptDAO {
public int addDept(Dept dept);
}
DeptDAOImpl:
public class DeptDAOImpl implements IDeptDAO{
private SessionFactory sessionFactory;
public int addDept(Dept dept) {
Serializable count = sessionFactory.getCurrentSession().save(dept);
return (Integer)count;
} public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
service层
IDeptService
public interface IDeptService {
public int addDept(Dept dept);
}
DeptServiceImpl
public class DeptServiceImpl implements IDeptService {
private IDeptDAO dao;
@Transactional
public int addDept(Dept dept) {
return dao.addDept(dept);
} public IDeptDAO getDao() {
return dao;
} public void setDao(IDeptDAO dao) {
this.dao = dao;
}
}
Controller层
DeptController:
@Controller
public class DeptController { private IDeptService deptService;
@RequestMapping("/adddept")
public String addDept(Dept dept){
deptService.addDept(dept);
return "success";
}
}
add.jsp
执行后sql语句: