连接mysql
idea中增加数据库相关操作
- 1、修改配置文件:application.properties,习惯用application.yml作为配置文件也可以,两种配置文件语法不同
需要注意的是,spring.datasource.url参数连接数据库的地址,设置成自己数据库的地址。spring.jpa.hibernate.ddl-auto的参数使用create,等调试完成后改成update,因为create模式在每次启动工程时,都会删除数据库中的表进行重建,用于调试比较合适。
#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo_db?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jpa配置
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
-
2、在工程中新建需要用到的类
entity包中新建实体类:StuInfo
dao包中新建操作数据库的接口类:StuInfoDao
service包中新建接口类及实现类:StuInfoService、StuInfoServiceImpl
controllor包中新建controllor:StuInfoControllor
-
3、新建实体类StuInfo:
使用@Data注解,省略constructor、setter、getter等代码;
使用@Entity注解,标注该类做为实体类使用;
使用@Table注解,标注该类在数据库中存在对应的表(name对应表名,会显示报错的划线,不影响代码,如果觉得不舒服,可以在idea中连接数据库)
@Data
@Entity
@Table(name = "stuinfo")
public class StuInfo{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;//id,键值,自增
private int stuid;//学生id
private String name;//姓名
private int gender;//性别,1-男,0-女
private int age;//年龄
private int grade_num;//年级
private int class_num;//班级
private int status;//状态,1-数据有效,0-数据删除
private Timestamp createtime;//创建时间
private Timestamp updatetime;//更新时间
}
- 4、新建数据库操作的dao类:StuInfoDao
使用注解@Repository,引入jpa,可以省略一些基础的sql操作。
@Repository
public interface StuInfoDao extends JpaRepository<StuInfo, Long> {
}
- 5、新建service类,以及实现类
StuInfoService:先只写增加和删除的方法。
public interface StuInfoService {
StuInfo save(StuInfo stuInfo);
Boolean deleteById(Long id);
}
实现类:StuInfoServiceImpl,需要使用注解@Service,否则springboot在启动后,程序在注入service的类时,找不到实现类,会报错。
service实现类中,引用dao中的save方法,但是在dao接口中并没有写相关的实现,这个方法jpa已经做了实现,直接调用即可。
@Service
public class StuInfoServiceImpl implements StuInfoService {
@Autowired
private StuInfoDao stuInfoDao;
@Override
public StuInfo save(StuInfo stuInfo) {
stuInfo.setStatus(1);
stuInfo.setCreatetime(new Timestamp(System.currentTimeMillis()));
stuInfo.setUpdatetime(new Timestamp(System.currentTimeMillis()));
return stuInfoDao.save(stuInfo);
}
@Override
public Boolean deleteById(Long id) {
if (stuInfoDao.existsById(id)) {
Optional<StuInfo> stuInfoOptional = stuInfoDao.findById(id);
StuInfo stuInfo = stuInfoOptional.get();
stuInfo.setStatus(0);
stuInfo.setUpdatetime(new Timestamp(System.currentTimeMillis()));
stuInfoDao.save(stuInfo);
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
}
- 6、新建StuInfoControllor类
post请求中,接口方法入参,需要添加@RequestBody注解,否则调用接口的时候,会出现json参数转换失败的错误。
@RestController
@RequestMapping("/stu")
public class StuInfoControllor {
@Autowired
private StuInfoService stuInfoService;
@PostMapping("/addStu")
public Response<Boolean> addStu(@RequestBody StuInfo stuInfo) {
if (stuInfoService.save(stuInfo).getId() != 0) {
return new Response<>(200, "success", Boolean.TRUE);
} else {
return new Response<>(200, "failure", Boolean.FALSE);
}
}
@GetMapping("/delStuById")
public Response<Boolean> delStuById(@RequestParam Long id) {
if (stuInfoService.deleteById(id)) {
return new Response<>(200, "success", Boolean.TRUE);
} else {
return new Response<>(200, "failure", Boolean.FALSE);
}
}
}
- 7、修改类DemoApplication中的注解@SpringBootApplication,去掉(exclude = {DataSourceAutoConfiguration.class})。右键Run类DemoApplication,启动工程。
调用接口并登录mysql进行验证
-
1、使用navicat连接本地mysql,登录数据库demo_db,查看工程启动后新建的stuinfo表。
-
2、使用postman调用接口发送post请求:http://localhost:8080/stu/addStu,
headers中增加content-type:application/json;charset=utf-8
body中使用raw模式进行参数传递。
{
"stuid":10001,
"name":"小红",
"gender":0,
"age":7,
"gradeNum":1,
"classNum":1
}
- 3、步骤2点击send发送请求后,查看表中数据
- 4、使用postman发送get请求:http://localhost:8080/stu/delStuById?id=2
- 5、步骤4点击send发送请求后,查看表中数据,status由1变为0,状态置为删除