1.搭建开发环境
maven配置
添加环境变量 path:
F:\apache-maven-3.9.2-bin\apache-maven-3.9.2\bin
cmd 输入 mvn -version 出现版本号,则安装成功
idea配置maven
Spring Initializr
修改端口号、路径
Spring入门
练习
@Autowired注解除了写在成员变量之前,也能写在set方法,构造器
@Qualifier 也可以引用默认名称
@Bean 可以装配任何Bean
Spring MVC 入门
HTTP:超文本传输协议
Thymeleaf
模板引擎:生成动态HTML
常用语法:标准表达式、判断与循环、模板的布局
GET
两种传参方式
// /students?current=1&limit=20 查询所有的学生 当前第一页,最多显示20条
@RequestMapping(path = "/students", method = RequestMethod.GET) //通过path指定路径 限制请求方式为GET
@ResponseBody
public String getStudents(
@RequestParam(name = "current", required = false, defaultValue = "1") int current,//defaultValue默认
@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
System.out.println(current);
System.out.println(limit);
return "some students";
}
//
// /student/123 根据ID查询一个学生,当参数成为路径的一部分
@RequestMapping(path = "/student/{id}", method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable("id") int id) { //@PathVariable路径变量
System.out.println(id);
return "a student";
}
响应HTML数据
// 响应HTML数据
@RequestMapping(path = "/teacher", method = RequestMethod.GET)
public ModelAndView getTeacher() {
ModelAndView mav = new ModelAndView(); //实例化
mav.addObject("name", "张三");
mav.addObject("age", 30);
mav.setViewName("/demo/view");
return mav;
}
@RequestMapping(path = "/school", method = RequestMethod.GET)
public String getSchool(Model model) {
model.addAttribute("name", "北京大学");
model.addAttribute("age", 80);
return "/demo/view";
}
响应JSON数据
// 响应JSON数据(异步请求)
// Java对象 -> JSON字符串 -> JS对象
@RequestMapping(path = "/emp", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getEmp() {
Map<String, Object> emp = new HashMap<>();
emp.put("name", "张三");
emp.put("age", 23);
emp.put("salary", 8000.00);
return emp;
}
POST
浏览器向服务器提交数据
// POST请求
@RequestMapping(path = "/student", method = RequestMethod.POST)
@ResponseBody
public String saveStudent(String name, int age) {
System.out.println(name);
System.out.println(age);
return "success";
}
MyBatis
安装MySQL Server
https://dev.mysql.com/downloads/mysql
初始化 必须用管理员身份打开命令行
cd F:\mysql-8.0.33-winx64\mysql-8.0.33-winx64\bin
mysqld --initialize --console
mysqld install
net start mysql //启动
alter user root@localhost identified by 'root' //修改密码
密码: root
安装 MySQL WorkBean
https://dev.mysql.com/downloads/workbench
MyBatis入门
http://www.mybatis.org/mybatis-3
在此下载jar包:Maven Repository: Search/Browse/Explore (mvnrepository.com)
MySql
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
粘贴到target-pom.xml
MyBatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
导入数据库文件
mysql -uroot -p
create database community;
use community;
source E:/sorce/allsorce/C/ziliao/community-init-sql-1.5/init_schema.sql;
source E:/sorce/allsorce/C/ziliao/community-init-sql-1.5/init_data.sql;
开发社区首页
实体类:表里有什么字段就建什么属性,get /set tostring:为了测试打印数据方便
Dao:通常一张表建立一个Mapper
分页查询,显示前10个帖子,返回的是一个列表
帖子列表
<ul class="list-unstyled">
<li class="media pb-3 pt-3 mb-3 border-bottom" th:each="map:${discussPosts}">
<a href="site/profile.html">
<img th:src="${map.user.headerUrl}" class="mr-4 rounded-circle" alt="用户头像" style="width:50px;height:50px;">
</a>
<div class="media-body">
<h6 class="mt-0 mb-3">
<a href="#" th:utext="${map.post.title}">备战春招,面试刷题跟他复习,一个月全搞定!</a>
<span class="badge badge-secondary bg-primary" th:if="${map.post.type==1}">置顶</span>
<span class="badge badge-secondary bg-danger" th:if="${map.post.status==1}">精华</span>
</h6>
<div class="text-muted font-size-12">
<u class="mr-3" th:utext="${map.user.username}">寒江雪</u> 发布于 <b th:text="${#dates.format(map.post.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</b>
<ul class="d-inline float-right">
<li class="d-inline ml-2">赞 11</li>
<li class="d-inline ml-2">|</li>
<li class="d-inline ml-2">回帖 7</li>
</ul>
</div>
</div>
</li>
</ul>
样式显示不出来,更换网址
项目调试技巧
响应状态码含义
以2开头都是成功
200:成功
以3开头都是重定向
302:重定向
以4开头表示错误
404:路径可能有误,找不到服务器的资源
500:服务器错误
服务端断点调试技巧
断点打到方法之内
F8:下一行
F7:进入当前行调用的方法内部
F9:执行到下一断点,没有断点执行至结束
客户端断点调试技巧
F10 下一行
F11进入
F8 执行到底
设置日志
Spring Boot内置日志工具:https://logback.qos.ch
版本控制
Git
Git CMD(windows)
git version
//查看默认已有的配置
git config --list
//配用户名
git config --global user.name "wangzhuohhua"
//配邮箱
git config --global user.email ""
Git常用命令
本地仓库
cd 进入到要管理的项目目录下
//初始化
git init
//查看当前管理的代码状态
git status
//添加到本地仓库
git add *
//提交 -m备注
git commit -m'test 2'
上传到远程仓库
ssh-keygen -t rsa -C"git 账号邮箱"
2.发送邮件
package com.nowcoder.community.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Component
public class MailClient {
private static final Logger logger= LoggerFactory.getLogger(MailClient.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
public void sendMail(String to, String subject, String content)//收件人,主题,内容
{
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(helper.getMimeMessage());
} catch ( MessagingException e) {
logger.error("发送邮件失败:" + e.getMessage());
}
}
}
导入commons long包
commons long :该工具类可常用在if判断中,判断数据是否相等、是否为空等
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>