SSH+Oracle的整合(SSH与Oracle整合坑巨多)

Posted 话森林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH+Oracle的整合(SSH与Oracle整合坑巨多)相关的知识,希望对你有一定的参考价值。

这里用SSH+Oracle做一个仓库管理系统,其中包含了查询、条件查询、删除、批量删除、修改、添加的功能。值得注意的是,Oracle与其它数据库存在很多不同之处,所以SSH和Oracle整合的时候小细节处理稍有不慎,就要花费大量时间去排查,亲身体会!

下面进入正文 ↓↓↓↓

Oracle数据库:

goods.sql

注意:创建表的时候,无论表名大写小写,创建完成时都会默认设置成大写。

--创建仓库表
create table goods
(
       Gno number not null primary key, --编号
       gname varchar2(500) not null,  --物质名称
       gtype varchar2(20) not null,   --物质类型
       Gnumber number not null check (Gnumber > 0),       --物质数量
       Gcompany varchar2(500) not null, --生产商
       Gcreatetime date default sysdate,       --生产日期
       Uintime date default sysdate   --入库时间
);
drop table goods;
--创建自增序列
create sequence goods_id
increment by 1  --增量为1
start with 1    --从1开始生成序列
nomaxvalue;      --没有最大值

--插入数据
insert into goods(Gno,gname,gtype,gnumber,gcompany) values(goods_id.nextval,\'康师傅方便面\',\'食品\',\'150000\',\'康师傅食品有限公司\');
insert into goods(Gno,gname,gtype,gnumber,gcompany) values(goods_id.nextval,\'康师傅矿泉水\',\'饮用水\',\'150000\',\'康师傅食品有限公司\');
insert into goods(Gno,gname,gtype,gnumber,gcompany) values(goods_id.nextval,\'康师傅辣条\',\'食品\',\'150000\',\'康师傅食品有限公司\');
insert into goods(Gno,gname,gtype,gnumber,gcompany) values(goods_id.nextval,\'康师傅可乐\',\'食品\',\'150000\',\'康师傅食品有限公司\');
insert into goods(Gno,gname,gtype,gnumber,gcompany) values(goods_id.nextval,\'康师傅面包\',\'食品\',\'150000\',\'康师傅食品有限公司\');

select * from goods;

select * from goods order by gcreatetime desc;

commit;

 

SSH:

包结构:

 

idea中数据库插件配置:

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hsl</groupId>
    <artifactId>Goods</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!--spring-->
        <!-- 这个依赖可以让spring容器有bean管理,依赖注入,基本的切面配置功能,但不支持切点表达式以及其解析-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- 让spring支持切点表达式功能-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <!-- 这个依赖是spring专门用来与其他持久层框架进行整合用的一个依赖里面有LocalSessionFactoryBean类型-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- 此依赖有事务方面的通知,事务管理器等 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--hibernate-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>

        <!--struts-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.5.12</version>
        </dependency>

        <!--oracle-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>


        <!-- 这是dbcp连接池依赖,完全可以换别的连接池组件-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!--<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>-->

    </dependencies>

    <build>
        <plugins>
            <!--用于设定源代码文件的编码,以及项目代码运行的目标jdk版本为1.8 -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 指定项目中web资源的根目录,用于maven打包时把此目录的所有内容拷贝到war中-->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <warSourceDirectory>web</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

Goods.java

package entity;

import java.util.Date;

public class Goods {

    private int Gno ; //编号
    private String gname ;  //物质名称
    private String gtype ;  //物质类型
    private int Gnumber ;  //物质数量
    private String Gcompany ;  //生产商
    private Date Gcreatetime ;  //生产日期
    private Date Uintime ;  //入库时间

    public int getGno() {
        return Gno;
    }

    public void setGno(int gno) {
        Gno = gno;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public String getGtype() {
        return gtype;
    }

    public void setGtype(String gtype) {
        this.gtype = gtype;
    }

    public int getGnumber() {
        return Gnumber;
    }

    public void setGnumber(int gnumber) {
        Gnumber = gnumber;
    }

    public String getGcompany() {
        return Gcompany;
    }

    public void setGcompany(String gcompany) {
        Gcompany = gcompany;
    }

    public Date getGcreatetime() {
        return Gcreatetime;
    }

    public void setGcreatetime(Date gcreatetime) {
        Gcreatetime = gcreatetime;
    }

    public Date getUintime() {
        return Uintime;
    }

    public void setUintime(Date uintime) {
        Uintime = uintime;
    }
}

 

GoodsDao.java

package dao;

import entity.Goods;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;

import java.util.Date;
import java.util.List;

public class GoodsDao {
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

    //查询全部
    public List<Goods> getAll(){
        Session session = sessionFactory.getCurrentSession();
        String sql = "from Goods as g order by g.Gcreatetime desc";
        return session.createQuery(sql).list();
    }

    //增加
    public void addGoods(Goods goods){
        Session session = sessionFactory.getCurrentSession();
        session.save(goods);
    }

    //删除
    public void delete(int Gno){
        Session session = sessionFactory.getCurrentSession();
        session.delete(session.get(Goods.class,Gno));
    }

    //根据编号查询
    public Goods getById(int Gno){
        Session session = sessionFactory.getCurrentSession();
        return session.get(Goods.class,Gno);
    }

    //修改
    public void update(Goods goods){
        Session session = sessionFactory.getCurrentSession();
        session.update(goods);
    }

    //按条件查询
    public List<Goods> getByTerm(String gname, Date firstTime,Date lastTime){
        Session session = sessionFactory.getCurrentSession();
        String hql = "from Goods g where g.gname like :gname and g.Gcreatetime between :firstTime and :lastTime";
        Query query = session.createQuery(hql);
        query.setParameter("gname","%"+gname+"%");
        query.setParameter("firstTime",firstTime);
        query.setParameter("lastTime",lastTime);
        return query.list();

    }

    //批量删除
    public void deleteAll(String[] Gno){
        Session session = sessionFactory.getCurrentSession();
        for(int i=0;i<Gno.length;i++){
            Goods goods = this.getById(Integer.parseInt(Gno[i]));
            session.delete(goods);
        }
    }

}

 

 GoodsService.java

package service;

import dao.GoodsDao;
import entity.Goods;

import java.util.Date;
import java.util.List;

public class GoodsService {
    private GoodsDao goodsDao;

    public GoodsDao getGoodsDao() {
        return goodsDao;
    }

    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    public List<Goods> getAll(){
        return goodsDao.getAll();
    }

    public void addGoods(Goods goods){
        goodsDao.addGoods(goods);
    }

    public void delete(int Gno){
        goodsDao.delete(Gno);
    }

    public Goods getById(int Gno){
        return goodsDao.getById(Gno);
    }

    public void update(Goods goods){
        goodsDao.update(goods);
    }

    public List<Goods> getByTerm(String gname, Date firstTime, Date lastTime){
        return goodsDao.getByTerm(gname,firstTime,lastTime);
    }

    public void deleteAll(String[] Gno){
        goodsDao.deleteAll(Gno);
    }
}

 

GoodsController.java

package controller;

import com.opensymphony.xwork2.ActionContext;
import entity.Goods;
import service.GoodsService;

import java.util.Date;
import java.util.List;

public class GoodsController {
    private GoodsService goodsService;

    private Goods goods;

    private Date firstTime;
    private Date lastTime;
    private String Gnos;

    public String getGnos() {
        return Gnos;
    }

    public void setGnos(String gnos) {
        Gnos = gnos;
    }

    public Date getFirstTime() {
        return firstTime;
    }

    public void setFirstTime(Date firstTime) {
        this.firstTime = firstTime;
    }

    public Date getLastTime() {
        return lastTime;
    }

    public void setLastTime(Date lastTime) {
        this.lastTime = lastTime;
    }

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    public GoodsService getGoodsService() {
        return goodsService;
    }

    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    //查询全部
    public String list(){
        List<Goods> goods = goodsService.getAll();
        ActionContext.getContext().put("goods",goods);
        return "list";
    }
    //添加商品
    public String add(){
        goodsService.addGoods(goods);
        return "success";
    }
    //删除商品
    public String delete(){
        goodsService.delete(goods.getGno());
        return "success";
    }
    //批量删除
    public String deleteAll(){
        System.out.println(Gnos);
        String[] gnos = Gnos.split(",");
        goodsService.deleteAll(gnos);
        return "success";
    }
    //查询要修改的商品
    public String edit(){
        Goods goods1 = goodsService.getById(goods.getGno());
        ActionContext.getContext().put("goods1",goods1);
        return "edit";
    }
    //修改商品
    public String update(){
        goodsService.update(goods);
        return "success";
    }

    //按条件查询
    public String getByTerm(){
        List<Goods> goods2 = goodsService.getByTerm(goods.getGname(),firstTime,lastTime);
        ActionContext.getContext().put("goods",goods2);
        return "list";
    }
}

 

Goods.hbm.xml

注意:

  1.table需要写全称。

  2.dynamic-insert和dynamic-update表示是否启动默认值,如果没有设置true则数据库中默认的字段会为空。

  3.主键生成策略为increment,其它无效,不知道为什么。

<?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">
<hibernate-mapping package="entity">

    <class name="entity.Goods" table="SCOTT.GOODS" dynamic-insert="true" dynamic-update="true" >
        <id name="Gno" column="Gno">
            <generator class="increment">
                <!--<param name="sequence">GOODS_ID</param>-->
            </generator>
        </id>
        <property name="gname" column="gname"/>
        <property name="gtype" column="gtype"/>
        <property name="Gnumber" column="Gnumber"/>
        <property name="Gcompany" column="Gcompany"/>
        <property name="Gcreatetime" column="Gcreatetime"/>
        <property name="uintime" column="uintime"/>
    </class>
</hibernate-mapping>

 

db.properties

url=jdbc:oracle:thin:@localhost:1521:orcl
driverclass=oracle.jdbc.driver.OracleDriver
username=SCOTT
password=tiger

 

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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/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">

    <context:property-placeholder location="classpath:db.properties" local-override="true"/>
    <

(c)2006-2024 SYSTEM All Rights Reserved IT常识