Spring   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java.lang.ClassCastException:com.sun.proxy.$Proxy0无法强制转换为org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图在Spring Framework应用程序中实现关于声明性事务的本教程,但是不起作用,因为当我尝试执行MainApp类来测试应用程序行为时,我得到一个错误

http://www.tutorialspoint.com/spring/declarative_management.htm

所以我有studentDAO接口我只定义了我想要的CRUD方法

package org.andrea.myexample.myDeclarativetransactionSpring;

import java.util.List;

import javax.sql.Datasource;

/** Interfaccia che definisce i metodi che implementano le operazioni di CRUD
 *  che vogliamo implementare nel nostro DAO:
 */
public interface studentDAO {

    /**
     * Questo metodo viene usato per inizializzare le risorse del database cioè
     * la connessione al database:
     */
    public void setDatasource(Datasource ds);

    /**
     * Questo metodo serve a creare un record nella tabella student e nella
     * tabella Marks:
     */
    public void create(String name,Integer age,Integer marks,Integer year);

    /**
     * Questo metodo serve ad elencare tutti i record all'interno della tabella
     * studend e della tabella Marks
     */
    public List

然后我有studentMark类,它将我的实体暴露在数据库的2表上:

package org.andrea.myexample.myDeclarativetransactionSpring;

// Rappresenta l'entity:
public class studentMarks {

    // Proprietà:
    private Integer age;
    private String name;
    private Integer id;
    private Integer marks;
    private Integer year;
    private Integer sid;

    // Metodi Getter & Setter:
    public void setAge(Integer agE) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String Name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setMarks(Integer marks) {
        this.marks = marks;
    }

    public Integer getMarks() {
        return marks;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Integer getYear() {
        return year;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Integer getSid() {
        return sid;
    }
}

然后我有实现RowMapper接口的类studentMarksmapper:

package org.andrea.myexample.myDeclarativetransactionSpring;

import java.sql.ResultSet;
import java.sql.sqlException;
import org.springframework.jdbc.core.RowMapper;


/** Classe che implementa l'interfaccia RowMapper. Si tratta di un'interfaccia
 *  usata da JdbcTemplate per mappare le righe di un ResultSet (oggetto che 
 *  contiene l'insieme delle righe restituite da una query sql) riga per riga.
 *  Le implementazioni di questa interfaccia mappano ogni riga su di un oggetto
 *  risultante senza doversi preoccupare della gestione delle eccezioni poichè
 *  le sqlException Saranno catturate e gestite dalla chiamata a JdbcTemplate.
 */
public class studentMarksmapper implements RowMappersql.ResultSet,int)
     */
    public studentMarks mapRow(ResultSet rs,int ROWNUM) throws sqlException {

        studentMarks studentMarks = new studentMarks();

        studentMarks.setId(rs.geTint("id"));
        studentMarks.setName(rs.getString("name"));
        studentMarks.setAge(rs.geTint("age"));
        studentMarks.setSid(rs.geTint("sid"));
        studentMarks.setMarks(rs.geTint("marks"));
        studentMarks.setYear(rs.geTint("year"));

        return studentMarks;
    }
}

它旁边是studentJDBCTemplate类,即studentDAO接口:

package org.andrea.myexample.myDeclarativetransactionSpring;

import java.util.List;
import javax.sql.Datasource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Classe che fornisce l'implementazione per il nostro DAO le cui funzionalità
 * di CRUD sono state definite tramite l'interfaccia studentDAO
 */
public class studentJDBCTemplate implements studentDAO {

    // Utility per l'accesso alla sorgente dati
    private JdbcTemplate jdbcTemplateObject;

    /**
     * Metodo Setter per l'Injection della dipendenza relativa alla sorgente
     * dati. Tale metodo inoltre costruisce anche l'oggetto istanza di
     * JdbcTemplate usato per interagire con i dati nel database.
     * 
     * @param la sorgente dati
     */
    public void setDatasource(Datasource datasourcE) {
        this.jdbcTemplateObject = new JdbcTemplate(datasourcE);
    }

