zoukankan      html  css  js  c++  java
  • SpringBootMVC02——Spring Data JPA的使用&JSP的使用

    Spring Data JPA的使用

    实体层

    package com.littlepage.domain;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="city")
    public class City {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
        private String name;
        public City() {
            super();
        }
        public City(Integer id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "City [id=" + id + ", name=" + name + "]";
        }
    }

    Dao层只需要继承JPARepository就可以

    package com.littlepage.dao;
    
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import com.littlepage.domain.City;
    
    public interface CityRepository extends JpaRepository<City, Integer> {
    
    }

    Service层的增删改查操作

    package com.littlepage.service;
    
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.littlepage.dao.CityRepository;
    import com.littlepage.domain.City;
    
    @Service
    public class CityService {
        @Autowired
        CityRepository cityDao;
    
        public List<City> findAll(){
            return cityDao.findAll();
        }
        
        public String add(City city) {
            if(cityDao.existsById(city.getId())) {
                return "添加失败";
            }
            cityDao.save(city);
            return "添加成功";
        }
    
        public String remove(Integer id) {
            if(cityDao.existsById(id)) {
                cityDao.deleteById(id);;
                return "删除成功";
            }
            return "不存在";
        }
    
        public String modify(City city) {
            if(!cityDao.existsById(city.getId())) {
                return "不存在";
            }
            cityDao.save(city);
            return "修改成功";
        }
    }

    application.properties

    spring.datasource.url=jdbc:mysql://120.78.206.78:3306/world
    spring.datasource.username=root
    spring.datasource.password=Root123..

    JSP的使用

    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
    <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.21</version>
    </dependency>

    添加依赖jasper和jstl

    SpringBoot默认从main下寻址,在application.properties里面配一下前缀后缀

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp

    jsp中可以用jstl表达式取出scope域的内容

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    
    
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    </head>
    <body>
    
    <table>
    
        <tr>
            <th>id</th>
            <th>name</th>
        </tr>
        
        <c:forEach items="${list}" var="item">
        
        <tr>
                <td>${item.id }</td>
                <td>${item.name }</td>
            
            </tr>
        
        </c:forEach>
        
    </table>
    </body>
    </html>

    controller代码

    @Controller
    @RequestMapping("city")
    public class CityController {
    
        @Autowired
        CityService citySrv;
        
        
        @RequestMapping("/jspp")
        public String s(Model model) {
            List<City> list=citySrv.findAll();
            model.addAttribute("list",list);
            return "list";
        }
    
    }

    显示截图

    易错点:使用jsp一定要从maven中取消thymeleaf引擎,不然会报错误

  • 相关阅读:
    数据结构:数组、链表、栈、队列的理解
    JVM学习记录-JVM的内存结构管理和运行时数据区理解
    线程池ThreadPoolExecutor的一种扩展办法
    四级地址插件升级改造(京东商城地址选择插件)city-picker
    java8在Collection中新增加的方法removeIf
    使用lambda编程之延迟执行
    JSP的内置对象以及作用域。
    Netty 异步的、事件驱动的网络应用程序框架和工具
    Zookeeper 服务注册和发现
    Jetty 发布web服务
  • 原文地址:https://www.cnblogs.com/littlepage/p/11064433.html
Copyright © 2011-2022 走看看