【问题标题】:Spring mvc MappingSqlQuery Property 'sql' is requiredSpring mvc MappingSqlQuery 属性“sql”是必需的
【发布时间】:2016-12-11 19:30:47
【问题描述】:

我使用 Netbeans、Spring MVC 和 Oracle。

我想使用 MappingSqlQuery 来执行 oracle sql。这是实现 MappingSqlQuery 的 java 类

public class SelectAllDepartamentos extends MappingSqlQuery<Departamento> {
      private static final String SQL_SELECT_DEPT =

            "SELECT DEPT_NO, DNOMBRE, LOC FROM DEPT";

    public SelectAllDepartamentos() {
    }
    public SelectAllDepartamentos(DataSource dataSource) {
        super(dataSource,SQL_SELECT_DEPT);

    }

    @Override
    protected Departamento mapRow(ResultSet rs, int i) throws SQLException {
       Departamento dept = new Departamento();
        dept.setNumero(rs.getInt("DEPT_NO"));

        dept.setNombre(rs.getString("DNOMBRE"));

        dept.setLocalidad(rs.getString("LOC"));

        return dept;
    }

}

使用 SelectAllDepartamentos 的类是 .我使用的方法是 findAll

public class JdbcDepartamentoDao1 implements InitializingBean,DepartamentoDao{
    private javax.sql.DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    private SelectAllDepartamentos selectdepartamentos;

    public JdbcDepartamentoDao1() {
    }
       public JdbcDepartamentoDao1(javax.sql.DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void setDataSource(javax.sql.DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.selectdepartamentos = new SelectAllDepartamentos();
    }

    @Override
    public List<Departamento> findAll() {
    return this.selectdepartamentos.execute();
    }

    @Override
    public List<Departamento> findByLocalidad(String localidad) {
      return null;
    }

    @Override
    public String findById(int iddepartamento) {
        String nombre = jdbcTemplate.queryForObject("SELECT DNOMBRE from DEPT WHERE DEPT_NO = ?",
                new Object[]{iddepartamento},String.class);
        return nombre;
    }

    @Override
    public void insertarDepartamento(Departamento departamento) {

    }

    @Override
    public void modificarDepartamento(Departamento departamento) {

    }

    @Override
    public void eliminarDepartamento(Departamento departamento) {

    }

    @Override
    public void afterPropertiesSet() throws Exception {
    if (dataSource == null){
        throw new BeanCreationException("Debe establece el dataSource ContactDao");
    }
    }

}

我的 application-context.xml 是

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />


          <bean id="departamentoDao" class="dao.JdbcDepartamentoDao1">
              <property name="dataSource" ref="dataSource"/>
          </bean>

 <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

   <bean id="selectAllDepartamentos" class="modelos.SelectAllDepartamentos">
    <property name="dataSource" ref="dataSource"></property>
    <constructor-arg type="DataSource" ref="dataSource"></constructor-arg>
</bean>



</beans>

当我执行 findAll 方法时

 @Override
    public List<Departamento> findAll() {
    return this.selectdepartamentos.execute();
    }

我得到了错误

【问题讨论】:

    标签: sql oracle spring-mvc properties spring-jdbc


    【解决方案1】:

    删除

    /*public void setDataSource(javax.sql.DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.selectdepartamentos = new SelectAllDepartamentos();
    }*/
    

    更改为 arg

    <bean id="departamentoDao" class="dao.JdbcDepartamentoDao1">
        <constructor-arg ref="dataSource" />
    </bean>
    

    更新构造函数

    public JdbcDepartamentoDao1(javax.sql.DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.selectdepartamentos = new SelectAllDepartamentos();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 2012-09-16
      • 1970-01-01
      • 2013-04-24
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多