框架整合____Spring整合hibernate
来源:互联网 发布:sd卡数据恢复手机版 编辑:程序博客网 时间:2024/06/18 14:25
备注:本人写博客就是想_让一些技术点和配置 遵循官方标准配置和更容易理解 为基础
以最精简例子入门,更容易的掌握一些东西.
整合结构图
1.添加spring支持配置spring的applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:frame_jdbc.properties</value></list></property></bean> <!-- 使用注解的方式装配置bean --><context:annotation-config /><context:component-scan base-package="com.frame"></context:component-scan><!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --><mvc:annotation-driven /><!-- 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" ><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:hibernate.cfg.xml"></property></bean><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /><tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> </beans>2.添加hibernate支持配置hibernate文件
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory><property name="format_sql">true</property><property name="show_sql">true</property><mapping resource="com/frame/student/bean/Student.hbm.xml" /></session-factory></hibernate-configuration>
//配置jdbc
################Oracle_JDBC################jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcljdbc.username=oraclejdbc.password=123456
//配置basedao
package com.frame.base.dao;import com.frame.student.bean.Student;public interface BaseDao {public void save(Student transientInstance);}//配置basedaoimpl
package com.frame.base.dao;import javax.annotation.Resource;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.transaction.annotation.Transactional;import com.frame.student.bean.Student;@Transactionalpublic class BaseDaoImpl implements BaseDao{private static final Logger log = LoggerFactory.getLogger(BaseDaoImpl.class);@Resourceprivate SessionFactory sessionFactory;private Session getCurrentSession() {return sessionFactory.getCurrentSession();}@Overridepublic void save(Student transientInstance) {log.debug("saving Student instance");try {getCurrentSession().save(transientInstance);log.debug("save successful");} catch (RuntimeException re) {re.printStackTrace();}}}//student实体类和映射文件
package com.frame.student.bean;/** * Student entity. @author MyEclipse Persistence Tools */public class Student implements java.io.Serializable {// Fieldsprivate String stuid;private String stuname;private String stutime;// Constructors/** default constructor */public Student() {}/** minimal constructor */public Student(String stuid) {this.stuid = stuid;}/** full constructor */public Student(String stuid, String stuname, String stutime) {this.stuid = stuid;this.stuname = stuname;this.stutime = stutime;}// Property accessorspublic String getStuid() {return this.stuid;}public void setStuid(String stuid) {this.stuid = stuid;}public String getStuname() {return this.stuname;}public void setStuname(String stuname) {this.stuname = stuname;}public String getStutime() {return this.stutime;}public void setStutime(String stutime) {this.stutime = stutime;}}
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.frame.student.bean.Student" table="STUDENT" schema="ORACLE"> <id name="stuid" type="java.lang.String"> <column name="STUID" length="32" /> <generator class="assigned" /> </id> <property name="stuname" type="java.lang.String"> <column name="STUNAME" length="32" /> </property> <property name="stutime" type="java.lang.String"> <column name="STUTIME" length="32" /> </property> </class></hibernate-mapping>//studentdao和impl
package com.frame.student.dao;import com.frame.base.dao.BaseDao;public interface StudentDao extends BaseDao{}
package com.frame.student.dao;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;import com.frame.base.dao.BaseDaoImpl;@Repositorypublic class StudentDaoImpl extends BaseDaoImpl implements StudentDao{}
//测试类
package com.frame.student.test;import org.junit.Before;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.frame.student.bean.Student;import com.frame.student.dao.StudentDao;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class Test {@AutowiredStudentDao dao;Student student; @Beforepublic void setUp() throws Exception { //做一些初始化操作 比如创建对象等 student=new Student(); student.setStuid("99"); student.setStuname("zhagnsan"); student.setStutime("2017-1-31");}@org.junit.Testpublic void insertStudent() {try {dao.save(student);} catch (Exception e) {e.printStackTrace();}}}
//源代码
http://pan.baidu.com/s/1hsvKafa
阅读全文
0 0
- 框架整合____Spring整合hibernate
- 框架整合____Spring整合Mybatis(主流,最精简整合方式,)
- 框架整合____Spring整合Struts2(主流整合方式,最精简整合方式)
- Spring整合Hibernate框架
- Hibernate框架整合
- springMVC+Hibernate 框架整合
- SpringMVC+Hibernate框架整合
- Spring整合hibernate框架
- ssh框架整合之hibernate整合
- SSH框架整合(Spring整合Struts2,和Spring整合Hibernate)
- Struts+Spring+Hibernate框架整合
- struts2+hibernate+spring框架整合
- SSH框架spring整合hibernate
- spring与hibernate框架整合
- Spring、Hibernate、Struts框架整合
- Spring+SpringMVC+Hibernate框架整合
- Spring+SpringMVC+Hibernate框架整合
- Spring+SpringMVC+Hibernate框架整合
- Maven设置将src/main/java下的xml编译到classpath(回应)
- Maven初识
- 已知中序、后序 序列重构二叉树
- HDU 6070 Dirt Ratio 线段树 二分
- Win7 手动开启 WiFi 共享
- 框架整合____Spring整合hibernate
- mfc 对话框 孙鑫 第七章
- C++迭代子类
- 判断一棵树是否是完全二叉树
- 1.2 解压可迭代对象赋值给多个变量
- 杭电暑期多校集训—Time To Get Up
- 动态规划——Compatible Numbers
- 无向图建立 邻接表(2)
- CentOS7如何设置防火墙