Spring JDBC与事务管理 问题

来源:1-5 自由编程

rudtjd

2023-03-05 19:36:21

https://img.mukewang.com/climg/64047e4a090c302108330198.jpg

麻烦老师帮我看看为什么报错不出来


<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/jdbc_imooc?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

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


    <bean id="hotelDao" class="com.imooc.spring.jdbc.dao.HotelDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>



</beans>
package com.imooc.spring.jdbc.dao;

import com.imooc.spring.jdbc.entity.Hotel;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class HotelDao {
    private JdbcTemplate jdbcTemplate;

    public Hotel findById(Integer orderno){
        String sql = " select * from hotel where orderno=?";
        Hotel hotel = jdbcTemplate.queryForObject(sql, new Object[]{orderno}, new BeanPropertyRowMapper<Hotel>(Hotel.class));
        return hotel;
    }

}
import com.imooc.spring.jdbc.dao.HotelDao;
import com.imooc.spring.jdbc.entity.Hotel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class JdbcTemplateTestor {
    @Resource
    private HotelDao hotelDao;

    @Test
    public void findById(){
        Hotel hotel = hotelDao.findById(10001);
        System.out.println(hotel);
    }


}
package com.imooc.spring.jdbc.entity;

import java.util.Date;

public class Hotel {
    private Integer orderNO;
    private String city;
    private Float price;
    private String hotelname;
    private Date arriveDate;
    private Date leaveDate;

    @Override
    public String toString() {
        return "Hotel{" +
                "orderNO=" + orderNO +
                ", city='" + city + '\'' +
                ", price=" + price +
                ", hotelname='" + hotelname + '\'' +
                ", arriveDate=" + arriveDate +
                ", leaveDate=" + leaveDate +
                '}';
    }

    public Integer getOrderNO() {
        return orderNO;
    }

    public void setOrderNO(Integer orderNO) {
        this.orderNO = orderNO;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getHotelname() {
        return hotelname;
    }

    public void setHotelname(String hotelname) {
        this.hotelname = hotelname;
    }

    public Date getArriveDate() {
        return arriveDate;
    }

    public void setArriveDate(Date arriveDate) {
        this.arriveDate = arriveDate;
    }

    public Date getLeaveDate() {
        return leaveDate;
    }

    public void setLeaveDate(Date leaveDate) {
        this.leaveDate = leaveDate;
    }
}
<?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.imooc</groupId>
    <artifactId>jdbc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>


写回答

1回答

好帮手慕小尤

2023-03-06

同学你好,在HotelDao类中缺少jdbcTemplate属性的set/get方法,从而导致出现异常。建议同学添加,如下所示:然后重新测试代码试一下

https://img.mukewang.com/climg/640551140945412111750381.jpg

祝学习愉快!

0

0 学习 · 9886 问题

查看课程