不知道如何获取我的下拉菜单的值

s5a0g9ez  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(218)

花了几个小时想弄明白这件事,我是否需要获取值,也就是所选制造商的制造商id值,从那里我可以用自动递增的id制造商id创建一个新的自行车对象,然后我只需要知道如何从下拉列表中获取id值,以便执行其他操作,如更新和删除
html文件

<form method="post" action="" th:action="@{/insertBike}" th:object="${bike}">
    <input type="hidden" name="bikeID" th:field="*{bikeID}"/>

Model: <input type="text" name="modelname" th:field="*{modelname}"/><br>

Year: <input type="number" name="year" th:field="*{year}"/><br>

Colour: <input type="text" name="colour" th:field="*{colour}"/><br>

Price: <input type="price" name="price" th:field="*{price}"/><br>

<input type="submit" value="Go!"/>

控制器类:

@GetMapping("/insert")
public String insert(Model model)
{
    model.addAttribute("manufacturer", new Manufacturer());
    model.addAttribute("manufacturerList", da.getManufacturers());

    model.addAttribute("bike", new Bike());
    model.addAttribute("bikeList", da.getBikes());

    return"insert";
}

@PostMapping("/insertBike")
public String insertBike(Model model, @RequestParam int manufacturerID, @RequestParam String 
    modelname, @RequestParam int year, @RequestParam String colour, @RequestParam double price) 
{  
    da.insertBike(manufacturerID, modelname, year, colour, price);

    model.addAttribute("manufacturer", new Manufacturer());
    model.addAttribute("manufacturerList", da.getManufacturers());

    model.addAttribute("bike", new Bike());
    model.addAttribute("bikeList", da.getBikes());

    return"insert";
}

sql架构

CREATE TABLE manufacturer (

manufacturerID int NOT NULL AUTO_INCREMENT,

manufacturer varchar(30) NOT NULL,

PRIMARY KEY (manufacturerID)

);

CREATE TABLE bike (

bikeID int NOT NULL AUTO_INCREMENT,

manufacturerID int NOT NULL,

model VARCHAR(30) NOT NULL,

year int NOT NULL,

colour VARCHAR(30) NOT NULL,

price decimal(7,2) NOT NULL,

PRIMARY KEY (bikeID),

FOREIGN KEY (manufacturerID) REFERENCES manufacturer(manufacturerID)

);

制造商等级:

import java.io.Serializable;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor

public class Manufacturer implements Serializable
{
    private int id;
    private String manufacturer;
}

自行车.java

import java.io.Serializable;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

@Data
@NoArgsConstructor
public class Bike implements Serializable
{
    @NonNull
    private Long bikeID;

    @NonNull
    private int manufactureID;

    @NonNull
    private String modelname;

    @NonNull
    private int year;

    @NonNull
    private String colour;

    @NonNull
    private double price;

}

数据访问.java

public List<Manufacturer>getManufacturers()
{
    MapSqlParameterSource namedParameters = new MapSqlParameterSource();
    String query = "SELECT * FROM MANUFACTURER ";

    return jdbc.query(query,namedParameters, new BeanPropertyRowMapper<Manufacturer 
    (Manufacturer.class));
}

public void insertBike(int manufacturerID, String model, int year, String colour, double price) 
{
    MapSqlParameterSource namedParameters = new MapSqlParameterSource();

    String query = "INSERT INTO bike(manufacturerID, model, year, colour, price) 
    VALUES(:manufacturerID, :model, :year, :colour, :price)";

    namedParameters.addValue("manufacturerID", manufacturerID);
    namedParameters.addValue("model", model);
    namedParameters.addValue("year", year);
    namedParameters.addValue("colour", colour);
    namedParameters.addValue("price", price);
    int rowsAffected = jdbc.update(query, namedParameters);

    if(rowsAffected > 0) 
    {
        System.out.println("Inserted student into database");
    }
}

public List<Bike>getBikes()
{
    MapSqlParameterSource namedParameters = new MapSqlParameterSource();
    String query = "SELECT * FROM bike ";

    return jdbc.query(query,namedParameters, new BeanPropertyRowMapper<Bike>(Bike.class));
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题