仿牛客网第一章WonderC

1.SpringBoot是Spring的简化2.SpringMVC用于处理浏览器的请求3.MyBatis用来访问数据库4.Redis用作缓存5.Kafka用作消息队列6.Elasticsearch用作全文搜索7.SpringSecurity可以管理系统权限8.SpringActuator用作系统上线后的状态监控

随时记:server.port=80//设置访问端口号server.servlet.context-path=/community//设置默认路径三、Spring入门1.SpringFramework

2.SpringIoC

此类其实是一个配置类

packagecom.hsw.community;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassCommunityApplication{publicstaticvoidmain(String[]args){SpringApplication.run(CommunityApplication.class,args);}}如何使用spring容器?

@SpringBootTest@ContextConfiguration(classes=CommunityApplication.class)//使用配置类//实现ApplicationContaxtAware接口并实现相应方法即可从参数中获取ApplicationContextclassCommunityApplicationTestsimplementsApplicationContextAware{privateApplicationContextapplicationContext;@OverridepublicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{this.applicationContext=applicationContext;}@TestpublicvoidtestApplication(){System.out.println(applicationContext);//常用方法applicationContext.getBean(Dao.class);applicationContext.getBean("mybatisDao",Dao.class);}}随时记/**使用场景比如我们有Dao接口下有两个实现类hibernateDao和mybatisDao*我们用applicationContext获取bean时希望获取mybatisDao则加入此注解即可*/@Primary@Repority("mybatisDao")//自定义bean的名字@PostConstruct//在构造器之后调用@PreDestroy//销毁之前调用@Scope("prototype")//spring默认的bean都是单例的加此注解会在每次getBean方法调用时实例化对象在配置类中配置要使用的bean(很笨拙的方法)

@ConfigurationpublicclassAlphaConfig{@BeanpublicSimpleDateFormatsimpleDateFormat(){returnnewSimpleDateFormat("yyyy-MM-dd");}}随时记@bean//bean的名称就是方法名如上simpleDateFormat@Autowired//依赖注入,获取bean@Qualifier("xxx")//把名字为xxx的bean注入,一般和Autowired一起使用四、SpringMVC入门SpringMVC

Thymeleaf

随时记spring.thymeleaf.cache=false//开发中关闭thymeleaf的缓存,上线后开启//Thymeleaf配置类,实际配置过程就是给某个bean设置属性@EnableConfigurationProperties(ThymeleafProperties.class)publicclassThymeleafAutoConfiguration@ConfigurationProperties(prefix="spring.thymeleaf")publicclassThymeleafProperties{简单举几个例子1.mvc

@Controller@RequestMapping("/demo")publicclassAlphaController{@RequestMapping("/test")publicvoiddemo(HttpServletRequestrequest,HttpServletResponseresponse){System.out.println(request.getContextPath());System.out.println(request.getMethod());EnumerationheaderNames=request.getHeaderNames();while(headerNames.hasMoreElements()){Stringname=headerNames.nextElement();Stringvalue=request.getHeader(name);System.out.println("header:"+name+"的值是->"+value);}response.setContentType("text/html;charset=utf-8");try(PrintWriterwriter=response.getWriter()){writer.write("我会变强的");}catch(IOExceptione){e.printStackTrace();}}}2.快速获取request中的参数

@RequestMapping(path="/testRequestParam",method=RequestMethod.GET)@ResponseBody///testRequestParami=10&j=100publicStringtestRequestParam(@RequestParam(name="i",required=false,defaultValue="1")inti,@RequestParam(name="j",required=false,defaultValue="100")intj){System.out.println(i);System.out.println(j);return"helloworld";}3.快速获取路径中的值

@RequestMapping(path="/testPathVariable/{id}",method=RequestMethod.GET)@ResponseBody///testPathVariable/123publicStringtestPathVariable(@PathVariable("id")intid){System.out.println(id);//123return"helloworld";}随时记:@RequestParam//经过DispatcherServlet处理后会从request对象中获取参数@PathVariable("xxx")//快速获取路径中的值如上所示4.表单中数据的获取

名字:

年龄:

@RequestMapping(path="/testPost",method=RequestMethod.POST)@ResponseBodypublicStringtestPost(Stringname,intage){System.out.println(name);System.out.println(age);return"helloworld";}随时记:直接让方法参数名和表单中定义的名字相等即可获取5.填充模板数据

@RequestMapping(path="/teacher",method=RequestMethod.GET)publicModelAndViewtestThymeleaf(){ModelAndViewmv=newModelAndView();mv.addObject("name","狂徒张三");mv.addObject("age","100");mv.setViewName("teacher.html");returnmv;}teacher.html位于templates下

@RequestMapping(path="/teacher",method=RequestMethod.GET)publicStringtestThymeleaf(Modelmodel){model.addAttribute("name","电棍");model.addAttribute("age","1000");return"teacher.html";}6.相应json数据(用于异步请求,java对象->json->js对象)

