浅尝springboot

springboot真香

IDEA真香,eclipse真的难用,哈哈哈

今天学了springboot;

首先说一下配置文件

  1. pom.xml

  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
      </dependency>

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <scope>runtime</scope>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.16</version>
      </dependency>

      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.6</version>
      </dependency>

      <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5 </version>
      </dependency>
      <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.1.1</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
  </dependencies>

其中有三个依赖是自带的。创建项目的时候就会自动生成。

2.配置application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bookshop?characterEncoding=utf8&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

当时配置的时候少了一个.cj 找了半天的错误

3.运行函数添加dao层的扫描。

package com.example.springbootweb;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.springbootweb.dao")//这个就是扫描dao层用的
public class SpringbootWebApplication {

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

}

在这里插入图片描述
这里是我的项目结构
enitty上面是实体类的声明和一些getset方法;
dao层放的是接口;
controller层负责接收请求;
html里面是界面。

html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
 <meta charset="UTF-8">
 <title>二手书交易平台</title>
</head>
<body>
<table border="1">
 <tr>
     <td>编号</td>
     <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp名</td>
     <td>作者</td>
     <td>价格</td>
     <td>出版社</td>
     <td>种类</td>
     <td>类型</td>
 </tr>
 <tr th:each="book:${books}">
     <td th:text="${book.bookId}"></td>
     <td th:text="${book.bookName}"></td>
     <td th:text="${book.author}"></td>
     <td th:text="${book.price}"></td>
     <td th:text="${book.press}"></td>
     <td th:text="${book.kind}"></td>
     <td th:text="${book.cnt}"></td>
 </tr>
</table>
<form action="/showprice">
 <input type="text" name="price"/>
 <input type="submit" value="查询">
</form>
<form action="/add">&nbsp&nbsp&nbsp名:<input type="text" name="name" /><br>&nbsp&nbsp&nbsp格:<input type="text" name="price" /><br>&nbsp&nbsp&nbsp者:<input type="text" name="author" /><br>
 出版社:<input type="text" name="press" /><br>&nbsp&nbsp&nbsp型:<input type="text" name="kind" /><br>
 <input type="submit" value="添加" />
</form>

</body>
</html>

controller层:


@Controller
public class BookController {

 @Autowired
 private BookDao book;

 @RequestMapping("showall2")
 public String showall2(ModelMap mp){
     List<Book> list =book.getAll();
     mp.addAttribute("books",list);
     return "showbook";
 }

 @RequestMapping("showprice")
 public String showprice(ModelMap mp, Float price){
     System.out.println(price);
     List<Book> list=book.getPriceBook(price);
     mp.addAttribute("books",list);
     return "showbook";
 }

 @RequestMapping("add")
 public String addBook(ModelMap mp,String name,String author,String press,String kind,Float price){
     book.addOneBook(name,author,press,kind,price);
     List<Book> list=book.getAll();
     mp.addAttribute("books",list);
     return "showbook";

 }
}

dao层:

@Repository
public interface BookDao {

 @Select("select * from book")
 List<Book> getAll();
 @Select("select * from book where price>#{price}")
 List<Book> getPriceBook(Float price);
 @Insert("insert into book (bookName,author,price,press,kind) values(#{name},#{author},#{price},#{press},#{kind});")
 void addOneBook(@Param("name") String name, @Param("author") String author,@Param("press") String press,@Param("kind") String kind,@Param("price") Float price);
}

完毕。

大概的思路就是:界面发来请求,controller接收到,调用dao层函数,之后再返回到界面。
嗯,springboot真香,比ssm香多了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值