怎么进入企业的网站,广西建设局建设行政主管部网站,wordpress 删除评论,电商小程序定制开发环境准备#xff1a; java集成开发环境#xff1a;IDEA 数据库#xff1a;Mysql Maven
最好在安装有个navicat#xff08;数据库可视化界面#xff09; 安装好上述几个软件后 总结下#xff1a;五步 1、创建新的工程 2、创建建applicatiom.yml 3、创建entity层 4、创建r…环境准备 java集成开发环境IDEA 数据库Mysql Maven
最好在安装有个navicat数据库可视化界面 安装好上述几个软件后 总结下五步 1、创建新的工程 2、创建建applicatiom.yml 3、创建entity层 4、创建respository层 5、创建Controller层 1、创建新的工程
给定工程名以及包名
2、选中web,jdbc,jpa,mysql四个包依赖 spring boot版本我选择的是2.2.2
确定有一下四个后点击下一步next 2、创建建applicatiom.yml
原项目 新建后applicatiom.yml文件
填入下面配置代码
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/学生管理系统?useUnicodetrueuseJDBCCompliantTimezoneShifttrueuseLegacyDatetimeCodefalseserverTimezoneUTCdriver-class-name: com.mysql.cj.jdbc.Driverjpa:hibernate:ddl-auto: updateshow-sql: true其中数据库“学生管理系统“需要你提前在数据库创建好你也可以改成你要连接的数据库
3、创建entity层
在xzy.1998.springboot创建同目录class文件
填入下列代码
package xyz.k1998.springboot.entity;import javax.persistence.*;Entity
public class Student {IdGeneratedValue(strategy GenerationType.IDENTITY)private Integer id;Columnprivate String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}
}
4、创建respository层 填入下列代码
package xyz.k1998.springboot.entity.repository;import org.springframework.data.jpa.repository.JpaRepository;
import xyz.k1998.springboot.entity.Student;public interface StudentRepository extends JpaRepositoryStudent,Integer {}
如果有包未导入 直接AltEnter即可
第四步结束后主程序运行就可以直接创建一个数据库的表单 字段什么都是Student的映射 如下图 现在来一些简单的增删改查利用controller
5、创建Controller层 这就是最后的项目结构了比较一下是否一致
然后在StudentController里填入下列代码
package pringboot.controller;import pringboot.entity.Student;
import pringboot.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;RestController
public class StudentController {AutowiredStudentRepository studentRepository;GetMapping(/student/{id})public Student getStudent(PathVariable(id) Integer id){Student student (Student) studentRepository.findById(id).get();return student;}GetMapping(/student)public Student insertUser(Student student){Student save studentRepository.save(student);return save;}}
这样再次运行主程序我们就可以向数据库插入或查询数据了
在网址输入 http://localhost:8080/student?username氪金password123456 返回 在到navicat去刷新看看 就有了新的数据且id自动给你添加是不是很方便
查询的话 输入 后面填入你需要查的id号即可
看到这你就可以做一个注册的页面了 有没有发现form表单提交和插入输入的地址是一样的
这样我们做一个注册的静态页面
!DOCTYPE html
html langen
headmeta charsetUTF-8title注册/title
/head
body
form action/studentinput nameusernamebrinput typepassword namepasswordbrinput typesubmit/form/body
/html提交后数据库就多了一条数据 这样注册页面就算大功告成了
后面写登录页面 添加可跳转页面的thymeleaf包
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency