gpt4 book ai didi

mysql - Spring Boot从没有实体的表中读取,编辑并保存在另一个表中

转载 作者:行者123 更新时间:2023-11-29 15:34:34 31 4
gpt4 key购买 nike

在我的系统中,我需要从表 userinout 中读取(只有表没有实体,DAO 等..)。然后将这些数据发送到 DateToDayConvert 类并将更改的数据保存在记录表中(有记录模型、DAo 和 Controller )。我对 spring boot 不太熟悉,所以我不知道如何在没有 Entity.Thx 的情况下从数据库读取数据

日期到日转换

public class DateToDayConvert {
public static void main(String[] args) throws ParseException {

//1. Create a Date from String
for (int i=0;i<args.length;i++) {
System.out.println("i"+args[i]);

}
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "22-01-2015 10:20:56";
Date date = sdf.parse(dateInString);
DateToDayConvert obj = new DateToDayConvert();

//2. Test - Convert Date to Calendar
Calendar calendar = obj.dateToCalendar(date);
System.out.println(calendar.getTime());

//3. Test - Convert Calendar to Date
String newDate = obj.calendarToDate(calendar);
System.out.println(newDate);


}


//Convert Date to Calendar
private Calendar dateToCalendar(Date date) {

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;

}

//Convert Calendar to Day
private String calendarToDate(Calendar calendar) {
int day= calendar.getTime().getDay();
int time=calendar.getTime().getHours();
System.out.println(time);
switch(day) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Satarday";
default:
return "Sunday";
}
}

}

记录模型

@Entity
@Table(name="records")
@EntityListeners(AuditingEntityListener.class)

public class Records {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int userId;
private String day;
private int time;


public Records() {
}

public Records(int userId, String day, int time) {
this.userId = userId;
this.day = day;
this.time = time;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getDay() {
return day;
}

public void setDay(String day) {
this.day = day;
}

public int getTime() {
return time;
}

public void setTime(int time) {
this.time = time;
}
}

Checkinout

records

最佳答案

@Configuration
public class DataSourceConfiguration {

@Autowired
Environment environment;

private final String URL = "url";
private final String USER = "dbuser";
private final String DRIVER = "driver";
private final String PASSWORD = "dbpassword";

@Bean
DataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setUrl(environment.getProperty(URL));
driverManagerDataSource.setUsername(environment.getProperty(USER));
driverManagerDataSource.setPassword(environment.getProperty(PASSWORD));
driverManagerDataSource.setDriverClassName(environment.getProperty(DRIVER));
return driverManagerDataSource;
}

@Bean
@Autowired
JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

您现在可以使用 JdbcTemplate 来触发查询并获取所需的列。

@Autowired
private JdbcTemplate jdbcTemplate;

关于mysql - Spring Boot从没有实体的表中读取,编辑并保存在另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58373683/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com