SSH整合(一)hibernate+spring

1.导入maven节点  <dependencies>

//测试用的
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>//aop相关
    <dependency>
      <groupId> org.aspectj</groupId >
      <artifactId> aspectjweaver</artifactId >
      <version> 1.8.7</version >
    </dependency>//mysql驱动
    <dependency>
      <groupId>org.wisdom-framework</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.34_1</version>
    </dependency>//c3p0数据源(选用)
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>//spring事务
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.3.RELEASE</version>
    </dependency>//sqring核心
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.0.RELEASE</version>
    </dependency>//hibernate核心
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.6.Final</version>
    </dependency>//spring和hibernate整合相关
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version> 4.2.2.RELEASE</version>
    </dependency>//测试
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>

  </dependencies>//用于解决idea读取不到非resource目录下配置文件问题
  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>
    </resources>
  </build>

2.建立分层

3.建立实体类Stuck

package cn.lhl.entity;

/**
 * Created by Administrator on 2017/4/2/002.
 *///股票类
public class Stuck {//id
    private int sid;//股票名
    private String sname;//持股数
    private int scount;
//相关geter,setter
    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;
    }

    public int getScount() {
        return scount;
    }

    public void setScount(int scount) {
        this.scount = scount;
    }
}

4.创建Stuck的映射文件Stuck.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.lhl.entity">
    <class name="Stuck">
        <id name="sid">
            <generator class="native"></generator>
        </id>
        <property name="scount"/>
        <property name="sname"/>
    </class>
</hibernate-mapping>

5.创建dao接口IStuckDao和实现类StuckDaoImpl

package cn.lhl.dao;

import cn.lhl.entity.Stuck;

/**
 * Created by Administrator on 2017/4/4/004.
 */
public interface IStuckDao {
    int addStuck(Stuck stuck);
}
package cn.lhl.dao.impl;

import cn.lhl.dao.IStuckDao;
import cn.lhl.entity.Stuck;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/4/4/004.
 */
