springboot启动更改mysql_Spring Boot+Mysql 入门 +简单的增删改查

本文介绍了如何使用Spring Boot创建一个简单的Web应用,集成MySQL数据库进行增删改查操作。通过Spring Data JPA和Thymeleaf模板引擎实现 CURD 功能,同时展示了配置文件、目录结构以及关键代码。
摘要由CSDN通过智能技术生成

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

我们使用maven来新建项目,首先,新建一个空白的web项目

74278017c202b07a2020cce0bfe49b60.png

然后添加相关依赖

4.0.0

com.ziv.test

MySpringboot

war

0.0.1-SNAPSHOT

MySpringboot Maven Webapp

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-freemarker

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-maven-plugin

这里使用spring-data-jpa来操作数据库,Spring Boot 不推荐使用jsp作前端页面渲染,这里我们使用thymeleaf作模板引擎。

在resources目录下新建如下目录和文件

6b1aa01a4eed89f7e5638685ddea2507.png

static用来存放静态资源,比如css/js等,templates用来存放html等资源。

application.yml是项目中最重要的文件,有了它,我们才不用编写如springmvc那种多而杂的配置,如下

spring:

datasource:

url: jdbc:mysql://localhost:3306/zz?characterEncoding=utf-8

username: root

password: root

driver-class-name: com.mysql.jdbc.Driver

jpa:

database-platform: org.hibernate.dialect.MySQL5Dialect

hibernate:

ddl-auto: update

show-sql: truethymeleaf:

prefix: classpath:/templates/logging:

file: F:/mylog/log.log

level:

org:

springframework:

web: DEBUG

看,就这么些,就完成了mysql,jpa,thyeleaf和log的配置,相比于springmvc,算了。。。

接下来在src/maim./java中加入如下目录和文件,如同springmvc

780f9abaa6423950e713a6fd9e60fca5.png

其中,Application.java文件是Spring Boot项目的启动类,必须加上@SpringBootApplication注解

packagecom.m2;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.builder.SpringApplicationBuilder;importorg.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplicationpublic class Application extendsSpringBootServletInitializer {/** @RequestMapping("/") public String greeting() { return "Hello World!"; }*/

protectedSpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);

}public static voidmain(String[] args) {

SpringApplication.run(Application.class, args);

}

}

model

packagecom.m2.model;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.Table;

@Table(name="Student")

@Entitypublic classStudent {

@Id

@GeneratedValueprivate intid;

@Column(nullable= false, unique = true)privateString name;

@Column(nullable= false)private intage;public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}

}

Dao(还不熟悉spring-data-jpa的同学,可以先去了解一下)

packagecom.m2.dao;importorg.springframework.data.jpa.repository.JpaRepository;importcom.m2.model.Student;public interface StudentRepository extends JpaRepository{

}

Service

packagecom.m2.service;importjava.util.List;importcom.m2.model.Student;public interfaceStudentService {

Student findUserById(intid);

ListfindAll();void deleteById(intid) ;voidsave(Student student) ;voidupdatestudent(Student student);

}

packagecom.m2.service.serviceImpl;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.m2.dao.StudentRepository;importcom.m2.model.Student;importcom.m2.service.StudentService;

@Servicepublic class StudentServiceImpl implementsStudentService{

@Autowired

StudentRepository studentRepository;

@Overridepublic Student findUserById(intid) {//TODO Auto-generated method stub

returnstudentRepository.findOne(id);

}

@Overridepublic ListfindAll() {//TODO Auto-generated method stub

returnstudentRepository.findAll();

}

@Overridepublic void deleteById(intid) {//TODO Auto-generated method stub

studentRepository.delete(id);

}

@Overridepublic voidsave(Student student) {

studentRepository.save(student);

}

@Overridepublic voidupdatestudent(Student student) {//TODO Auto-generated method stub

studentRepository.save(student);

}

}

Controller

packagecom.m2.Controller;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importcom.m2.model.Student;importcom.m2.service.StudentService;

@Controller

@RequestMapping("/student")public classStudentController {

@Autowired

StudentService studentService;

@RequestMapping("/findbyid")

@ResponseBodypublic String findbyid(intid) {

Student student=studentService.findUserById(id);returnstudent.getName();

}

@RequestMapping("/studentlist")publicString findalldata(Model model) {

List studentlist=studentService.findAll();

model.addAttribute("studentlist", studentlist);return "showstudent";

}

@RequestMapping("/delectbyid")public String delectbyid(intid) {

studentService.deleteById(id);return "redirect:/student/studentlist";

}

@RequestMapping("/savestudent")publicString savestudent(Student student) {

studentService.save(student);return "redirect:/student/studentlist";

}

@RequestMapping("/toupdatestudent")public String toupdatestudent(intid,Model model) {

Student student=studentService.findUserById(id);

model.addAttribute("student", student);return "editstudent";

}

@RequestMapping("/updatestudent")publicString updatestudent(Student student) {

studentService.updatestudent(student);return "redirect:/student/studentlist";

}

}

至此,简单的CURD就已经完成了。

然后,我们编写前台页面,在templates目录下新建如下html页面:

e8713f766dde046d7c554b37ab9fff41.png

showstudent.html: (要引用thymeleaf模板,html必须加入:  代码)

Getting Started: Serving Web Content
请输入id

idnameageeditdelete

edit delete
请输入姓名请输入年龄

editstudent.html:

Getting Started: Serving Web Content

请输入姓名请输入年龄

student.html:

Getting Started: Serving Web Content

最后,我们更新一下项目:

按 ALT+F5,选中当前项目,点击OK。

489a46dd2cbadfcec2b7654faa192326.png

然后,到我们之前编写的启动类Application.java中运行项目:右击-->run as-->spring boot App/java application

22e4e8a1a54f158e214130f1d3e88747.png

dd218c9ba855e0f5b2292ad7e6b96375.png控制台出现如上效果,证明项目运行成功

浏览器输入网址:http://localhost:8080/student/studentlist:

21b79878a7d556f2d045c1b40674f14b.png

添加:

61ad79dca5a8810b82780e5319411cde.png

f57f895c430d7094f3f55829289fe6eb.png

删除:

ed99e2221ec5e3de9008a78c55bc3ed4.png

修改:zhangsan改为lisi

293e8c38d9109666159460dcd1eea50d.png

aecde77cfb7f9d112f1bd5686dc548ac.png

根据id查询:

8c2b0652f3411d73d7af67df5b24b001.png

1058c4e2d6f8a2699938388219723b75.png

本人也是小白,也是最近才开始研究Spring Boot,如有写错或者不详细的地方,欢迎指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值