springboot(二)

SpringBoot使用JSP

1.创建一个webapp子项目

2.导入依赖

<!-- 
  配置springboot的父节点依赖,之后引入就不需要添加version配置了!
 springboot会自动选择最合适的版本进行添加依赖
 -->
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.1.RELEASE</version>
  </parent>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--指定JDK版本 我们使用的是1.8,不配置默认环境是1.6  -->
    <java.version>1.8</java.version>
  </properties>
  
  
  <dependencies>
       <dependency>
    <!-- spring-boot-starter-web是为我们提供了包括mvc,aop等需要的一些jar -->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <!-- 因为我们已经配置了 parent 中的version 所以这里不需要指定version了  -->
    </dependency>
    
        <!-- Spring-boot-devtools -->
    <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-devtools</artifactId>  
            <optional>true</optional>  
   </dependency>  
   <!-- 配置servlet需要的依赖  -->
     <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>javax.servlet-api</artifactId>  
   </dependency>  
   <!-- 配置jstl标签库需要的依赖  -->
     <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>jstl</artifactId>  
   </dependency> 
   
   <!-- 添加tomcat的支持 -->
   <dependency>  
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
   </dependency> 
   <dependency>  
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
   </dependency> 
  </dependencies>

3.创建一个application.yml文件

spring:
  mvc:
    view:
      suffix: .jsp
      prefix: /jsp/

4.编辑Controller

@Controller
@RequestMapping("/jsp")
public class JSPController {
    @RequestMapping("/first")
    public String jsp(Model model){
        model.addAttribute("name","张三");
        return "index";
    }
}

5.创建一个jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hehe</title>
</head>
<body>
<h2>呵呵,欢迎:${name}1234</h2>
</body>
</html>

6.启动运行

 SpringBoot使用thymeleaf

1.pom文件中增加thymeleaf的依赖和版本号

 <!-- 添加thymeleaf模版的依赖 -->

     <dependency>

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-thymeleaf</artifactId>  

   </dependency>

<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>

<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>

2.application.yml文件中新增thymeleaf配置

 3.在resources中创建templates文件夹

并且创建一个index.html

 

 

 

4.创建一个entity  Student实体类

public class Student {
    private Integer id;
    private String name;
}

5.编写Controller控制器

@Controller
public class thyController {
    @RequestMapping("/thy")
    public String thy(Model model){
        List<Student> list = new ArrayList<>();
        Student stu1 = new Student(1,"张三");
        Student stu2 = new Student(2,"李四");
        Student stu3 = new Student(3,"王大爷是弟弟,垃圾");

        list.add(stu1);
        list.add(stu2);
        list.add(stu3);

        model.addAttribute("stulist",list);
        return "index";
    }
}

6.书写index.html中的代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>temples</title>
</head>
<body>
<table border="1">
    <tr>
        <td>学生编号</td>
        <td>学生姓名</td>
    </tr>
    <tr th:each="stu:${stulist}">
        <td th:text="${stu.id}"></td>
        <td th:text="${stu.name}"></td>
    </tr>
</table>
</body>
</html>

7.运行效果

SpringBoot JPA

1. 实现数据的增删改查

  1.1:在pom文件中引入需要的依赖

    

  <!--  引入mysql需要的依赖  -->

    <dependency>  

            <groupId>mysql</groupId>  

            <artifactId>mysql-connector-java</artifactId>  

   </dependency>  

    <!--  引入spring data jpa需要的依赖  -->

    <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-data-jpa</artifactId>  

   </dependency>

2.创建resources文件夹创建application.yml文件进行编写

##数据库四大连接参数
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///springdatajpa
    username: root
    password: 123456
##jpa表的生成策略
  jpa:
    hibernate:
      ddl-auto: update

3.编写entity

@Entity
public class Grade {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer grade_id;
    private String grade_name;

    public Integer getGrade_id() {
        return grade_id;
    }

    public void setGrade_id(Integer grade_id) {
        this.grade_id = grade_id;
    }

    public String getGrade_name() {
        return grade_name;
    }

    public void setGrade_name(String grade_name) {
        this.grade_name = grade_name;
    }
}

4.编写dao层

/**
 * CrudRepository<实体,主键数据类型>
 */
@Repository
public interface IGradeMapper extends CrudRepository<Grade,Integer> {
}

5.编写增删改查的方法service

public interface IGradeService {
    public Grade insertGrade(Grade grade);
    
    public Grade updateGrade(Grade grade);

    public void deleteGrade(Integer id);
    
    public Iterable<Grade> getAll();
}

6.编写实现类

@Service("iGradeService")
public class IGradeServcieImpl implements IGradeService {
    @Resource
    private IGradeMapper iGradeMapper;
    
    @Override
    public Grade insertGrade(Grade grade) {
        return iGradeMapper.save(grade);
    }

    @Override
    public Grade updateGrade(Grade grade) {
        return iGradeMapper.save(grade);
    }

    @Override
    public void deleteGrade(Integer id) {
        iGradeMapper.delete(id);
    }
    @Override
    public Iterable<Grade> getAll() {
        return iGradeMapper.findAll();
    }
}

7.创建Controller层进行编写

@RestController
@RequestMapping("/IGradeController")
public class IGradeController {
    @Resource
    private IGradeService iGradeService;
    
    //新增数据
    @RequestMapping("/insertGrade")
    public Grade insertGrade(){
        Grade grade=new Grade();
        grade.setGrade_name("Y2");
        return iGradeService.insertGrade(grade);
    }
  //修改数据
    @RequestMapping("/updateGrade")
    public Grade updateGrade(){
        Grade grade=new Grade();
        grade.setGrade_id(2);
        grade.setGrade_name("S3");
        return iGradeService.updateGrade(grade);
    }
  //删除数据
    @RequestMapping("/deleteGrade")
    public void deleteGrade(){
        iGradeService.deleteGrade(1);
    }
    //查看数据
    @RequestMapping("/getAllGrade")
    public Iterable<Grade> getAllGrade(){
       return  iGradeService.getAll();
    }
}

8.运行查看效果

 

 这里已经添加成功了我们可以看一下数据库

 

现在我们来看一下修改

 

 可以看到现在编号为3的数据值为S2

访问http://localhost:8080/IGradeController/updateGrade

 

 

 

 

 接下来进行删除

访问http://localhost:8080/IGradeController/deleteGrade

 

 

 

 接下来进行查看

posted @ 2019-12-12 15:38  琴昕LNS~  阅读(165)  评论(0编辑  收藏  举报