@RequestMapping(path="/testJson",method=RequestMethod.GET)@ResponseBodypublicMaptestJson(){Mapmap=newHashMap<>();map.put("name","猪猪侠");map.put("age",19);returnmap;}五、MyBatis入门MyBatis

mysqlmysql-connector-java5.1.47org.mybatis.spring.bootmybatis-spring-boot-starter2.0.1添加配置

#DataSourcePropertiesspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/communitycharacterEncoding=utf-8&useSSL=false&serverTimezone=Hongkongspring.datasource.username=rootspring.datasource.password=123456spring.datasource.type=com.zaxxer.hikari.HikariDataSourcespring.datasource.hikari.maximum-pool-size=15spring.datasource.hikari.minimum-idle=5spring.datasource.hikari.idle-timeout=30000#MybatisProperties#resources目录下新建一个mapper目录存放xml文件mybatis.mapper-locations=classpath:mapper/*.xmlmybatis.type-aliases-package=com.hsw.community.entity#启动自动设置主键mybatis.configuration.useGeneratedKeys=true#下划线命名方式和驼峰命名方式匹配如:header_url==headerUrlmybatis.configuration.mapUnderscoreToCamelCase=true创建entity包并创建User类

importjava.util.Date;publicclassUser{privateintid;privateStringusername;privateStringpassword;privateStringsalt;privateStringemail;privateinttype;privateintstatus;privateStringactivationCode;privateStringheaderUrl;privateDatecreateTime;在dao包下创建UserMapper接口

@Mapper@RepositorypublicinterfaceUserMapper{UserselectById(intid);UserselectByName(Stringusername);UserselectByEmail(Stringemail);intinsertUser(Useruser);intupdateStatus(intid,intstatus);intupdateHeader(intid,StringheaderUrl);intupdatePassword(intid,Stringpassword);}在mapper文件夹下建立user-mapper.xml文件

@SpringBootTest@ContextConfiguration(classes=CommunityApplication.class)publicclassMapperTest{@AutowiredprivateUserMapperuserMapper;@TestpublicvoidtestSelectUser(){Useruser=userMapper.selectById(101);System.out.println(user);}}遇到的问题:

最初在UserMapper接口上只是用了@Mapper注解也能跑但是idea总是提示找不到bean单独加@Repository其实也能跑这里把两个都写上去了其实没必要

随时记:因为mapper.xml文件中的sql语句写错很难被发现,为了排错可以设置日志级别为debug便于调错#loggerlogging.level.com.hsw.community=debug六、开发社区首页开发流程

社区首页

publicclassDiscussPost{privateintid;privateintuserId;privateStringtitle;privateStringcontent;//0-普通;1-置顶;privateinttype;//0-正常;1-精华;2-拉黑;privateintstatus;privateDatecreateTime;privateintcommentCount;privatedoublescore;}2.写对应dao

@RepositorypublicinterfaceDiscussPostMapper{/***@paramuserId考虑查看我的帖子的情况下设置动态sql,看mapper就知道了*@paramoffset*@paramlimit*@return*/ListselectDiscussPosts(intuserId,intoffset,intlimit);//如果需要动态拼接条件(里使用)并且这个方法有且只有一个参数需要用@Param起别名//@Param用于给参数取别名intselectDiscussPostRows(@Param("userId")intuserId);}3.mapper目录下写个discusspost-mapper.xml

2.服务端断点调试技巧

3.客户端断点调试技巧

packageorg.slf4j;publicinterfaceLogger{//我们可以在配置文件中启动不同的级别,则其以上级别日志可以显示低级别日志不会显示。//Printingmethods:publicvoidtrace(Stringmessage);publicvoiddebug(Stringmessage);publicvoidinfo(Stringmessage);publicvoidwarn(Stringmessage);publicvoiderror(Stringmessage);}写个测试类

packagecom.hsw.community;importorg.junit.jupiter.api.Test;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.ContextConfiguration;@SpringBootTest@ContextConfiguration(classes=CommunityApplication.class)publicclassLoggerTest{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(LoggerTest.class);@TestpublicvoidtestLogger1(){System.out.println(logger.getName());logger.trace("hellotrace");//程序调试日志logger.debug("hellodebug");//普通级别日志logger.info("helloinfo");logger.warn("hellowarn");//错误日志logger.error("hellolog");}}添加配置文件

#loggerlogging.level.com.hsw.community=debug输出结果

com.hsw.community.LoggerTest2020-05-0415:25:59.505DEBUG2644---[main]com.hsw.community.LoggerTest:hellodebug2020-05-0415:25:59.505INFO2644---[main]com.hsw.community.LoggerTest:helloinfo2020-05-0415:25:59.505WARN2644---[main]com.hsw.community.LoggerTest:hellowarn2020-05-0415:25:59.505ERROR2644---[main]com.hsw.community.LoggerTest:hellolog更改配置文件

logging.level.com.hsw.community=warn在此运行测试类查看输出结果

com.hsw.community.LoggerTest2020-05-0415:28:54.515WARN9020---[main]com.hsw.community.LoggerTest:hellowarn2020-05-0415:28:54.515ERROR9020---[main]com.hsw.community.LoggerTest:hellolog日志输出到文件中的配置

#文件都是以log结尾logging.file.name=d:work.log注意:这么搞有个问题,不同级别日志混杂不易查看且文件庞大。解决方法:使用配置文件配置(放到resource目录下)

$gitconfig--globaluser.name"hsw"$gitconfig--globaluser.emailhsw@hust.com12

THE END
1.牛客网求职之前,先上牛客,就业找工作一站解决。互联网IT技术/产品/运营/硬件/汽车机械制造/金融/财务管理/审计/银行/市场营销/地产/快消/管培生等等专业技能学习/备考/求职神器,在线进行企业校招实习笔试面试真题模拟考试练习,全面提升求职竞争力,找到好工作,拿到好offer。_https://m.nowcoder.com/feed/main/detail/86523f888c904033aeaf5c68f02fd88a
2.牛客网求职之前,先上牛客,就业找工作一站解决。互联网IT技术/产品/运营/硬件/汽车机械制造/金融/财务管理/审计/银行/市场营销/地产/快消/管培生等等专业技能学习/备考/求职神器,在线进行企业校招实习笔试面试真题模拟考试练习,全面提升求职竞争力,找到好工作,拿到好offer。_https://www.zhiyeapp.com/
3.牛客网剑指offer(Python版)剑指offer官网牛客网剑指offer(Python版) 剑指offer官网:https://www.nowcoder.com/ta/coding-interviews 写在前面的话 刷剑指offer的时候只需要提交函数核心部分,但是在公司实际笔试时却需要自己写好输入输出,各个题目的输入也都是五花八门的,在这里记录一下一般常用的输入的写法,以免忘记。https://blog.csdn.net/hitzijiyingcai/article/details/90757095/
4.黄金城最新娱乐官网下载(综合)官方网站入口/网页版/苹果/手机【?注册充值?送好礼】黄金城最新娱乐官网下载 ?支持:64/128bit系统类型:黄金城最新娱乐官网下载(综合)官方网站入口/网页版/苹果/手机版app下载v14.8.91(安全平台)官方入口是一款根据美少女忍者漫画改编而来的手游,多种多样的二次元美女等着你来收集,还原各种漫画剧情,解锁自己https://m.vipzhuanli.com/patent/201920983882.6/
5.星空app官方版官网版下载假如需求赶忙来下载网下载体会吧! 3. 「分享下」 星空app官网下载官方网站官网-APP下载支持:winall/win7/win10/win11系统类型:星空app官网下载官方网站下载(2024全站)最新版本IOS/安卓官方入口v9.776.2.54(安全平台)登录入口《星空app官网下载官方网站》一款非常好用的网页离线阅览插件。http://www.chinablueskytech.com/Ipp/detail/MbEdtL.html
6.在Oracle官网下载安装JDK1.8,环境的配置在Oracle官网下载、安装JDK1.8,环境的配置 jdk是JAVA的开发编译环境啊,里面包含了很多类库。即jar包。还有jre jvm 虚拟机。 总而言之jdk 是java语言开发最基础的工具包,是java程序运行的基础也是各种IDE开发环境的基础,由sun公司开发,目前已被oracle收购,要想编辑JAVA语言,jdk是必须的,下面就来说说如何下载,安装,与https://blog.nowcoder.net/n/ffc1242c729b433484932a48bde2a29c
7.关于游卡–游卡官网中国网页游戏龙虎榜 · 十佳网页游戏 《三国杀移动版》 游戏十强 · 最受欢迎原创移动游戏 《三国杀》 BGA国产桌游最具影响力大奖 黄恺 BGA最佳商业桌游设计师 游卡Yokaverse 国内最佳桌游设计与推广奖 《三国杀Online》 机锋网 · 最受用户喜爱的手机网络策略游戏 http://yokaverse.com/about-us/
8.「牛客招聘」牛客怎么样?社区论坛研发,任职牛客网后端开发工程师职位,常驻北京;近期有336位访问者,在脉脉形成影响力97;在2021-5至今,任牛客网公司后端开发工程师职位;在2020-7至2021-5,任北京乐天派网络科技有限公司公司服务端开发工程师职位。 工作经历 后端开发工程师 牛客网 https://maimai.cn/brand/home/2vetfIQL
9.搜一搜全网最易搜的搜索导航,搜一搜导航,soyisou,so易搜牛客网搜索 求职之前,先上牛客,就业找工作一站解决。互联网IT技术/产品/运营/硬件/汽车机械制造/金融/财务管理/审计/银行/市场营销/地产/快消/管培生等等专业技能学习/备考/求职神器,在线进行企业校招实习笔试面试真题模拟考试练习,全面提升求职竞争力,找到好工作,拿到好offer。 https://www.soyisou.cn/