我已经创建了springboot应用程序。我想将用户输入的数据保存在数据库表“ orders”中(在PostgreSQL中)
我已经使用百里香叶创建了icecream.html文件。而且我使用script.js文件进行集成。
IcecreamDao.java
@Repository
public class IcecreamDao {
@Autowired
private JdbcTemplate jdbcTemplate;
private static final String SQL2 = "INSERT INTO orders (id, name, price, qty, total, cname) VALUES (:id, :name ,:price, :qty, :total, :cname)";
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
// To create new record
public Icecream saveIcecream(Icecream icecream) {
KeyHolder holder = new GeneratedKeyHolder();
SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("id", icecream.getid())
.addValue("cname", icecream.getCName())
.addValue("name", icecream.getName())
.addValue("price", icecream.getPrice())
.addValue("qty" , icecream.getQty())
.addValue("total", icecream.getTotal());
namedParameterJdbcTemplate.update(SQL2, parameters, holder);
icecream.setid(holder.getKey().intValue());
return icecream;
}
}
IcecreamController.java
@Controller
@RequestMapping(value = "/")
public class IcecreamController {
@Autowired
public IcecreamDao icecreamDao;
@RequestMapping(value = "/", produces="application/json", method = RequestMethod.POST)
public ResponseEntity<?> createUser(@RequestBody Icecream icecream, UriComponentsBuilder ucBuilder) {
icecreamDao.saveIcecream(icecream);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/{id}").buildAndExpand(icecream.getid()).toUri());
return new ResponseEntity<List>(headers, HttpStatus.CREATED);
}
}
script.js
function updateClick() {
var icecream = {id:0 , name:"" , cname: "", price: 0, qty: 0, total: 0 }
icecream.id = $("#id").val();
icecream.cname = $("#cname").val();
icecream.name = $("#name").val();
icecream.price = $("#price").val();
icecream.qty = $("#qty").val();
icecream.total = $("#total").val();
productAdd(icecream);
}
function productAdd(icecream) {
$.ajax({
url: "/",
type: 'POST',
contentType: "application/json",
data: JSON.stringify(icecream),
success: function (icecream)
{
productAddSuccess(icecream);
},
error: function (request, message, error) {
handleException(request, message, error);
}
});
}
function productAddSuccess(icecream) {
productAddRow(icecream);
formClear();
}
function formClear() {
$("#id").val("");
$("#cname").val("");
$("#name").val("");
$("#price").val("");
$("#qty").val("");
$("#total").val("");
}
function addClick() {
formClear();
}
icecream.html
<body>
<form>
<fieldset>
<div>
<h1> <u>Order Your Ice-cream</u> </h1>
<label for="id">Order Id:</label>
<input type="text" id="id" value="Please Enter Your order id "> <br> <br>
<label for="cname">Customer Name :</label>
<input type="text" id="cname" value="Please Enter Your Name"> <br> <br>
<label for="name">Ice-Cream Name :</label> <select id="name" onchange="pickicecream()">
<option>Select Ice_Cream</option>
<option>Vanilla Icecream</option>
<option>Chocolate Icecream</option>
<option>Blueberry Icecream</option>
<option>CheeseCake with Waffle</option>
</select> <br> <br>
<label for="price">Price :</label>
<input type="text" id="price" value="0"> <br> <br>
<label for="qty">Quantity :</label>
<!-- Increment Button -->
<div class="value-button" id="increase">+</div>
<input type="number" id="qty" value="0" onclick="calTotal()" />
<div class="value-button" id="decrease">-</div>
<p> Total Amount : <input type="text" id="total" value="0"> </p>
<button type="submit" id="saveButton" value="Submit" onclick="updateClick()">Submit</button> <br> <br>
Click Here To See <a th:href="@{orders/view}">Your Orders </a> <br> <br>
</div>
</fieldset>
</form>
</body>
</html>
所有用户输入的数据必须保存在我的表“ orders”中。但它没有节省。请有人指导我代码中出了什么问题...预先感谢。