自由编程,批量插入
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&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&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("批量插入成功");
}
}20
收起
正在回答 回答被采纳积分+1
2023版Java工程师
- 参与学习 人
- 提交作业 8789 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星