public class StuckDaoImpl implements IStuckDao {//创建session工厂
    private SessionFactory sessionFactory;//事务注解(执行该方法时打开事务)
    @Transactional//添加股票的方法
    public int addStuck(Stuck stuck) {
        Session session = sessionFactory.getCurrentSession();
        Serializable save = session.save(stuck);
        return (Integer)save;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

6.创建service层接口IStuckService和实现类StuckServiceImpl

package cn.lhl.service;

import cn.lhl.entity.Stuck;

/**
 * Created by Administrator on 2017/4/4/004.
 */
public interface IStuckService {
    int addStuck(Stuck stuck);
}
package cn.lhl.service.impl;

import cn.lhl.dao.IStuckDao;
import cn.lhl.entity.Stuck;
import cn.lhl.service.IStuckService;

/**
 * Created by Administrator on 2017/4/4/004.
 */
public class StuckServiceImpl implements IStuckService {
    private IStuckDao iStuckDao;
    public int addStuck(Stuck stuck) {
        return iStuckDao.addStuck(stuck);
    }

    public IStuckDao getiStuckDao() {
        return iStuckDao;
    }

    public void setiStuckDao(IStuckDao iStuckDao) {
        this.iStuckDao = iStuckDao;
    }
}
7.创建spring配置文件applicationContext.xml<?xml version="1.0" encoding="UTF-8"?<beans xmlns="http://www.springframework.org/schema/beans"

<?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:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <!--配置数据源-->    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql:///gupiao" p:username="root" p:password="qq1684547081">    </bean>    <!--配置sessionFactory-->    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">        <!--引入数据源-->        <property name="dataSource" ref="dataSource"></property>        <!--设置hibernate相关配置-->        <property name="hibernateProperties">            <props>                <!--sql方言-->                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>                <!--是否显示sql-->                <prop key="hibernate.show_sql">true</prop>                <!--设置于线程绑定的session-->                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>                <!--设置自动更新表结构-->                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>        <!--扫描hibernate小配置-->        <property name="mappingDirectoryLocations" value="classpath:cn/lhl/entity"></property>    </bean>    <!--dao代理bean-->    <bean name="stuckDaoImpl" class="cn.lhl.dao.impl.StuckDaoImpl">        <!--植入sessionFactory-->        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--service代理bean-->    <bean name="stuckService" class="cn.lhl.service.impl.StuckServiceImpl">        <!--植入dao-->        <property name="iStuckDao" ref="stuckDaoImpl"></property>    </bean>    <!--设置事务管理器-->    <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">        <!--引入sessionFactory-->        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--配置注解事务扫描器-->    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>

8.创建测试类

package cn.lhl.test;

import cn.lhl.entity.Stuck;
import cn.lhl.service.impl.StuckServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2017/4/4/004.
 */
public class SSHTest {
    @Test
    public void add(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        StuckServiceImpl  stuckService=(StuckServiceImpl) context.getBean("stuckService");
        Stuck stuck=new Stuck();
        stuck.setSname("龙骨");
        stuck.setSname("1");
        stuckService.addStuck(stuck);
    }
}

整合完成

时间: 2024-10-13 07:06:22

SSH整合(一)hibernate+spring的相关文章

ssh整合之三hibernate和spring整合

1.拷贝我们的spring事务控制所需的jar包 2.在spring容器中配置我们的hibernateTemplate以及事务管理器 <?xml version="1.0" encoding="UTF-8"?> <!-- spring的配置文件:导入约束 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt

SSH整合(Struts2+Spring+Hibernate)

为了方便本次使用的是Myeclipse8.5 一.创建web工程,搭建Struts框架开发环境: 步骤1::导入struts框架所需的jar包 步骤2:在web.xml中配置struts2.0主过滤器 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" x

重新学习之spring第四个程序,整合struts2+hibernate+spring

第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4) 第二步:编写web.xml 和struts.xml和applicationContext.xml和applicationContext-service.xml和application-actionContext.xml和applicationContext-dao.xml web.xml 1 <?xml version="1.0" encoding="UTF

struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决

最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myeclipse进行整合的,本来也没那么多问题,看视频吧居然好多要手打,我不喜欢看不下去放弃了,教程把就是一堆坑,最最让人不解的是明明有一个冲突是需要解决的,但我看到的教程居然都没有提到,还有一个错误居然好多人都好像自动忽略一样,能解决我问题的都是要漫长的找,所以我一定一定要把这个过程记录下来,给第一次搞

SSH三大框架的搭建整合(struts2+spring+hibernate)(转)

原文地址:http://blog.csdn.net/kyle0349/article/details/51751913  尊重原创,请访问原文地址 SSH说的上是javaweb经典框架,不能说100%要会SSH框架,但是大部分公司都在用,说到框架,都会提到ssh吧,这次就以很简单的注册例子来整合SSH框架.整合框架要注意的是先每个框架单独测通后再整合,不然整合后出现问题比较难排查. 环境:windows + MyEclipse + JDK1.7 + Tomcat7 + MySQL 代码已经测通,

Struts2+Hibernate+Spring(SSH)三大框架整合jar包

Struts2 + Spring3 + Hibernate3 框架整合 1. 每个框架使用 (开发环境搭建 )* 表现层框架 struts2 1) jar包导入: apps/struts2_blank.war 包含struts2 开发最基本的jar包 struts2-convention-plugin-2.3.7.jar用于struts使用注解 (如果不使用注解开发,无需导入) struts2-json-plugin-2.3.7.jar 用于struts2整合Ajax struts2-sprin

Struts2 convention插件试用+ Spring+Hibernate SSH整合

第一步,引入struts2-convention-plugin-2.2.1.jar 然后,改动配置文件. 我是在struts.properties文件里改动的: struts.objectFactory = spring struts.devMode = true struts.i18n.encoding = UTF-8 struts.convention.result.path =/WEB-INF/jsp/ struts.convention.package.locators = action

ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点

Web层用Structs2的action Service层用Spring的IoC和aop以及JdbcTemplate或者Transaction事务(创建对象及维护对象间的关系) Dao层用Hibernate的crude操作 看上去是Structs2和Spring(把Structs2的action交给Spring整合,action处理表单,通常都是多实例用Spring <bean id="" class="" scope="prototype"

工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

上文中我们介绍<工作笔记2.软件开发常用工具> 从今天开始本文将教大家如何进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个独立配置:struts2. Hibernate. Spring 2)2个整合:整合Sring和struts2. 整合Spring和Hibernate 3)资源分类 开发包.软件.框架源码,已经共享到百度网盘:http://pan.baidu.com/s/1o6FkbA6 一.3个独立配置 1.Struts2: 1

Spring(八)SSH整合简述

一.Spring与Struts2的整合 1.1.整合步骤 1.2.配置web.xml 1.3.配置spring配置文件applicationContext.xml 1.4.配置struts配置文件 1.5.Action继承ActionSupport类 二.spring与hibernate整合 2.1.步骤 2.2.注入sessionFactory <--数据源--> <bean id="dataSource" class="org.springframewo