自由编程,批量插入

自由编程,批量插入

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;

   }
   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;

   }
   public int insert(Hotel hotel){
       String sql="insert into hotel(orderNo,city,price,hotelName,arriveDate,leaveDate)values(?,?,?,?,?,?)";
       int i = jdbcTemplate.update(sql, new Object[]{hotel.getOrderNo(), hotel.getCity(), hotel.getPrice(), hotel.getHotelName(), hotel.getArriveDate(), hotel.getLeaveDate()});

       return i;
   }
   public int alter(Hotel hotel){
       String sql="update hotel set city=?,price=?,hotelName=?,arriveDate=?,leaveDate=? where orderNO=? ";
       int count = jdbcTemplate.update(sql, new Object[]{hotel.getCity(), hotel.getPrice(), hotel.getHotelName(),hotel.getArriveDate(), hotel.getLeaveDate(),hotel.getOrderNo()});
       return count;
   }
   public  int delete(Integer integer){
       String sql="delete from hotel where orderNo=?";
       int count = jdbcTemplate.update(sql, new Object[]{integer});
       return count;

   }


    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
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.service;

import com.imooc.spring.jdbc.dao.HotelDao;
import com.imooc.spring.jdbc.entity.Hotel;

import java.util.Date;

public class HotelService {
    private HotelDao hotelDao;
    public  void batchImport(){
     for (int i=1;i<=5;i++){
         if (i==3){
             throw new RuntimeException();
         }
         Hotel hotel=new Hotel();
         hotel.setOrderNo(100+i);
         hotel.setCity("长沙");
         hotel.setHotelName("酒店8"+i);
         hotel.setArriveDate(new Date());
         hotel.setLeaveDate(new Date());
         hotel.setPrice(999.98f+i);
         hotelDao.insert(hotel);
     }
    }

    public HotelDao getHotelDao() {
        return hotelDao;
    }

    public void setHotelDao(HotelDao hotelDao) {
        this.hotelDao = hotelDao;
    }
}
<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
       http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- bean definitions here -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <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>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
    <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManger">
        <tx:attributes>
            <tx:method name="batchImport" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(public * com.imooc..*Service.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
    <bean id="hotelDao" class="com.imooc.spring.jdbc.dao.HotelDao">
<property name="jdbcTemplate" ref="JdbcTemplate"></property>
    </bean>
    <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">
        </property>
    </bean>
    <bean id="hotelService" class="com.imooc.spring.jdbc.service.HotelService">
        <property name="hotelDao" ref="hotelDao">

        </property>
    </bean>
</beans>
import com.imooc.spring.jdbc.dao.HotelDao;
import com.imooc.spring.jdbc.entity.Hotel;
import com.imooc.spring.jdbc.service.HotelService;
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.Date;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class JdbcTemplateTestor {
    @Resource
    private HotelService hotelService;
    @Resource
    private HotelDao hotelDao;
    @Test
    public void testFindById(){
        Hotel hotel = hotelDao.findById(10001);
        System.out.println(hotel);

    }
    @Test
    public void testFindByName(){
        List<Hotel> 上海 = hotelDao.findByCity("上海");
        System.out.println(上海);

    }
    @Test
    public  void testInsert(){
        Hotel hotel=new Hotel();
        hotel.setOrderNo(10099);
        hotel.setCity("长沙");
        hotel.setHotelName("酒店8");
        hotel.setArriveDate(new Date());
        hotel.setLeaveDate(new Date());
        hotel.setPrice(999.98f);
        int count = hotelDao.insert(hotel);
        System.out.println(count);
    }
    @Test
    public void testUpdate() throws ParseException {
        String date1="2020-4-30";
        String data2="2020-5-05";
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        Hotel hotel = hotelDao.findById(10003);
        hotel.setLeaveDate(simpleDateFormat.parse(date1));
        hotel.setArriveDate(simpleDateFormat.parse(data2));
        int count = hotelDao.alter(hotel);
        System.out.println(count);
    }
    @Test
    public  void testDelete(){
        int count = hotelDao.delete(10099);
        System.out.println(count);
    }
    @Test
    public  void testBatchImport(){
        hotelService.batchImport();
        System.out.println("批量插入成功");

    }
    }


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小脸 2021-11-05 17:21:36

同学你好,

1、上述贴出代码是可以的,非常棒,继续加油!

2、建议同学定义变量名时使用英文,尽可能的不去使用中文呐~

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师