Spring+SpringMVC+Hibernate整合注解版
来源:互联网 发布:啾米网络是培训机构吗? 编辑:程序博客网 时间:2024/06/05 05:40
这次是Spring+SpringMVC+Hibernate整合的注解版
结果目录如下:
bean层:
Student:
package cn.happy.bean;import javax.persistence.*;/** * Created by lenovo on 2017/10/13. */@Entity@Table(name = "studentnew")public class Student { @Id @GeneratedValue private int sid; @Column private String sname; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; }}
controller层:
StudentCTR:
package cn.happy.controller;import cn.happy.bean.Student;import cn.happy.service.StudentService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.annotation.Resource;/** * Created by lenovo on 2017/10/13. */@Controllerpublic class StudentCTR { @Resource(name = "studentService") private StudentService studentService; @RequestMapping("/add") public String add(Student student){ studentService.add(student); return "index"; }}
dao层:
StudentDao:
package cn.happy.dao;import cn.happy.bean.Student;import org.hibernate.Session;/** * Created by lenovo on 2017/10/13. */public interface StudentDao { public int add(Student student);}
StudentDaoImpl:
package cn.happy.dao;import cn.happy.bean.Student;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.stereotype.Repository;import javax.annotation.Resource;import java.io.Serializable;/** * Created by lenovo on 2017/10/13. */@Repository("studentdao")public class StudnetDaoImpl implements StudentDao {@Resourceprivate SessionFactory sessionFactory; public int add(Student student) { Serializable s=sessionFactory.getCurrentSession().save(student); return (Integer) s; }}
service:
StudentService:
package cn.happy.service;import cn.happy.bean.Student;/** * Created by lenovo on 2017/10/13. */public interface StudentService { public int add(Student student);}
StudentServiceImpl:
package cn.happy.service;import cn.happy.dao.StudentDao;import cn.happy.bean.Student;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/** * Created by lenovo on 2017/10/13. */@Service("studentService")public class StudentServiceImpl implements StudentService { @Resource(name = "studentdao") private StudentDao dao; @Transactional public int add(Student student) { int result=dao.add(student); return result; } public StudentDao getDao() { return dao; } public void setDao(StudentDao dao) { this.dao = dao; }}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> <!--视图解析器--> <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="hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</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/bean"></property>--> <property name="packagesToScan" > <list> <value>cn.happy.bean</value> </list> </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>
其他和之前没什么不同,就省略了。
阅读全文
0 0
- Spring+SpringMVC+Hibernate整合非注解版
- Spring+SpringMVC+Hibernate整合注解版
- SSH整合(Spring+SpringMVC+Hibernate)注解版
- springmvc+spring+hibernate 整合 annotation(全注解)方式
- Spring+Hibernate+SpringMVC整合配置(注解式)
- spring hibernate 整合 注解
- hibernate注解整合spring
- spring和springMVC整合注解版helloworld
- Spring SpringMVC Hibernate 注解
- SpringMVC+Spring+Hibernate整合
- SpringMVC整合Spring、Hibernate
- Spring-SpringMVC-Hibernate整合
- SpringMVC+Spring+Hibernate整合
- SpringMVC+Spring+hibernate整合
- springmvc-spring-hibernate整合
- Spring+SpringMVC+Hibernate整合
- spring+springMVC+hibernate整合
- SpringMVC+Hibernate+Spring整合
- jmeter应用---创建测试计划
- div中英文无法自动换行的解决办法
- Redis 讲解系列之 Redis的主从复制(二)
- 登录检验用户名密码是否存在的源码
- Charles配置使用
- Spring+SpringMVC+Hibernate整合注解版
- [NOIP2017模拟]鱼群分裂
- 对UX设计5年内发展的5项预测
- 测井录井
- 对 left join 多个表的join 和 on 的分析Demo
- Unity如何实现网络通讯(三):观察者模式以及在网络模块的应用
- C#中的原子操作Interlocked你真的了解吗?
- (三) ElasticSearch基础知识
- C#--Winform项目核心--考勤模块(二)