Spring JDBC与事务管理 作业1-5

来源:1-5 自由编程

rudtjd

2023-03-06 14:08:31

<?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.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;
    }
}
package com.imooc.spring.jdbc.dao;

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

import javax.swing.plaf.synth.SynthToolTipUI;
import java.util.List;

public class HotelDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

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

    // 1.查询订单号为****的订单信息
    public Hotel findById(Integer orderno){
        String sql = " select * from hotel where orderno=?";
        Hotel id = jdbcTemplate.queryForObject(sql, new Object[]{orderno}, new BeanPropertyRowMapper<Hotel>(Hotel.class));
        return id;
    }

    // 2.查询city订单信息
    public List<Hotel> findByCity(String city){
        String sql = " select * from hotel where city=? ";
        List<Hotel> list = jdbcTemplate.query(sql, new Object[]{city}, new BeanPropertyRowMapper<Hotel>(Hotel.class));
        return list;
    }

    // 3.向表格中插入一条数据
    public void insert(Hotel hotel){
        String sql = " insert into hotel(orderno,city,price,hotelname,arrivedate,leavedate) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,new Object[]{
                hotel.getOrderNO(),hotel.getCity(),hotel.getPrice(),hotel.getHotelname(),hotel.getArriveDate(),hotel.getLeaveDate()
        });
    }

    // 4.更新表格数据
    public int update(Hotel hotel){
        String sql = "update hotel set arrivedate=?,leavedate=? where orderno=?";
        int count = jdbcTemplate.update(sql, new Object[]{
                hotel.getArriveDate(), hotel.getLeaveDate(), hotel.getOrderNO()
        });
        return count;
    }

    // 5.将订单号为10005的订单删除
    public int delete(Integer orderno){
        String sql = "delete from hotel where orderno=?";
        return jdbcTemplate.update(sql,new Object[]{orderno});
    }
    
}
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;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

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

    // 1.查询订单号为10001的订单信息
    @Test
    public void testFindById(){
        Hotel hotel = hotelDao.findById(10001);
        System.out.println(hotel);
    }

    // 2.查询上海市的订单信息
    @Test
    public void testFindCity(){
        List<Hotel> city = hotelDao.findByCity("上海");
        System.out.println(city);
    }

    // 3.向表格中插入一条数据
    @Test
    public void testInsert() throws ParseException {
        String ArrDate = "2015-05-08";
        String LeDate = "2015-05-11";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Hotel hotel = new Hotel();
        hotel.setOrderNO(10005);
        hotel.setCity("北京");
        hotel.setPrice(588f);
        hotel.setHotelname("酒店5");
        hotel.setArriveDate(simpleDateFormat.parse(ArrDate));
        hotel.setLeaveDate(simpleDateFormat.parse(LeDate));
        hotelDao.insert(hotel);
    }

    // 4.修改订单号为10003的到达时间改为2020-4-30,离开时间改为2020-5-3
    @Test
    public void testUpdate() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = "2020-04-30";
        String date2 = "2020-05-03";
        Hotel hotel = hotelDao.findById(10003);
        hotel.setArriveDate(simpleDateFormat.parse(date1));
        hotel.setLeaveDate(simpleDateFormat.parse(date2));
        int count = hotelDao.update(hotel);
        System.out.println("本次更新了"+count+"条消息");
    }

    // 5.将订单号为10005的订单删除
    @Test
    public void testDelete(){
        int count = hotelDao.delete(10005);
        System.out.println("本次删除了"+count+"条消息");
    }

}


写回答

1回答

好帮手慕小尤

2023-03-06

同学你好,测试同学代码已完成练习,棒棒哒!继续加油!

祝学习愉快!

0

0 学习 · 9886 问题

查看课程