【问题标题】:Changes from spring mvc not getting reflected in oracle databasespring mvc 的更改未反映在 oracle 数据库中
【发布时间】:2020-04-24 08:11:22
【问题描述】:

我正在学习从 Spring MVC 建立到 oracle 数据库的连接。所以这就是我到目前为止所做的事情

spring.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"
    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">

    <context:annotation-config/>
    <context:component-scan base-package="org.springdemo" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@192.168.0.8:1521:xe"/>
        <property name="username" value="sys as sysdba"/>
        <property name="password" value="7299"/>
    </bean>



</beans> 

Circle.java

package org.springdemo.model;

public class Circle {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Circle(int id , String name){

        setId(id);
        setName(name);
    }


}

JdbcDaoImpl.java

package org.springdemo.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class JdbcDaoImpl {

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate = new JdbcTemplate();

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void getCircle(){


        String sql = "SELECT COUNT(*) FROM circle";
        jdbcTemplate.setDataSource(getDataSource());
        int count = jdbcTemplate.queryForObject(sql,Integer.class);
        System.out.println("count is"+count);

    }

    public void delete(int id) {
        JdbcTemplate delete = new JdbcTemplate(dataSource);
        delete.update("DELETE from CIRCLE where ID= ?",
                new Object[] {id});
        System.out.println("deleted successfully");
    }

}

JdbcDemo.java

package org.springdemo;

import org.springdemo.dao.JdbcDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcDemo {



    public static void main(String[] args) {

        ApplicationContext ctxt = new ClassPathXmlApplicationContext("spring.xml");
        JdbcDaoImpl circleDao =(JdbcDaoImpl) ctxt.getBean("jdbcDaoImpl", JdbcDaoImpl.class);
        circleDao.getCircle();
        circleDao.delete(2);

    }

}

一切似乎都运行良好,没有任何错误,我得到以下输出。

但在数据库中我有 3 行,我的表没有得到更新。

当我尝试查询一个不存在的表以确认我是否连接到数据库时,我收到异常,表明我只连接到数据库。有人可以解释一下我的更改未反映在数据库中的可能原因是什么。

【问题讨论】:

    标签: java spring-mvc oracle-sqldeveloper jdbctemplate oracle18c


    【解决方案1】:

    让 Spring 实例化你的 jdbctemplate

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">    
    <property name="dataSource" ref="dataSource"></property>    
    </bean> 
    
    @Autowired
    private JdbcTemplate jdbcTemplate
    

    不要使用 Obect[] 而只使用 id。

    delete.update 方法返回什么?

    【讨论】:

    • 添加注释有效。谢谢你 。我正在关注一个教程,所以他作为第一步,而不是注入依赖项,他使用 new 运算符创建了 obj 。所以我认为这将是创建对象的一种正常方式,因此它不需要 @Autowired 注释。你能解释一下为什么它不起作用
    • 通常 JdbcTemplate 可以在方法中实例化 可以通过使用 DataSource 引用的直接实例化在服务实现中使用,或者在应用程序上下文中准备好并作为 bean 引用提供给服务。注意:DataSource 应该始终配置为应用程序上下文中的 bean,在第一种情况下直接提供给服务,在第二种情况下提供给准备好的模板。我认为是对象 []
    • 实际上在我的代码中发生的事情是我在方法调用之前创建了对象并使用 new 运算符进行了实例化。因此对象的实例化没有正确发生。所以我修改了我的代码,它对我有用。 public void getCircle(){ jdbcTemplate = new JdbcTemplate(); String sql = "SELECT COUNT(*) FROM circle"; jdbcTemplate.setDataSource(getDataSource()); int count = jdbcTemplate.queryForObject(sql,Integer.class); System.out.println("计数为"+count); }
    猜你喜欢
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2019-12-16
    相关资源
    最近更新 更多