SpringBoot快速配置(无数据库)

pom.xml

<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.0.7.RELEASE</version>
  </parent>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <!-- JSP -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <!-- Maven 依赖示例 -->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.16.0</version> <!-- 使用适当的版本号 -->
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>

    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.6</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

application.yml

spring:
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp
#logging:
#  level:
#    org.springframework: DEBUG

DemoApplication.java

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

HelloHandler的增删改查

@Controller
@RequestMapping("/hello")
public class HelloHandler {
    @Autowired
    private StudentRepository studentRepository;


    @GetMapping("/index")
    public ModelAndView index(){
        System.out.println("Index method is called.");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("list",studentRepository.findAll());
        System.out.println("= =:"+studentRepository.findAll());
        return modelAndView;
    }
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id")long id){
        studentRepository.deleteById(id);
        return "redirect:/hello/index";
    }


    @GetMapping("/save")
    public String showSaveForm(Model model) {
         // Add an empty student for the form
        return "save";
    }

    @PostMapping("/save")
    public String save(Student student){
        studentRepository.saveOrUpdate(student);
        return "redirect:/hello/index";
    }
    @PostMapping("/update")
    public String update(Student student){
        studentRepository.saveOrUpdate(student);
        return "redirect:/hello/index";
    }
    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id")long id){
        Student byId = studentRepository.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("update");
        modelAndView.addObject("student",studentRepository.findById(id));
        return modelAndView;

    }


}
Student类
@Data
@AllArgsConstructor
public class Student {
    private long id;
    private String name;
    private int age;

}
StudentRepository 和 StudentRepositoryimpl

public interface StudentRepository {
    public Collection<Student> findAll();
    public Student findById(long id);
    public void saveOrUpdate(Student student);
    public void deleteById(long id);
}
@Repository
public class StudentRepositoryimpl  implements StudentRepository {
    private static Map<Long, Student> studentMap;
    static {
        System.out.println("Initializing studentMap...");
        studentMap = new HashMap<>();
        studentMap.put(1L,new Student(1L,"张三",22));
        studentMap.put(2L,new Student(2L,"田小班",10));
        studentMap.put(3L,new Student(3L,"田小文",10));
        System.out.println("studentMap initialized: " + studentMap);
    }
    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }

    @Override
    public Student findById(long id) {
        return studentMap.get(id);
    }

    @Override
    public void saveOrUpdate(Student student) {
        studentMap.put(student.getId(),student);
    }

    @Override
    public void deleteById(long id) {
        studentMap.remove(id);
    }
}

----------JSP表格------------------

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>学生信息</h1>
    <table>
        <tr>
           <th>学生编号</th>
            <th>学生姓名</th>
            <th>学生年龄</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${list}" var="student">
            <tr>
                <td><c:out value="${student.id}" /></td>
                <td><c:out value="${student.name}" /></td>
                <td><c:out value="${student.age}" /></td>
                <td>
                    <a href="/hello/findById/${student.id}">修改</a>
                    <a href="/hello/deleteById/${student.id}">删除</a>
                </td>
            </tr>
        </c:forEach>


    </table>
<a href="/hello/save">添加学生</a>

----------JSP表单------------------

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/hello/save" method="post">
    <div>ID:<input type="text" name="id"></div>
    <div>Name:<input type="text" name="name"></div>
    <div>Age:<input type="text" name="age"></div>
    <input type="submit" value="提交">
</form>
</body>
</html>

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个快速开发框架,它内置了很多常用的配置,其中也包括数据库连接的配置。在 Spring Boot 中,我们可以使用 application.properties 或者 application.yml 来配置数据库连接。 以下是一个使用 application.properties 配置数据库连接的例子: ``` # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root # 数据库驱动配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 连接池配置 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=10000 ``` 以上配置文件中,我们指定了数据库连接的 URL、用户名和密码,以及数据库驱动的类名。同时,我们也可以配置连接池相关的属性,如初始化大小、最小空闲连接数、最大活跃连接数、最大等待时间等。 如果你使用的是 application.yml 来配置数据库连接,可以参考以下示例: ``` # 数据库连接配置 spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: root # 数据库驱动配置 spring: datasource: driver-class-name: com.mysql.jdbc.Driver # 连接池配置 spring: datasource: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 10000 ``` 以上就是 Spring Boot 配置数据库连接的基本方法。当然,具体的配置还需要根据你使用的数据库和具体需求进行调整。如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值