在第二个DAO类中可能出现错误,虽然我使用相同的算法,这在我的第一个DAO类中工作正常吗? (Spring JDBC)

时间:2014-07-07 17:21:42

标签: java spring spring-jdbc

不工作案例

在unworking情况下,该方法始终返回

中具有空值的bean

表格

int- matrix_id; int user_id; varchar(45)matrix_name; int matrix_length;

MatrixDAO:

    package com.calango.DAOLayer.DAO;

import com.calango.DAOLayer.model.Matrix;

/**
 *
 * @author Calango
 */
public interface MatrixDAO {
    public Matrix findByID(int id);
    public String findMatrixNameById(int matrixId);
    public int getMatrixLength(int matrix_id);
    public void insertRecord(Matrix inserObject);
    public int findIdByName(String matrixName);
}

JdbcMatrixDAO:

package com.calango.DAOLayer.DAO.impl;

import com.calango.DAOLayer.DAO.MatrixDAO;
import com.calango.DAOLayer.model.Matrix;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author Calango
 */
public class JdbcMatrixDAO implements MatrixDAO{
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

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

    @Override
    public Matrix findByID(int matrixId) {
        String sqlForID = "SELECT * FROM matrix WHERE matrix_id = ?";
        jdbcTemplate = new JdbcTemplate(dataSource);
        Matrix matrix = (Matrix)jdbcTemplate.queryForObject(
        sqlForID, new Object[] { matrixId }, 
        new BeanPropertyRowMapper(Matrix.class));
        return matrix;
    }

    @Override
    public String findMatrixNameById(int matrixId){return findByID(matrixId).getMatrixName();}

    @Override
    public int getMatrixLength(int matrixId){return findByID(matrixId).getMatrixLength();}

    @Override
    public void insertRecord(Matrix insertObject) {
        String insertMatrix = "INSERT INTO matrix (user_id, matrix_name, matrix_length) VALUES (?, ?, ?)";
        jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update(insertMatrix, new Object[] { insertObject.getUserId(),
            insertObject.getMatrixName(),insertObject.getMatrixLength()
    });
    }

    @Override
    public int findIdByName(String matrixName) {
        String sqlForName = "SELECT matrix_id FROM matrix WHERE matrix_name = ?";
        jdbcTemplate = new JdbcTemplate(dataSource);
        Matrix matrix = (Matrix)jdbcTemplate.queryForObject(
        sqlForName, new Object[] { matrixName }, 
        new BeanPropertyRowMapper(Matrix.class));
        return matrix.getMatrixId();
    }
}

弹簧database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="location">
            <value>config.properties</value>
        </property>
    </bean>

    <bean id="historyDAO" class="com.calango.DAOLayer.DAO.impl.JdbcHistoryDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="elementsDAO" class="com.calango.DAOLayer.DAO.impl.JdbcElementsDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="matrixDAO" class="com.calango.DAOLayer.DAO.impl.JdbcMatrixDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>        

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="${DB_DRIVER}" />
        <property name="url" value="${DB_CONNECTION}" />
        <property name="username" value="${DB_USER}" />
        <property name="password" value="${DB_PASSWORD}" />
    </bean>

</beans>

我打电话:

        ApplicationContext context = 
            new ClassPathXmlApplicationContext("spring-database.xml");
int matrixID = 4;
JdbcMatrixDAO matrixDAO = (JdbcMatrixDAO) context.getBean("matrixDAO");
String matrixName = matrixDAO.findMatrixNameById(matrixID);

矩阵

package com.calango.DAOLayer.model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Calango
 */
@Entity
@Table(name="matrix")
public class Matrix implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "matrix_id", nullable = false)   
    int matrixId;
    @Column(name = "user_id")
    int userId;
    @Column(name = "matrix_name")        
    String matrixName;
    @Column(name = "matrix_length")
    int matrixLength;

    public Matrix(){}

    public Matrix(int matrixId, int userId, String matrixName, int matrixLength){
        this.matrixId = matrixId;
        this.userId = userId;
        this.matrixName = matrixName;
        this.matrixLength = matrixLength;
    }

    public int getMatrixId() {
        return matrixId;
    }

    public int getUserId() {
        return userId;
    }

    public String getMatrixName() {
        return matrixName;
    }

    public int getMatrixLength() {
        return matrixLength;
    }
}

