2021.7.1
1.数据库
1.1新建数据库
create database springboot_demo;
1.2新建表
CREATE TABLE `product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `p_name` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1.3插入数据
insert into product values(null,"小米");
insert into product values(null,"华为");
insert into product values(null,"vivo");
insert into product values(null,"苹果");
insert into product values(null,"魅族");
select * from product;
1.4在IDEA中添加数据库链接
修改 application.properties,新增数据库链接必须的参数
# 数据库连接配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_demo?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2.修改 maven pom.xml 配置
在之间添加对 mysql 和 mybatis 对应依赖
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
3.创建Product
创建 com.qst.springbootdemo.pojo,并创建 Product.java 实体类
package pojo;
/**
* @program: demo2
* @description:
* @author: 鱼挂东南枝
* @create: 2021-06-30 16:42
**/
public class Product {
/**为什么要用private修饰呢:用来限定作用范围,private只有自己能访问到,
*protected同一个包中的类和自己能访问,public项目中的所有类都能访问
*/
private int id;
private String pName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
}
get和setter可以使用快捷键自动生成:alt+insert
光标放在类里面,按alt+insert,会弹出一个小窗口,选择getter and Setter:
然后会弹出一个窗口,让选择给谁生成方法,可以都选。
选择多个属性时,点击第一个然后按住shift键,再点击最后一个。
或者ctrl+a全选,然后再把不应该选择的去掉。
4.新建 dao 层接口
新建包com.example.demo.mapper,并新建接口 ProductMapper.java。 使用注解@Mapper 表示这是一个 Mybatis Mapper 接口。
package com.example.demo.mapper;
import com.example.demo.pojo.Product;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @program: demo2
* @description:
* @author: 鱼挂东南枝
* @create: 2021-07-01 09:03
**/
//使用注解@Mapper 表示这是一个 Mybatis Mapper 接口
@Mapper
public interface ProductMapper {
/**
* 查找product所有数据
* @return
*/
List<Product> findAll();
}
5.创建映射
5.1创建商品映射文件 productMapper.xml
先在 resources 文件夹中,创建 mybatis 文件夹,并创建子文件夹 mapper:
右键 resources 文件夹–>new–>Directory
5.2创建 mybatis 的 xml 模板
点击 File —>Settings —>file and code template–>点击+ --> 设置 name–>选择模版文件后缀–> 添加模版 内容–> 点击 ok
模板内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
</mapper>
创建好自定的 Mybatis 模板文件之后,就可以创建对应的 xml 文件
5.3创建 product 对应映射 xml
右键 mapper 文件夹,选择刚刚创建的 mybatis-mapper 模板
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.ProductMapper">
<!--查找product所有数据-->
<select id="findAll" resultType="com.example.demo.pojo.Product">
select *
from product;
</select>
</mapper>
6.添加 mybatis 配置
在 application.properties 文件中添加 Mybatis 配置
- 添加 mybatis 映射文件路径配置
- 添加 mybatis 中实体类对应包配置
- 添加驼峰命名规范 如:数据库字段是 order_id 那么 实体字段就要写成 orderId
- 开启 mybatis 日志信息 其中“logging.level.”为前缀,“com.qst”为 Mapper 接口所在的包路径,对应的 value 值为日志的级别。
# mybatis 映射文件路径配置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
# mybatis 中实体类对应包配置
mybatis.type-aliases-package=com.example.demo.pojo
# 驼峰命名规范 如:数据库字段是 order_id 那么 实体字段就要写成orderId
mybatis.configuration.map-underscore-to-camel-case=true
# 如果想看到 mybatis 日志需要做如下配置
logging.level.com.qst=DEBUG
默认情况下,Spring Boot 项目使用 LogBack 实现日志,使用 apache Commons Logging 作为日志接口。 日志级别主要有 ERROR、WARN、INFO、DEBUG。Spring Boot 默认的日志级别为 INFO,日志信息可以打印到控制台。 debug<info<warn<Error
设定 com.qst 包下的日志级别为 debug
7.创建商品 Controller
新建 com.example.demo.web 包,在包中创建 ProductController.java
- 接受 product 映射
- 然后获取所有的产品数据
- 接着放入 Model 中
- 跳转到 listProduct.jsp 中
package com.example.demo.web;
import com.example.demo.mapper.ProductMapper;
import com.example.demo.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @program: demo2
* @description:
* @author: 鱼挂东南枝
* @create: 2021-07-01 09:51
**/
@RequestMapping("productController")
@Controller
public class ProductController {
@Autowired
ProductMapper productMapper;
@RequestMapping("listProduct")
public String listProduct(Model m) {
//获取从数据库查到的product列表
List<Product> products = productMapper.findAll();
//将product列表发送到前端
m.addAttribute("products", products);
//跳转到listProduct.jsp页面
return "listProduct";
}
}
8.创建商品展示页面
共性问题:idea 中 springboot 项目无法新建 jsp 文件
https://blog.csdn.net/jjkang_/article/details/81053522
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>产品列表页</title>
</head>
<body>
<table border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${products}" var="p" varStatus="st">
<tr>
<td>${p.id}</td>
<td>${p.pName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
9添加视图定位配置
在 application.properties 文件中添加视图定位配置
# 视图定位
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
10.测试
10.1ava.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
原因:
我的mysql版本是8.0.22,但是jar是5.1的,jar版本不满足,版本过低
在pom.xml中修改数据库连接的jar版本,然后重转一下就会自动下载
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
10.2The server time zone value ‘???ú±ê×??±??’ is unrecognized or represents more than one time zone.
其中是有乱码。
解决的方法是,在连接mysql的url后面加上了一句话。
原来的jdbc:mysql://127.0.0.1:3306/springboot_demo?characterEncoding=UTF-8
修改后的是jdbc:mysql://127.0.0.1:3306/springboot_demo?characterEncoding=UTF-8&serverTimezone=UTC
10.3javax.servlet.ServletException: Circular view path [listProduct]: would dispatch back to the current handler URL [/productController/listProduct] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.
https://blog.csdn.net/universsky2015/article/details/77965402
10.4项目运行成果,页面404
原因:pom.xml配置不全
我将之前项目的pom.xml全贴进去就成功了,可惜页面只有空表
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--缺少的部分start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--jstl依赖-->
<!--spring-boot-starter-parenth会为我们提供常用jar包版本,
其实不是不用指定,springboot为我们指定好了。
自己指定版本号也可以,会覆盖官方版本-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--tomcat的支持-->
<!--若缺少tomcat jasper依赖,会导致jsp没法转为java文件-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
<!--缺少的部分end-->
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
10.5页面没有数据
原因:我在jsp中使用了c:if标签,但是忘记引用了,加上就行了:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
11总结
之前一直用的是eclipse,昨天才开始使用IDEA,所以对IDEA很多东西都不太熟悉,第一个项目就遇到了很多问题。