本项目是一个简单的图书管理系统,本博客在tomcat中采用servlet 和vue和mybatis技术实现如下功能:
JavaWeb项目【源码】——图书项目1.0:Tomcat版本 + Servlet + Filter + Jsp + bootstrap & JavaScript初步 + Ajax初步
JavaWeb项目【源码】——图书项目2.0:Tomcat版本 & Vue + axios + Servlet + Lombok + JDBC + MySQL技术栈实现
JavaWeb项目【源码】——图书项目3.0:Tomcat版本 & Mybatis + vue + axios + js + mysql + log4j 技术栈实现
resp.getWriter().write(JSON.toJSONString(new ResData(
// 4.new PageInfo对象,共享页数等,以及查询到的数据
List<Company> list = companyService.queryByLikeNameLimit(pageNum, pageSize,name);
PageInfo<Company> pageInfo = new PageInfo<>(pageNum, pageSize, total, pages, list);
resp.getWriter().write(JSON.toJSONString(
new ResData(200, "ok", pageInfo)
));
String name = req.getParameter("name");
String typeId = req.getParameter("typeId");
String birthday = req.getParameter("birthday");
当用的时候,如果想要的类型其实不是String,还需要自己转换格式
// 日期类型转换
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
comUser.setBirthday(sdf.parse(birthday));
} catch (ParseException e) {
throw new RuntimeException(e);
}
// int类型转换
opus.setTypeId(Integer.parseInt(typeId));
Spring核心容器——从配置文件到注解开发 & 创建对象+成员变量赋值 & 增强方法
SpringMvc学习——在idea中新建springWeb项目 & 浏览器请求 和 服务器响应 & SpringMvc文件相关
配置文件application.xml文件,加入mybatis相关
server:
port: 80
# 1.连接数据库——对应之前 xml文件的数据库
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/javaweb?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123
# mybatis其他配置
mybatis:
# 2.给实体类起别名,首字母小写
type-aliases-package: com.tianju.entity
configuration:
# 3.开启驼峰命名
map-underscore-to-camel-case: true
# 4.让日志生效
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 5.扫描sql的位置
mapper-locations: classpath:/mapper/*Mapper.xml
springBoot学习——spring+springMVC & 集成mybatis & 拦截器
SpringBoot学习——追根溯源servlet是啥,tomcat是啥,maven是啥 & springBoot项目初步,maven构建,打包 & 测试
resp.getWriter().write(JSON.toJSONString(new ResData(
// 4.new PageInfo对象,共享页数等,以及查询到的数据
List<Company> list = companyService.queryByLikeNameLimit(pageNum, pageSize,name);
PageInfo<Company> pageInfo = new PageInfo<>(pageNum, pageSize, total, pages, list);
resp.getWriter().write(JSON.toJSONString(
new ResData(200, "ok", pageInfo)
));
在springMvc下,响应一个json @ResponseBody;
要点:
响应是json,要加@ResponseBody;
时间显示的问题,要GMT+8:
@JsonFormat(pattern = “yyyy-MM-DD hh:mm:ss”, timezone = “GMT+8”)
如果一个controller响应都是json则,可以用@RestController代替 @Controller 和 @ResponseBody
package com.tianju.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 响应的json
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResData {
private Integer code;
private String msg;
private Object data;
}
注意这里,日期转换格式写错了,DD要改成小写dd
String name = req.getParameter("name");
String typeId = req.getParameter("typeId");
String birthday = req.getParameter("birthday");
当用的时候,如果想要的类型其实不是String,还需要自己转换格式
// 日期类型转换
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
comUser.setBirthday(sdf.parse(birthday));
} catch (ParseException e) {
throw new RuntimeException(e);
}
// int类型转换
opus.setTypeId(Integer.parseInt(typeId));
在springMvc下,参数自动进行了类型的匹配,通常前端传的是一个JSON队形,后端接收要加上@RequestBody注解
要点:
前端发送对象的方式:
(1) user对象逐个赋值,发送post请求
let user = {}
user.username = this.username;
user.password = this.password;
user.sex = this.sex;
user.birthday = this.birthday;
axios.post("/user/register",user)
(2) 直接创建好user对象,发送post请求
let user = {
"username":this.username,
"password":this.password,
"sex":this.sex,
"birthday":this.birthday,
}
axios.post("/user/register",user)
后端接收要加上@RequestBody,在类上加 @JsonFormat(pattern = “yyyy-MM-dd”)
// 在登陆页面,用户点击登陆按钮,处理请求
@RequestMapping("register")
@ResponseBody
// 如果前端用json对象发,后端需要加上@RequestBody
public ResData register(@RequestBody User user){
System.out.println(user);
return new ResData(200, "ok", null);
}
在spring中集成mybatis
配置文件application.xml文件,加入mybatis相关
server:
port: 80
# 1.连接数据库——对应之前 xml文件的数据库
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/javaweb?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123
# mybatis其他配置
mybatis:
# 2.给实体类起别名,首字母小写
type-aliases-package: com.tianju.entity
configuration:
# 3.开启驼峰命名
map-underscore-to-camel-case: true
# 4.让日志生效
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 5.扫描sql的位置
mapper-locations: classpath:/mapper/*Mapper.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>SpringBootOpusartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<version>2.3.0.RELEASEversion>
<artifactId>spring-boot-starter-parentartifactId>
parent>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.22version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.16version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.12version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.8.11version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
# 注释
server:
port: 80
## spring相关的配置
spring:
# 连接数据库
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/javaweb?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123
## 设置上传文件大小
servlet:
multipart:
max-file-size: 10MB # 设置单个文件最大大小为10MB
# # 另一种解决方案
# thymeleaf:
# cache: false
# prefix: classpath:/templates
# suffix: .html
## mybatis相关配置
mybatis:
# 起别名的包在哪里
type-aliases-package: com.tianju.entity
# 写sql的xml文件,放在这个文件夹下
mapper-locations: classpath:/mapper/*Mapper.xml
# 驼峰命名 + 日志
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
imgLocation: D:/620/
package com.tianju.config;
import com.tianju.interceptor.LoginAuthorInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* springMvc的配置类
* 1.是配置类:@Configuration
* 2.是springMvc的配置类:implements WebMvcConfigurer
*/
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
LoginAuthorInterceptor loginAuthorInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginAuthorInterceptor)
.addPathPatterns("/**") // 拦截谁,表示都拦截
.excludePathPatterns(
"/user/loginPage","/user/login",
"/user/registerPage","/user/register","/image/get",
"/js/**","/css/**","/bootstrap/**","/img/**"
);
}
// 静态资源映射
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 1.浏览器访问哪个链接,2.映射到哪里
registry.addResourceHandler("/bookImg/**") // 访问的连接
.addResourceLocations("file:D:/620/bookImg/"); // 映射的位置
}
}
package com.tianju.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* spring的拦截器
* 1.在容器中,@Component
* 2.是拦截器,实现接口 implements HandlerInterceptor
*/
@Component
public class LoginAuthorInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 如果没有登陆,就去登陆页面; 如果登陆了,就放行;
HttpSession session = request.getSession();
Object user = session.getAttribute("user");
if (user==null){
// 没有登陆,重定向到登陆页面
response.sendRedirect("/user/loginPage");
return false;
}
// 登陆了,放行
return true;
}
}
package com.tianju;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
}
}