工作案例

记录

package com.calango.DAOLayer.model;

import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.*;

/**
 *
 * @author Calango
 */
@Entity
@Table(name="history")
public class History implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "history_id", nullable = false)        
    int historyId;
    @Column(name = "matrix1_id")
    int matrix1Id;
    @Column(name = "matrix2_id")
    int matrix2Id;
    @Column(name = "matrix_result_id")
    int matrixResultId;
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)        
    Timestamp date;

    public History(){}

    public History
        (int historyId, int matrix1ID, int matrix2ID, 
                int matrixResultID, Timestamp date) {
        this.historyId = historyId;
        this.matrix1Id = matrix1ID;
        this.matrix2Id = matrix2ID;
        this.matrixResultId = matrixResultID;
        this.date = date;
    }

    public int getHistoryId() {
        return historyId;
    }    

    public int getMatrix1Id() {
        return matrix1Id;
    }

    public int getMatrix2Id() {
        return matrix2Id;
    }

    public int getMatrixResultId() {
        return matrixResultId;
    }

    public Timestamp getDate() {
        return date;
    }

    public void setHistoryId(int historyId) {
        this.historyId = historyId;
    }

    public void setMatrix1Id(int matrix1Id) {
        this.matrix1Id = matrix1Id;
    }

    public void setMatrix2Id(int matrix2Id) {
        this.matrix2Id = matrix2Id;
    }

    public void setMatrixResultId(int matrixResultId) {
        this.matrixResultId = matrixResultId;
    }

    public void setDate(Timestamp date) {
        this.date = date;
    }
}

HistoryDAO

package com.calango.DAOLayer.DAO;

import com.calango.DAOLayer.model.History;
import java.util.List;

/**
 *
 * @author Calango
 */
public interface HistoryDAO {
    public History findByID(int id);
    public int findIdByDate(String date);
    public List<String> getDatesList();
    public void insertHistory(History insertObject);
}

JdbcHistoryDAO

package com.calango.DAOLayer.DAO.impl;

import com.calango.DAOLayer.DAO.HistoryDAO;
import com.calango.DAOLayer.model.History;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author Calango
 */
public class JdbcHistoryDAO implements HistoryDAO{
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

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

    @Override
    public History findByID(int historyId) {
        String sqlForID = "SELECT * FROM history WHERE history_id = ?";
        jdbcTemplate = new JdbcTemplate(dataSource);
        History history = (History)jdbcTemplate.queryForObject(
            sqlForID, new Object[] { historyId }, 
            new BeanPropertyRowMapper(History.class));
        return history;
    }

    @Override
    public int findIdByDate(String date) {
        try{
        String sqlForDate = "SELECT * FROM history WHERE date = ?";
        jdbcTemplate = new JdbcTemplate(dataSource);
        History history = (History)jdbcTemplate.queryForObject(
            sqlForDate, new Object[] { date }, 
            new BeanPropertyRowMapper(History.class));
        return history.getMatrix1Id();
        }catch(EmptyResultDataAccessException ex){
            System.out.println("No date has been found: "+date);
            return 1;
        }
        //Logger.getLogger(JdbcHistoryDAO.class.getName()).log(Level.SEVERE, null, "No id was found by this date");    
    }

    @Override
    public List<String> getDatesList(){
        String sqlForDates = "SELECT date FROM history";
        jdbcTemplate = new JdbcTemplate(dataSource);
        List<String> dates = 
                (List<String>)jdbcTemplate.queryForList(sqlForDates,String.class);
        return dates;
    }

    @Override
    public void insertHistory(History insertObject) {
        String insertHistorySql = "INSERT INTO history (matrix1_id, matrix2_id, matrix_result_id, date) VALUES(?, ?, ?, NOW())"; 
        jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update(insertHistorySql, new Object[] { insertObject.getMatrix1Id(),
                    insertObject.getMatrix2Id(),insertObject.getMatrixResultId()
        });
    }
}

调用

ApplicationContext context = 
     new ClassPathXmlApplicationContext("spring-database.xml");
JdbcHistoryDAO historyDAO = (JdbcHistoryDAO) context.getBean("historyDAO");
String selectedDate = "2014-05-01 16:10:09.0";
int matrixID = historyDAO.findIdByDate(selectedDate);

0 个答案:

没有答案