- 1 项目需求
- 2 把项目跑起来
- 3 MyBatis入门
本项目要实现一个简化版牛客网首页
社区首页实际上就是一个帖子列表,可以按照最新或最热排序
点击“我要发布”就可以发布帖子,网站服务端需要做一个敏感词过滤,把非法的词隐藏掉
如果没有登陆,只能查看帖子
点击某一帖子时,能够进入帖子的详情页面
如果是登陆的,可以给帖子点赞、回帖
右上方按钮“置顶”“精华”只有管理员和帖主才能看到,这里也要做权限控制
注册功能,注册后,服务器会给用户发一封注册邮箱
登陆功能:1.验证密码与验证码是否正确;2.记住登陆状态
登陆成功后服务端需要记住用户的状态,下次访问就不用重新登陆(用小饼干)
登陆后可查看个人主页
点赞和关注是非常高频的操作,采用什么方案才能提高服务器性能?在流量高峰时段不让服务器挂掉,消息队列
账号设置:修改头像
退出登陆
当别人评论你的帖子或者点赞,系统就会自动发消息通知你
网站每天会产生大量消息,需要用专门的消息队列,需要加强多线程方面的知识,需要理解生产者与消费者模式
可以全栈搜索,搜索不是说查一下数据库就完事了,要通过专业的搜索引擎实现,这也是出于性能的考虑
数据库和搜索引擎之间是如何同步数据的?如何支持中间分词的?如何高亮显示的?
管理员可以统计一段时间内网站的UV或活跃人数。统计用户行为的时候怎样才能节约存储空间,拥有更高的统计效率
如何提高系统性能?如何让系统更安全?
2 把项目跑起来按照https://blog.csdn.net/qq_38939822/article/details/119297748创建maven项目,并用IDEA打开,右下角会有个加载maven项目的按钮,点击以后就会按照maven的pom.xml加载依赖,run 主文件CommunityApplication成功!底层帮我们自动启动了Tomcat服务器,现在可以访问localhost:8080
写配置文件:如果不设置端口号,tomcat默认为8080
# ServerProperties #访问端口 server.port=8080 #项目的访问路径 server.servlet.context-path=/community
就可以用我们指定的地址访问项目啦!http://localhost:8080/community/
0.把数据导入数据库
C:Users9>mysql -uroot -p*** mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 10 Server version: 8.0.26 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | bjpowernode | | community | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> drop database community ; Query OK, 5 rows affected (0.18 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | bjpowernode | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> create database community; Query OK, 1 row affected (0.02 sec) mysql> use community; Database changed mysql> source D:codeinit_schema.sql Query OK, 0 rows affected (0.00 sec) ... Query OK, 0 rows affected (0.00 sec) mysql> show tables; +---------------------+ | Tables_in_community | +---------------------+ | comment | | discuss_post | | login_ticket | | message | | user | +---------------------+ 5 rows in set (0.00 sec) mysql> source D:codeinit_data.sql; ERROR: Unknown command 'i'. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'odeinit_data.sql' at line 1 mysql> source D:/code/init_data.sql; Query OK, 0 rows affected (0.00 sec) ... Query OK, 0 rows affected (0.00 sec)
1.下载依赖包
https://mvnrepository.com/
2.写配置文件
# DataSourceProperties #驱动 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #连接格式 spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong spring.datasource.username=root #1.写自己的密码 spring.datasource.password= spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.idle-timeout=30000 # MybatisProperties #2.resourcees下创建mapper包 mybatis.mapper-locations=classpath:mapper/*.xml #3.按路径创建entity包 mybatis.type-aliases-package=com.nowcoder.community.entity mybatis.configuration.useGeneratedKeys=true #header_url headerUrl这两种不同命名方式可以自动匹配 mybatis.configuration.mapUnderscoreToCamelCase=true
3.写代码访问数据库里的数据
首先要写实体类,有个实体类能够封装表里的数据,然后才好用实体类对数据操作
3.1 实体类
有多少个字段就对应多少个属性,不过属性按照驼峰式命名的方式命名
Alt+insert生成get/set方法、toString方法(方便打印对象看数据)
3.2 dao数据访问层
要在dao数据访问层写UserMapper接口,声明增删改查的方法,再写上对应的配置文件(下一步)
加注解@Mapper
声明增删改查的方法
3.3配置文件
声明好Mapper之后,要想实现它,需要写一个配置文件,配置文件里给每个方法提供它所需要的sql,mybatis底层会自动生成实现类
在mapper包下创建user-mapper.xml文件
配置文件的所有格式在MyBatis官网都有说明
https://mybatis.org/mybatis-3/zh/getting-started.html
注意:namespace写对应的Mapper的路径名+Mapper名,不然MyBatis也找不到对应的是谁呀
sql语句的格式(以select为例)官网都有,很详细
注意:sql语句里的属性名都是表格里的属性名,#{}里的属性名才是实体类里的属性名,不要搞混了
现在练习是一下子都写了,实际开发是用到什么方法再加
3.4 测试
1.写注解
@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes = CommunityApplication.class)
2.注入Mapper
3.写测试类
3.5 调试着玩玩嘻嘻
Mybatis会自动实例化一个Mapper代理
本期感悟:一定一定要在不看老师代的情况下自己写一遍代码!这样才会发现会遇到很多bug与手误



