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引擎,不然会报错误

 

posted @ 2019-06-21 14:40  SteveYu  阅读(382)  评论(0编辑  收藏  举报