Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
我们使用maven来新建项目,首先,新建一个空白的web项目
然后添加相关依赖
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目录下新建如下目录和文件
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
其中,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页面:
showstudent.html: (要引用thymeleaf模板,html必须加入: 代码)
Getting Started: Serving Web Contentidnameageeditdelete
edit deleteeditstudent.html:
Getting Started: Serving Web Content请输入姓名请输入年龄
student.html:
Getting Started: Serving Web Content最后,我们更新一下项目:
按 ALT+F5,选中当前项目,点击OK。
然后,到我们之前编写的启动类Application.java中运行项目:右击-->run as-->spring boot App/java application
控制台出现如上效果,证明项目运行成功
浏览器输入网址:http://localhost:8080/student/studentlist:
添加:
删除:
修改:zhangsan改为lisi
根据id查询:
本人也是小白,也是最近才开始研究Spring Boot,如有写错或者不详细的地方,欢迎指正!