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

时间:2019-10-14 09:21:07

标签: mysql hibernate spring-boot thymeleaf

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

的情况下从数据库中读取数据。

DateToDayConvert

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

1 个答案:

答案 0 :(得分:0)

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