    /**
     * Metodo relativo all'operazione di CREATE che inserisce un nuovo record
     * all'interno della tabella student ed un correlato nuovo record nella
     * tabella Marks.
     */
    public void create(String name,Integer year) {

        try {
            // Query che inserisce nome ed età nella tabella student:
            String sql1 = "insert into student (name,agE) values (?,?)";
            // Esegue la query passandogli anche i valori effettivi da inserire:
            jdbcTemplateObject.update(sql1,name,agE);

            // SELEziona l'ultimo studente inserito nella tabella Marks:
            String sql2 = "SELEct max(id) from student";
            // Esegue la query e mette il risultato (l'ID) in sid:
            int sid = jdbcTemplateObject.queryForInt(sql2);

            /**
             * Query che inserisce un nuovo record nella tabella Marks. Il
             * record rappresenta il voto per l'ultimo studente inserito nella
             * tabella student:
             */
            String sql3 = "insert into Marks(sid,marks,year) "
                    + "values (?,?,?)";
            // Esegue la query passandogli anche i valori effettivi da inserire:
            jdbcTemplateObject.update(sql3,sid,year);

            System.out.println("Created Name = " + name + ",Age = " + agE);

            // SIMULA UNA RuntimeExceptio:
            throw new RuntimeException("Simulazione di una condizione d'errore");
        } catch (DataAccessException E) {       // GESTIONE DELL'ECCEZIONE
            System.out.println("Errore nella creazione dei record,esegue rollBACk");
            throw e;
        }
    }

    /**
     * Metodo relativo all'operazione di READ che recupera la lista degli
     * studenti e dei relativi voti
     * 
     * @return La lista di oggetti che rappresentano uno studente ed i suoi voti
     *         correlati
     */
    public Listsql = "SELEct * from student,Marks where student.id=Marks.sid";

        /**
         * Ottengo la lista degli oggetti studentMarks,corrispondenti ognuno ad
         * un record della tabella student con i correlati vori rappresentati
         * dai record della tabella Marks,invocando il metodo query 
         * sull'oggetto JdbcTemplate passandogli i seguenti parametri.
         * 
         * @param La query per creare il preparated statement
         * @param Un oggetto che implementa RowMapper che viene usato per
         *        mappare una singola riga della tabella su di un oggetto Java
         */
        Listsql,new studentMarksmapper());
        return studentMarks;
    }
}

然后这是测试应用程序的MainApp类:

package org.andrea.myexample.myDeclarativetransactionSpring;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

// Classe principale:
public class MainApp {

    public static void main(String[] args) {

        /**
         * Crea il contesto in base alle impostazioni dell'applicazione definite
         * nel file Beans.xml
         */
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        /**
         * Recupera un bean avente id="studentJDBCTemplate" nel file di
         * configurazione Beans.xml
         */
        studentJDBCTemplate studentJDBCTemplate = (studentJDBCTemplatE) context.getBean("studentJDBCTemplate");

        System.out.println("------Creazione dei record--------");
        // Creo i record nelle tabelle studend e Marks:
        studentJDBCTemplate.create("Zara",11,99,2010);
        studentJDBCTemplate.create("Nuha",20,97,2010);
        studentJDBCTemplate.create("Ayan",25,100,2011);

        System.out.println("------Elenca tutti i record--------");
        // Recupera la lista degli studenti con i voti ad essi associati:
        Liststem.out.print("ID : " + record.getId());
            System.out.print(",Name : " + record.getName());
            System.out.print(",Marks : " + record.getMarks());
            System.out.print(",Year : " + record.getYear());
            System.out.println(",Age : " + record.getAge());
        }
    }
}

最后这是我的Beans.xml配置文件

http://www.springframework.org/scheR_840_11845@a/beans" xmlns:xsi="http://www.w3.org/2001/XMLscheR_840_11845@a-instance" xmlns:tx="http://www.springframework.org/scheR_840_11845@a/tx" xmlns:aop="http://www.springframework.org/scheR_840_11845@a/aop" xsi:scheR_840_11845@aLOCATIOn="http://www.springframework.org/scheR_840_11845@a/beans
   http://www.springframework.org/scheR_840_11845@a/beans/spring-beans-3.0.xsd 
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    

大佬总结

以上是大佬教程为你收集整理的java.lang.ClassCastException:com.sun.proxy.$Proxy0无法强制转换为org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate全部内容,希望文章能够帮你解决java.lang.ClassCastException:com.sun.proxy.$Proxy0无法强制转换为org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。