pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.4
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
2,连接数据库
application.properties
server.port=8082
spring.datasource.url=jdbc:mysql://localhost:3306/ssm_db?serverTimezone=GMT%2B8&characterEncoding-utf-8&useSSL=false
spring.datasource.username=root //mysql用户名
spring.datasource.password=root //mysql密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
3,编写实体类
创建entity.user
package com.example.demo.entity;
//实体类
public class user {
private Integer commodity_Id;
private String name;
private Double price;
private String creation_Time;
private String picture;
private Integer id;
private Integer commodityType;
private Integer commoditySate;
public Integer getCommodity_Id() {
return commodity_Id;
}
public void setCommodity_Id(Integer commodity_Id) {
this.commodity_Id = commodity_Id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCreation_Time() {
return creation_Time;
}
public void setCreation_Time(String creation_Time) {
this.creation_Time = creation_Time;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCommodityType() {
return commodityType;
}
public void setCommodityType(Integer commodityType) {
this.commodityType = commodityType;
}
public Integer getCommoditySate() {
return commoditySate;
}
public void setCommoditySate(Integer commoditySate) {
this.commoditySate = commoditySate;
}
}
4,编写控制层
创建controller.UserController
package com.example.demo.controller;
import com.example.demo.entity.user;
import com.example.demo.mapper.userMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
//解决跨域问题
@CrossOrigin(origins = {"*", "null"})
@RestController
@RequestMapping("/phone")
public class UserController {
@Resource
userMapper userMapper;
@GetMapping
public List getUser(){
return userMapper.findAll();
}
@PostMapping
public String addUser(@RequestBody user phone){
userMapper.save(phone);
return "添加成功";
}
@PutMapping
public String updateUser(@RequestBody user phone) {
userMapper.updateById(phone);
return "更新成功";
}
}
5,编写持久层
创建mapper.userMapper
package com.example.demo.mapper;
import com.example.demo.entity.user;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface userMapper {
@Select("Select * from phone")
List findAll();
//INSERT INTO `phone`(`commodity_Id`, `name`, `price`, `creation_Time`, `picture`, `id`, `commodityType`, `commoditySate`) VALUES (10, '手机01', 2000.00, '2022-03-13 22:20:51', 'http://www.kaotop.com/file/tupian/20220427/s3.bmp.ovh/imgs/2022/03/cb452b6f8d9bef72.jpg', 1, NULL, NULL);
@Update("INSERT INTO `phone`( `name`, `price`, `creation_Time`, `picture`, `id`, `commodityType`, `commoditySate`) VALUES (#{name},#{price},#{creation_Time},#{picture},#{id},#{commodityType},#{commoditySate});")
@Transactional
// 事物注解
void save(user phone);
//UPDATE `ssm_db`.`phone` SET `name` = '手机01', `price` = 2000.00, `creation_Time` = '2022-03-13 22:20:51', `picture` = 'http://www.kaotop.com/file/tupian/20220427/s3.bmp.ovh/imgs/2022/03/cb452b6f8d9bef72.jpg', `id` = 1, `commodityType` = NULL, `commoditySate` = NULL WHERE `commodity_Id` = 10;
@Update("Update phone SET name = #{name},price = #{price},creation_Time = #{creation_Time}, picture= #{picture},id=#{id},commodityType=#{commodityType},commoditySate = #{commoditySate} WHERE commodity_Id = #{commodity_Id}")
@Transactional
void updateById(user phone);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)