diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 6e6764b..1b6f520 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,12 +2,33 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index fda0dd7..2f87a08 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -2,14 +2,22 @@ + + + + diff --git a/README.md b/README.md index cee056b..7cd164e 100644 --- a/README.md +++ b/README.md @@ -91,12 +91,24 @@ filetree ├── question总结 记录每天遇到的问题 ├── readme-img README存放图片目录 ├── /study-notes/ java学习笔记 +│ ├── AJAX.md +│ ├── IDEA常用快捷键.md │ ├── javaSE基础.md │ ├── javaSE进阶.md -│ ├── javaweb.md -│ └── Markdown.md +│ ├── javaweb.md +│ ├── Markdown.md +│ ├── Mysql.md +│ ├── Mysql常用命令.md +│ ├── Spring5.md +│ ├── XML.md +│ ├── 注解和反射.md +│ └── 设计模式.md ├── study-notes-imgs java学习笔记中存放的图片 +├── studyDesignCode 设计模式相关代码 ├── studyJavaseCode javaSE相关代码 +├── studyJavaeeCode javaEE相关代码 +├── studyMybatisCode Mybatis相关代码 +├── studySpringCode Spring5相关代码 ├── LICENSE.MD └── README.md @@ -118,18 +130,18 @@ filetree
java基础 -- [ ] java基础 -- [ ] Html、css、js -- [ ] Mysql -- [ ] JDBC 、数据库连接池 -- [ ] XML、XML解析、JSON、JSON解析、Ajax请求 -- [ ] Tomcat 、 Maven -- [ ] Servlet -- [ ] Filter与Listener -- [ ] java反射机制 +- [x] java基础 +- [x] Html、css、js +- [x] Mysql +- [x] JDBC 、数据库连接池 +- [x] XML、XML解析、JSON、JSON解析、Ajax请求 +- [x] Tomcat 、 Maven +- [x] Servlet +- [x] Filter与Listener +- [x] java反射机制 - [ ] JSP - [ ] EL表达式与JSTL -- [ ] 会话控制Cookie和Session +- [x] 会话控制Cookie和Session
diff --git "a/question346円200円273円347円273円223円/1.10/question.md" "b/question346円200円273円347円273円223円/1.10/question.md" new file mode 100644 index 0000000..353eaca --- /dev/null +++ "b/question346円200円273円347円273円223円/1.10/question.md" @@ -0,0 +1,6 @@ +[学习Spring之前要先学习什么?](https://zhuanlan.zhihu.com/p/64001753)
+[自学Java最起码要学到什么程度?](https://www.zhihu.com/question/283856073/answer/1350093889)
+[简单理解前后端分离,微服务,分布式开发](https://blog.csdn.net/qq_46497604/article/details/123816296)
+[设计模式学习顺序](https://blog.csdn.net/londa/article/details/120094154)
+[idea打开项目没有项目目录](https://blog.csdn.net/wyz0923/article/details/105562316)
+[IDEA中如何使用debug调试项目 一步一步详细教程](https://blog.csdn.net/m0_37154839/article/details/107617122)
\ No newline at end of file diff --git "a/question346円200円273円347円273円223円/1.5/question.md" "b/question346円200円273円347円273円223円/1.5/question.md" new file mode 100644 index 0000000..d03d5d7 --- /dev/null +++ "b/question346円200円273円347円273円223円/1.5/question.md" @@ -0,0 +1,16 @@ +[Listener、Filter、Servlet详解](https://www.jianshu.com/p/ec0a382c3d2c) +
+[idea 乱码 浏览器_Java 编码那些事(三) 实战解决IDEA+Tomcat+Servlet 乱码问题](https://codeantenna.com/a/C5FVxMY8jX) +
+[filter代码分析](https://www.cnblogs.com/Luiweony/p/8635217.html) +
+[Java三大器之过滤器(Filter)的工作原理和代码演示](https://developer.aliyun.com/article/636632) +
+[Response重定向的代码实现](https://www.cnblogs.com/xjw12345/p/16566049.html) +
+[response.sendRedirect()实现重定向(页面跳转)](http://c.biancheng.net/view/4015.html) +
+[重温Servlet,2020年了,它还有必要学吗? ](https://www.cnblogs.com/tanghaorong/p/12713000.html#_label0) +
+[常见的API接口管理工具](https://bbs.huaweicloud.com/blogs/339863) +
\ No newline at end of file diff --git "a/question346円200円273円347円273円223円/1.6/question.md" "b/question346円200円273円347円273円223円/1.6/question.md" new file mode 100644 index 0000000..39b2d6b --- /dev/null +++ "b/question346円200円273円347円273円223円/1.6/question.md" @@ -0,0 +1,10 @@ +[JAVA反射与注解](https://www.jianshu.com/p/da4a20d3ab77) +
+[ava类加载器 — classloader 的原理及应用](https://juejin.cn/post/6931972267609948167) +
+[Java基础之注解和反射](https://segmentfault.com/a/1190000038939993) +
+[一文搞懂Java中的注解和反射](https://www.finclip.com/news/f/12801.html) +
+[Java的类加载机制(ClassLoader)](https://www.jianshu.com/p/5dede0e41ec3) +
diff --git "a/question346円200円273円347円273円223円/1.7/question.md" "b/question346円200円273円347円273円223円/1.7/question.md" new file mode 100644 index 0000000..6500fb2 --- /dev/null +++ "b/question346円200円273円347円273円223円/1.7/question.md" @@ -0,0 +1,58 @@ +[我设计数据库常用的几个原则](https://zhuanlan.zhihu.com/p/331500905) +
+[MySQL 规范 (数据库表设计规范)](https://learnku.com/articles/25116) +
+[Git速查表](https://learnku.com/articles/68324) +
+[Database: Migrations](https://laravel.com/docs/9.x/migrations) +
+[傻傻分不清的 DATE、DATETIME 和 TIMESTAMP ( 中 )](https://www.twle.cn/c/yufei/mysqlfav/mysqlfav-basic-datetime-and-timestamp2.html) +
+[MySql 外键约束 之CASCADE、SET NULL、RESTRICT、NO ACTION分析和作用](https://www.cnblogs.com/yzuzhang/p/5174720.html) +
+[JSON Schema](https://json-schema.org/) +
+[JavaWeb学习总结(五十)——文件上传和下载](https://www.cnblogs.com/xdp-gacl/p/4200090.html) +
+[关于图片或者文件在数据库的存储方式归纳](https://www.cnblogs.com/wangtao_20/p/3440570.html) +
+[【不用框架】文件上传和下载](https://cloud.tencent.com/developer/article/1062312) +
+[github常用术语解释](https://www.jianshu.com/p/7691f3e6c45c) +
+[mysql8.0连接jdbc驱动配置以及db.properties文件配置](https://blog.csdn.net/weixin_43951932/article/details/88375062) +
+[Java注释:类、方法和字段注释](http://c.biancheng.net/view/6114.html) +
+[在MySQL中存储图片路径](https://blog.csdn.net/learn_sunzhuli/article/details/45150929) +
+[Java Servlet Filter(过滤器)是什么?它的方法有哪些?](http://www.itjava.info/xuejava/content_237.html) +
+[一文弄懂Vue与Servlet的交互:让你的大学课设变得容易起来](https://blog.csdn.net/m0_59792745/article/details/127695179) +
+[Vue+axios+Servlet 中提交表单数据(含上传图片)超详版!!](https://blog.csdn.net/qq_36291868/article/details/104045985?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-104045985-blog-125792897.pc_relevant_recovery_v2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-104045985-blog-125792897.pc_relevant_recovery_v2&utm_relevant_index=2) +
+[将大文件存到Mysql数据库](https://blog.csdn.net/zxd1435513775/article/details/98943796) +
+[文件下载后删除文件](https://learnku.com/laravel/t/18440) +
+[【Java】java: 无法访问org.testng.annotations.Test ](https://www.cnblogs.com/danhuai/p/16454360.html) +
+[ervlet3.0了,使用@WebServlet注解配置真香](https://blog.csdn.net/Sky_QiaoBa_Sum/article/details/104942390) +
+[JDBC基础-将数据库数据遍历到List集合中](https://blog.csdn.net/aongi/article/details/71076768) +
+[MySQL8.0连接配置信息](https://blog.csdn.net/shenzhou_yh/article/details/106988710) +
+[java: 无法访问org.testng.annotations.Test](https://blog.csdn.net/weixin_46022090/article/details/127241979) +
+[vue-axios与Servlet前后端连接,进行数据交互](https://blog.csdn.net/pgycjh/article/details/125792897?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-125792897-blog-127695179.pc_relevant_landingrelevant&spm=1001.2101.3001.4242.1&utm_relevant_index=3) +
+[关于PrintWriter out = response.getWriter()的使用及注意事项](https://blog.csdn.net/workinghardboy/article/details/80556735) +
+[response.getwriter().println()与response.getwriter.write()的区别](https://blog.csdn.net/qyfx123456/article/details/105075429) +
+[]() +
+[]() +
\ No newline at end of file diff --git "a/question346円200円273円347円273円223円/1.8/requestion.md" "b/question346円200円273円347円273円223円/1.8/requestion.md" new file mode 100644 index 0000000..d217cd2 --- /dev/null +++ "b/question346円200円273円347円273円223円/1.8/requestion.md" @@ -0,0 +1,36 @@ +[idea-快捷键设置(代码格式化)](https://blog.51cto.com/u_3664660/3216285)
+[Swagger @Api 注解详解](https://www.hxstrive.com/article/958.htm)
+[Swagger常用注解API介绍](https://blog.csdn.net/small_to_large/article/details/77586765)
+[Swagger整合Servlet](https://blog.csdn.net/yunzaiqintian/article/details/103177240?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-103177240-blog-82697915.pc_relevant_recovery_v2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-103177240-blog-82697915.pc_relevant_recovery_v2&utm_relevant_index=13)
+[MYSQL如何为数组类型定义字段类型?](https://bbs.csdn.net/topics/330212181)
+[Java 使用 commons-fileupload 实现文件上传工具类](https://blog.csdn.net/weixin_44953227/article/details/112567318)
+[IDEA中Tomcat在控制台乱码问题以及IDEA编码设置UTF-8](https://blog.csdn.net/nan_cheung/article/details/79337273)
+[response.setHeader的各种用法](https://blog.csdn.net/junmoxi/article/details/76976692)
+[IDEA使用谷歌代码规范格式化代码](https://blog.csdn.net/whgyxy/article/details/88747178)
+[IDEA 从入门到踩坑(3)-代码格式化配置](https://blog.csdn.net/weixin_45505313/article/details/103671555)
+[IntelliJ IDEA如何整理代码格式](https://www.jianshu.com/p/0411f971e8b2)
+[转义字符](https://baike.baidu.com/item/%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6/86397)
+[MultipartFile 对上传的文件大小进行限制](https://www.jianshu.com/p/b9421446a315)
+[https://blog.csdn.net/qq_37899792/article/details/102495619](https://blog.csdn.net/qq_37899792/article/details/102495619)
+[js 获取文件名和后缀名称](https://www.cnblogs.com/sunliyuan/p/13925124.html)
+[Java读取文件夹大小的6种方法及代码](https://blog.csdn.net/layman1024/article/details/77988009)
+[关于cookie和session最全解释](https://blog.csdn.net/qq_41723615/article/details/104364061)
+[JS cookie的设置、获取和删除(非常详细)](http://c.biancheng.net/view/9376.html)
+[cookie属性及有效期的设置](https://blog.csdn.net/Guesshat/article/details/109570923)
+[session何时被创建,何时被销毁以及设置session过期时间](https://blog.csdn.net/qq_41538097/article/details/106239901)
+[JSON.parseObject 和 JSON.toJSONString 实例](https://blog.csdn.net/antony9118/article/details/71023009)
+[简单的后端传送json格式至前端正确处理方法](https://blog.csdn.net/weixin_45609535/article/details/122088095)
+[后端如何防止重复提交](https://blog.csdn.net/qq_44750696/article/details/124075129)
+[拦截器原理及案例](https://blog.csdn.net/weixin_43145299/article/details/121530576?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-121530576-blog-52962774.pc_relevant_3mothn_strategy_and_data_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=2)
+[Java 实现加密数据库连接](https://juejin.cn/post/6844903534853357582)
+[MD5加密原理解析及OC版原理实现](https://developer.aliyun.com/article/790494)
+[开发环境、测试环境、生产环境 到底是什么](https://blog.51cto.com/u_15315508/3208529)
+[maven把指定的文件打包到指定文件夹](https://blog.51cto.com/qq578418506/1625475)
+[关于maven打包时, 资源文件没有被打包进来的问题](https://blog.csdn.net/vcj1009784814/article/details/115118255)
+[IDEA 2022年3月1日 破解版下载_激活安装图文教程(永久激活,亲测有效)](https://www.exception.site/essay/how-to-free-use-idea-202021-by-resigter-code)
+[SQL 多表查询之 where和INNER JOIN](https://www.cnblogs.com/Transkai/p/13414146.html)
+[jdbc多表关联查询,多表查询返回结果集处理方法。](https://blog.csdn.net/adfguochen/article/details/95311750)
+[关于 Java 字符串拼接的几种方式以及性能比较](https://www.jianshu.com/p/0245bbd977eb)
+[java使用Servlet进行文件下载](https://blog.csdn.net/qq_26710805/article/details/78209489)
+[Java实现图片上传到服务器,并把上传的图片读取出来「建议收藏」](https://cloud.tencent.com/developer/article/2060512)
+[SQL:多表查询](https://zhuanlan.zhihu.com/p/91973413)
diff --git "a/question346円200円273円347円273円223円/1.9/question.md" "b/question346円200円273円347円273円223円/1.9/question.md" new file mode 100644 index 0000000..9d07016 --- /dev/null +++ "b/question346円200円273円347円273円223円/1.9/question.md" @@ -0,0 +1,11 @@ +[不用任何框架,Java 就能实现定时任务的三种方法](https://www.51cto.com/article/649076.html)
+[MySQL 执行.sql文件](https://www.jianshu.com/p/e603abae317d)
+[使用Maven命令行下载依赖库](https://www.cnblogs.com/ddcoder/p/10374660.html)
+[JAVA 超详细 将文件夹目录打包为 ZIP 压缩包并下载](https://blog.csdn.net/zhyingvip/article/details/115367675)
+[关于java文件打包的用法](https://blog.51cto.com/u_11949423/5610579)
+[Java Servlet - 获取请求头](https://blog.csdn.net/qq_29761395/article/details/122104150)
+[Java获取客户端操作系统类型-HTTP请求头User-Agent](https://developer.aliyun.com/article/991119)
+[Servlet 客户端 HTTP 请求](https://www.runoob.com/servlet/servlet-client-request.html)
+[从数据库中查出的时间是一串数字](https://my.oschina.net/zjllovecode/blog/1553156#:~:text=%E4%BB%8E%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E6%9F%A5%E5%87%BA%E7%9A%84%E6%97%B6%E9%97%B4%E4%BC%A0%E5%88%B0%E5%89%8D%E5%8F%B0%E6%98%BE%E7%A4%BA%E7%9A%84%E6%97%B6%E5%80%99%E5%8F%98%E6%88%90%E4%BA%86%E4%B8%80%E4%B8%B2%E6%95%B0%E5%AD%97%E3%80%82%20%E8%BF%99%E5%85%B6%E5%AE%9E%E6%B2%A1%E6%9C%89%E9%94%99%EF%BC%8C%E8%BF%99%E4%B8%B2%E6%95%B0%E5%AD%97%E6%98%AF%20%E6%97%B6%E9%97%B4%E6%88%B3%20%28timestamp%29%20%EF%BC%8C%E9%80%9A%E5%B8%B8%E6%98%AF%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E5%BA%8F%E5%88%97%EF%BC%8C%E5%94%AF%E4%B8%80%E5%9C%B0%E6%A0%87%E8%AF%86%E6%9F%90%E4%B8%80%E5%88%BB%E7%9A%84%E6%97%B6%E9%97%B4%E3%80%82,%E4%BE%8B%E5%A6%82%EF%BC%9A1305687917%20%E8%A1%A8%E7%A4%BA%E7%9A%84%E6%98%AF%EF%BC%8C%E4%BB%8E%201970%20%E5%B9%B4%E5%88%B0%E7%8E%B0%E5%9C%A8%E7%9A%84%E6%97%B6%E9%97%B4%E6%80%BB%E5%85%B1%E6%98%AF%201305687917%20%E7%A7%92)
+[数据库表中创建时间和更新时间的自动填充](https://blog.csdn.net/weixin_43934513/article/details/111032099)
+[Servlet--HttpServletRequest获取请求信息(请求头、请求行、参数)详解](https://blog.csdn.net/cold___play/article/details/100920952)
diff --git a/study-notes-imgs/image-20191206113717441.png b/study-notes-imgs/image-20191206113717441.png new file mode 100644 index 0000000..8caa0f8 Binary files /dev/null and b/study-notes-imgs/image-20191206113717441.png differ diff --git a/study-notes-imgs/image-20191206122027709.png b/study-notes-imgs/image-20191206122027709.png new file mode 100644 index 0000000..2380210 Binary files /dev/null and b/study-notes-imgs/image-20191206122027709.png differ diff --git a/study-notes-imgs/image-20191206122053933.png b/study-notes-imgs/image-20191206122053933.png new file mode 100644 index 0000000..fe9f4c5 Binary files /dev/null and b/study-notes-imgs/image-20191206122053933.png differ diff --git a/study-notes-imgs/image-20191206172852832.png b/study-notes-imgs/image-20191206172852832.png new file mode 100644 index 0000000..5bcd42b Binary files /dev/null and b/study-notes-imgs/image-20191206172852832.png differ diff --git a/study-notes-imgs/image-20191206184244344.png b/study-notes-imgs/image-20191206184244344.png new file mode 100644 index 0000000..5ad5cf9 Binary files /dev/null and b/study-notes-imgs/image-20191206184244344.png differ diff --git a/study-notes-imgs/image-20191209152153995-16733603410251.png b/study-notes-imgs/image-20191209152153995-16733603410251.png new file mode 100644 index 0000000..0e39a8c Binary files /dev/null and b/study-notes-imgs/image-20191209152153995-16733603410251.png differ diff --git a/study-notes-imgs/image-20191209152153995-16733603516736.png b/study-notes-imgs/image-20191209152153995-16733603516736.png new file mode 100644 index 0000000..0e39a8c Binary files /dev/null and b/study-notes-imgs/image-20191209152153995-16733603516736.png differ diff --git a/study-notes-imgs/image-20191209152153995.png b/study-notes-imgs/image-20191209152153995.png new file mode 100644 index 0000000..0e39a8c Binary files /dev/null and b/study-notes-imgs/image-20191209152153995.png differ diff --git a/study-notes-imgs/image-20191210134209304-16733603410252.png b/study-notes-imgs/image-20191210134209304-16733603410252.png new file mode 100644 index 0000000..d671b9e Binary files /dev/null and b/study-notes-imgs/image-20191210134209304-16733603410252.png differ diff --git a/study-notes-imgs/image-20191210134209304-16733603516737.png b/study-notes-imgs/image-20191210134209304-16733603516737.png new file mode 100644 index 0000000..d671b9e Binary files /dev/null and b/study-notes-imgs/image-20191210134209304-16733603516737.png differ diff --git a/study-notes-imgs/image-20191210134209304.png b/study-notes-imgs/image-20191210134209304.png new file mode 100644 index 0000000..d671b9e Binary files /dev/null and b/study-notes-imgs/image-20191210134209304.png differ diff --git a/study-notes-imgs/image-20191210172248020-16733603410263.png b/study-notes-imgs/image-20191210172248020-16733603410263.png new file mode 100644 index 0000000..d1b3c74 Binary files /dev/null and b/study-notes-imgs/image-20191210172248020-16733603410263.png differ diff --git a/study-notes-imgs/image-20191210172248020-16733603516738.png b/study-notes-imgs/image-20191210172248020-16733603516738.png new file mode 100644 index 0000000..d1b3c74 Binary files /dev/null and b/study-notes-imgs/image-20191210172248020-16733603516738.png differ diff --git a/study-notes-imgs/image-20191210172248020.png b/study-notes-imgs/image-20191210172248020.png new file mode 100644 index 0000000..d1b3c74 Binary files /dev/null and b/study-notes-imgs/image-20191210172248020.png differ diff --git a/study-notes-imgs/image-20230106230042422.png b/study-notes-imgs/image-20230106230042422.png new file mode 100644 index 0000000..bb27990 Binary files /dev/null and b/study-notes-imgs/image-20230106230042422.png differ diff --git a/study-notes-imgs/image-20230106233833715.png b/study-notes-imgs/image-20230106233833715.png new file mode 100644 index 0000000..4ce45f6 Binary files /dev/null and b/study-notes-imgs/image-20230106233833715.png differ diff --git a/study-notes-imgs/image-20230110133812257.png b/study-notes-imgs/image-20230110133812257.png new file mode 100644 index 0000000..3f11afe Binary files /dev/null and b/study-notes-imgs/image-20230110133812257.png differ diff --git a/study-notes-imgs/image-20230110174123453-16733603516724.png b/study-notes-imgs/image-20230110174123453-16733603516724.png new file mode 100644 index 0000000..97e74f9 Binary files /dev/null and b/study-notes-imgs/image-20230110174123453-16733603516724.png differ diff --git a/study-notes-imgs/image-20230110174123453.png b/study-notes-imgs/image-20230110174123453.png new file mode 100644 index 0000000..97e74f9 Binary files /dev/null and b/study-notes-imgs/image-20230110174123453.png differ diff --git a/study-notes-imgs/image-20230110174146729-16733603516735.png b/study-notes-imgs/image-20230110174146729-16733603516735.png new file mode 100644 index 0000000..1a113d7 Binary files /dev/null and b/study-notes-imgs/image-20230110174146729-16733603516735.png differ diff --git a/study-notes-imgs/image-20230110174146729.png b/study-notes-imgs/image-20230110174146729.png new file mode 100644 index 0000000..1a113d7 Binary files /dev/null and b/study-notes-imgs/image-20230110174146729.png differ diff --git "a/study-notes/IDEA345円270円270円347円224円250円345円277円253円346円215円267円351円224円256円.md" "b/study-notes/IDEA345円270円270円347円224円250円345円277円253円346円215円267円351円224円256円.md" new file mode 100644 index 0000000..ec87c29 --- /dev/null +++ "b/study-notes/IDEA345円270円270円347円224円250円345円277円253円346円215円267円351円224円256円.md" @@ -0,0 +1,24 @@ +ctrl+shift+alt:多行操作
+psvm:生成main()方法;
+fori:生成for循环;
+Ctrl+Alt+v:自动补齐返回值类型
+ctrl+o:覆写方法
+ctrl+i:实现接口中的方法
+ctrl+shift+u:大小写转换
+CTRL+SHIFT+Z:取消撤销
+Alt+Insert:生成构造方法、getter、setter
+ctrl+y:删除当前行
+Ctrl+Shift+J:将选中的行合并成一行
+ctrl+g:定位到某一行
+Ctrl+Shitft+向下箭头:将光标所在的代码块向下整体移动
+Ctrl+Shift+向上箭头:将光标所在的代码块向上整体移动
+Alt+Shift+向下箭头:将行向下移动
+Alt+Shift+向上箭头:将行向上移动
+Ctrl+F:在当前文件中查找
+Ctrl+R:替换字符串
+Ctrl+Shift+F:在全局文件中查找字符串
+Ctrl+Shift+R:在全局中替换字符串
+Ctrl+Shift+Enter:自动补齐{}或者分号;
+Shift+Enter:在当前行的下方开始新行
+Ctrl+Alt+Enter:在当前行的上方插入新行
+Ctrl+Delete:删除光标所在至单词结尾处的所有字符
\ No newline at end of file diff --git a/study-notes/Mybatis.md b/study-notes/Mybatis.md new file mode 100644 index 0000000..193d8e0 --- /dev/null +++ b/study-notes/Mybatis.md @@ -0,0 +1,1676 @@ +## Mybatis-9.28 + +环境: + +- JDK1.8 +- Mysql 5.7 +- maven 3.6.1 +- IDEA + +回顾: + +- JDBC +- Mysql +- Java基础 +- Maven +- Junit + +SSM框架:配置文件的。 最好的方式:看官网文档; + +## 1、简介 + +### 1.1、什么是Mybatis + +- MyBatis 是一款优秀的**持久层框架** +- 它支持定制化 SQL、存储过程以及高级映射。 +- MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 +- MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。 +- MyBatis 本是[apache](https://baike.baidu.com/item/apache/6265)的一个开源项目[iBatis](https://baike.baidu.com/item/iBatis), 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。 +- 2013年11月迁移到Github。 + +如何获得Mybatis? + +- maven仓库: + + ```xml + + + org.mybatis + mybatis + 3.5.2 + + ``` + +- Github : https://github.com/mybatis/mybatis-3/releases + +- 中文文档:https://mybatis.org/mybatis-3/zh/index.html + +### 1.2、持久化 + +数据持久化 + +- 持久化就是将程序的数据在持久状态和瞬时状态转化的过程 +- 内存:**断电即失** +- 数据库(Jdbc),io文件持久化。 +- 生活:冷藏. 罐头。 + +**为什么需要需要持久化?** + +- 有一些对象,不能让他丢掉。 +- 内存太贵了 + +### 1.3、持久层 + +Dao层,Service层,Controller层.... + +- 完成持久化工作的代码块 +- 层界限十分明显。 + +### 1.4 为什么需要Mybatis? + +- 帮助程序猿将数据存入到数据库中。 +- 方便 +- 传统的JDBC代码太复杂了。简化。框架。自动化。 +- 不用Mybatis也可以。更容易上手。 **技术没有高低之分** +- 优点: + - 简单易学 + - 灵活 + - sql和代码的分离,提高了可维护性。 + - 提供映射标签,支持对象与数据库的orm字段关系映射 + - 提供对象关系映射标签,支持对象关系组建维护 + - 提供xml标签,支持编写动态sql。 + +**最重要的一点:使用的人多!** + +Spring SpringMVC SpringBoot + +## 2、第一个Mybatis程序 + +思路:搭建环境-->导入Mybatis-->编写代码-->测试! + +### 2.1、搭建环境 + +搭建数据库 + +```sql +CREATE DATABASE `mybatis`; + +USE `mybatis`; + +CREATE TABLE `user`( + `id` INT(20) NOT NULL PRIMARY KEY, + `name` VARCHAR(30) DEFAULT NULL, + `pwd` VARCHAR(30) DEFAULT NULL +)ENGINE=INNODB DEFAULT CHARSET=utf8; + +INSERT INTO `user`(`id`,`name`,`pwd`) VALUES +(1,'clevermis1','123456'), +(2,'clevermis2','123456'), +(3,'clevermis3','123890') +``` + +新建项目 + +1. 新建一个普通的maven项目 + +2. 删除src目录 + +3. 导入maven依赖 + + ```xml + + + + + mysql + mysql-connector-java + 5.1.47 + + + + + org.mybatis + mybatis + 3.5.2 + + + + junit + junit + 4.12 + + + ``` + +### 2.2、创建一个模块 + +- 编写mybatis的核心配置文件 + + ```xml + + + + + + + +
+ + + + + + + + + + + ``` + +- 编写mybatis工具类 + + ```java + //sqlSessionFactory --> sqlSession + public class MybatisUtils { + + private static SqlSessionFactory sqlSessionFactory; + + static{ + try { + //使用Mybatis第一步:获取sqlSessionFactory对象 + String resource = "mybatis-config.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + //既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。 + // SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。 + public static SqlSession getSqlSession(){ + return sqlSessionFactory.openSession(); + } + + } + ``` + +### 2.3、编写代码 + +- 实体类 + + ```java + package com.kuang.pojo; + + //实体类 + public class User { + private int id; + private String name; + private String pwd; + + public User() { + } + + public User(int id, String name, String pwd) { + this.id = id; + this.name = name; + this.pwd = pwd; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", pwd='" + pwd + '\'' + + '}'; + } + } + ``` + +- Dao接口 + + ```java + public interface UserDao { + List getUserList(); + } + ``` + +- 接口实现类由原来的UserDaoImpl转变为一个 Mapper配置文件. + + ```xml + + + +
    + + + + + + ``` + +### 2.4、测试 + +注意点: + +org.apache.ibatis.binding.BindingException: Type interface com.kuang.dao.UserDao is not known to the MapperRegistry. + +**MapperRegistry是什么?** + +核心配置文件中注册 mappers + +- junit测试 + + ```java + @Test + public void test(){ + //第一步:获得SqlSession对象 + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + + //方式一:getMapper + UserDao userDao = sqlSession.getMapper(UserDao.class); + List userList = userDao.getUserList(); + + for (User user : userList) { + System.out.println(user); + } + + + + //关闭SqlSession + sqlSession.close(); + } + ``` + +你们可以能会遇到的问题: + +1. 配置文件没有注册 +2. 绑定接口错误。 +3. 方法名不对 +4. 返回类型不对 +5. Maven导出资源问题 + +## 3、CRUD + +### 1、namespace + +namespace中的包名要和 Dao/mapper 接口的包名一致! + +### 2、select + +选择,查询语句; + +- id : 就是对应的namespace中的方法名; +- resultType:Sql语句执行的返回值! +- parameterType : 参数类型! + +1. 编写接口 + + ```java + //根据ID查询用户 + User getUserById(int id); + ``` + +2. 编写对应的mapper中的sql语句 + + ```java + + ``` + +3. 测试 + + ```java + @Test + public void getUserById() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + User user = mapper.getUserById(1); + System.out.println(user); + + sqlSession.close(); + } + ``` + +### 3、Insert + +```xml + + + insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd}); + +``` + +### 4、update + +```xml + + update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id} ; + +``` + +### 5、Delete + +```xml + + delete from mybatis.user where id = #{id}; + +``` + +注意点: + +- 增删改需要提交事务! + +### 6、分析错误 + +- 标签不要匹配错 +- resource 绑定mapper,需要使用路径! +- 程序配置文件必须符合规范! +- NullPointerException,没有注册到资源! +- 输出的xml文件中存在中文乱码问题! +- maven资源没有导出问题! + +### 7、万能Map + +假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map! + +```java + //万能的Map + int addUser2(Map map); + + + insert into mybatis.user (id, pwd) values (#{userid},#{passWord}); + + @Test + public void addUser2(){ + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + + Map map = new HashMap(); + + map.put("userid",5); + map.put("passWord","2222333"); + + mapper.addUser2(map); + + sqlSession.close(); + } +``` + +Map传递参数,直接在sql中取出key即可! 【parameterType="map"】 + +对象传递参数,直接在sql中取对象的属性即可!【parameterType="Object"】 + +只有一个基本类型参数的情况下,可以直接在sql中取到! + +多个参数用Map,**或者注解!** + +### 8、思考题 + +模糊查询怎么写? + +1. Java代码执行的时候,传递通配符 % % + + ```java + List userList = mapper.getUserLike("%李%"); + ``` + +2. 在sql拼接中使用通配符! + + ```java + select * from mybatis.user where name like "%"#{value}"%" + ``` + +## 4、配置解析 + +### 1、核心配置文件 + +- mybatis-config.xml + +- MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 + + ```xml + configuration(配置) + properties(属性) + settings(设置) + typeAliases(类型别名) + typeHandlers(类型处理器) + objectFactory(对象工厂) + plugins(插件) + environments(环境配置) + environment(环境变量) + transactionManager(事务管理器) + dataSource(数据源) + databaseIdProvider(数据库厂商标识) + mappers(映射器) + ``` + +### 2、环境配置(environments) + +MyBatis 可以配置成适应多种环境 + +**不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。** + +学会使用配置多套运行环境! + +Mybatis默认的事务管理器就是 JDBC , 连接池 : POOLED + +### 3、属性(properties) + +我们可以通过properties属性来实现引用配置文件 + +这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递。【db.properties】 + +![1569656528134](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/416cfd3c7f72e6590678e464d409d98d-1569656528134.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +编写一个配置文件 + +db.properties + +```properties +driver=com.mysql.jdbc.Driver +url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8 +username=root +password=123456 +``` + +在核心配置文件中映入 + +```xml + + + + + +``` + +- 可以直接引入外部文件 +- 可以在其中增加一些属性配置 +- 如果两个文件有同一个字段,优先使用外部配置文件的! + +### 4、类型别名(typeAliases) + +- 类型别名是为 Java 类型设置一个短的名字。‘ +- 存在的意义仅在于用来减少类完全限定名的冗余。 + +```xml + + + + +``` + +也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如: + +扫描实体类的包,它的默认别名就为这个类的 类名,首字母小写! + +```xml + + + + +``` + +在实体类比较少的时候,使用第一种方式。 + +如果实体类十分多,建议使用第二种。 + +第一种可以DIY别名,第二种则·不行·,如果非要改,需要在实体上增加注解 + +```java +@Alias("user") +public class User {} +``` + +### 5、设置 + +这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。 + +![1569657659080](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/d71320d52f7810808903f260c3e9447a-1569657659080.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +![1569657672791](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/125fecc488792c3519e97642496469d8-1569657672791.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +### 6、其他配置 + +- [typeHandlers(类型处理器)](https://mybatis.org/mybatis-3/zh/configuration.html#typeHandlers) +- [objectFactory(对象工厂)](https://mybatis.org/mybatis-3/zh/configuration.html#objectFactory) +- plugins插件 + - mybatis-generator-core + - mybatis-plus + - 通用mapper + +### 7、映射器(mappers) + +MapperRegistry:注册绑定我们的Mapper文件; + +方式一: 【推荐使用】 + +```xml + +
      +
        + +``` + +方式二:使用class文件绑定注册 + +```xml + +
          +
            + +``` + +注意点: + +- 接口和他的Mapper配置文件必须同名! +- 接口和他的Mapper配置文件必须在同一个包下! + +方式三:使用扫描包进行注入绑定 + +```xml + +
              + + +``` + +注意点: + +- 接口和他的Mapper配置文件必须同名! +- 接口和他的Mapper配置文件必须在同一个包下! + +练习时间: + +- 将数据库配置文件外部引入 +- 实体类别名 +- 保证UserMapper 接口 和 UserMapper .xml 改为一致!并且放在同一个包下! + +### 8、生命周期和作用域 + +![1569660357745](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/344bd983cf87f0cd0c9f93666599abb1-1569660357745.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +生命周期,和作用域,是至关重要的,因为错误的使用会导致非常严重的**并发问题**。 + +**SqlSessionFactoryBuilder:** + +- 一旦创建了 SqlSessionFactory,就不再需要它了 +- 局部变量 + +**SqlSessionFactory:** + +- 说白了就是可以想象为 :数据库连接池 +- SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,**没有任何理由丢弃它或重新创建另一个实例。** +- 因此 SqlSessionFactory 的最佳作用域是应用作用域。 +- 最简单的就是使用**单例模式**或者静态单例模式。 + +**SqlSession** + +- 连接到连接池的一个请求! +- SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的作用域是请求或方法作用域。 +- 用完之后需要赶紧关闭,否则资源被占用! + +![1569660737088](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/4c9e5eca26ab6f2ed69d3b4814d3dc3b-1569660737088.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +这里面的每一个Mapper,就代表一个具体的业务! + +## 5、解决属性名和字段名不一致的问题 + +### 1、 问题 + +数据库中的字段 + +![1569660831076](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/9dc67b534bc4dc0d242a5a7e8face85c-1569660831076.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +新建一个项目,拷贝之前的,测试实体类字段不一致的情况 + +```java +public class User { + + private int id; + private String name; + private String password; +} +``` + +测试出现问题 + +![1569661145806](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/aba1ad7dfa9301fb515b54d49f069e6d-1569661145806.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +```xml +// select * from mybatis.user where id = #{id} +//类型处理器 +// select id,name,pwd from mybatis.user where id = #{id} +``` + +解决方法: + +- 起别名 + + ```xml + + ``` + +### 2、resultMap + +结果集映射 + +``` +id name pwd +id name password + + + + + + + + + +``` + +- `resultMap` 元素是 MyBatis 中最重要最强大的元素 +- ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。 +- `ResultMap` 最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显式地用到他们。 +- 如果世界总是这么简单就好了。 + +## 6、日志 + +### 6.1、日志工厂 + +如果一个数据库操作,出现了异常,我们需要排错。日志就是最好的助手! + +曾经:sout 、debug + +现在:日志工厂! + +![1569892155104](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/fe1929ce5d25a066250cf0d0f73ff709-1569892155104.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +- SLF4J +- LOG4J 【掌握】 +- LOG4J2 +- JDK_LOGGING +- COMMONS_LOGGING +- STDOUT_LOGGING 【掌握】 +- NO_LOGGING + +在Mybatis中具体使用那个一日志实现,在设置中设定! + +**STDOUT_LOGGING标准日志输出** + +在mybatis核心配置文件中,配置我们的日志! + +```xml + + + +``` + +![1569892595060](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/7099dcb3fc11c3f85fc5c939b2c2c5ce-1569892595060.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +### 6.2、Log4j + +什么是Log4j? + +- Log4j是[Apache](https://baike.baidu.com/item/Apache/8512995)的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是[控制台](https://baike.baidu.com/item/控制台/2438626)、文件、[GUI](https://baike.baidu.com/item/GUI)组件 +- 我们也可以控制每一条日志的输出格式; +- 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。 +- 通过一个[配置文件](https://baike.baidu.com/item/配置文件/286550)来灵活地进行配置,而不需要修改应用的代码。 + +1. 先导入log4j的包 + + ```xml + + + log4j + log4j + 1.2.17 + + ``` + +2. log4j.properties + + ```properties + #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 + log4j.rootLogger=DEBUG,console,file + + #控制台输出的相关设置 + log4j.appender.console = org.apache.log4j.ConsoleAppender + log4j.appender.console.Target = System.out + log4j.appender.console.Threshold=DEBUG + log4j.appender.console.layout = org.apache.log4j.PatternLayout + log4j.appender.console.layout.ConversionPattern=[%c]-%m%n + + #文件输出的相关设置 + log4j.appender.file = org.apache.log4j.RollingFileAppender + log4j.appender.file.File=./log/kuang.log + log4j.appender.file.MaxFileSize=10mb + log4j.appender.file.Threshold=DEBUG + log4j.appender.file.layout=org.apache.log4j.PatternLayout + log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n + + #日志输出级别 + log4j.logger.org.mybatis=DEBUG + log4j.logger.java.sql=DEBUG + log4j.logger.java.sql.Statement=DEBUG + log4j.logger.java.sql.ResultSet=DEBUG + log4j.logger.java.sql.PreparedStatement=DEBUG + ``` + +3. 配置log4j为日志的实现 + + ```xml + + + + ``` + +4. Log4j的使用!,直接测试运行刚才的查询 + + ![1569893505842](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/44a728eacf2cc7fbfc1f7b9b49a4a1e5-1569893505842.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +**简单使用** + +1. 在要使用Log4j 的类中,导入包 import org.apache.log4j.Logger; + +2. 日志对象,参数为当前类的class + + ```java + static Logger logger = Logger.getLogger(UserDaoTest.class); + ``` + +3. 日志级别 + + ```java + logger.info("info:进入了testLog4j"); + logger.debug("debug:进入了testLog4j"); + logger.error("error:进入了testLog4j"); + ``` + +## 7、分页 + +**思考:为什么要分页?** + +- 减少数据的处理量 + +### 7.1、使用Limit分页 + +```sql +语法:SELECT * from user limit startIndex,pageSize; +SELECT * from user limit 3; #[0,n] +``` + +使用Mybatis实现分页,核心SQL + +1. 接口 + + ```java + //分页 + List getUserByLimit(Map map); + ``` + +2. Mapper.xml + + ```xml + + + ``` + +3. 测试 + + ```java + @Test + public void getUserByLimit(){ + SqlSession sqlSession = MybatisUtils.getSqlSession(); + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + HashMap map = new HashMap(); + map.put("startIndex",1); + map.put("pageSize",2); + + List userList = mapper.getUserByLimit(map); + for (User user : userList) { + System.out.println(user); + } + + sqlSession.close(); + } + ``` + +### 7.2、RowBounds分页 + +不再使用SQL实现分页 + +1. 接口 + + ```java + //分页2 + List getUserByRowBounds(); + ``` + +2. mapper.xml + + ```xml + + + ``` + +3. 测试 + + ```java + @Test + public void getUserByRowBounds(){ + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + //RowBounds实现 + RowBounds rowBounds = new RowBounds(1, 2); + + //通过Java代码层面实现分页 + List userList = sqlSession.selectList("com.kuang.dao.UserMapper.getUserByRowBounds",null,rowBounds); + + for (User user : userList) { + System.out.println(user); + } + + sqlSession.close(); + } + ``` + +### 7.3、分页插件 + +![1569896603103](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/cfe20a9f8c16938f5c70ca23201fee22-1569896603103.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +了解即可,万一 以后公司的架构师,说要使用,你需要知道它是什么东西! + +## 8、使用注解开发 + +### 8.1、面向接口编程 + +\- 大家之前都学过面向对象编程,也学习过接口,但在真正的开发中,很多时候我们会选择面向接口编程 \- **根本原因 : ==解耦== , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的实现 , 大家都遵守共同的标准 , 使得开发变得容易 , 规范性更好** \- 在一个面向对象的系统中,系统的各种功能是由许许多多的不同对象协作完成的。在这种情况下,各个对象内部是如何实现自己的,对系统设计人员来讲就不那么重要了; \- 而各个对象之间的协作关系则成为系统设计的关键。小到不同类之间的通信,大到各模块之间的交互,在系统设计之初都是要着重考虑的,这也是系统设计的主要工作内容。面向接口编程就是指按照这种思想来编程。 + +**关于接口的理解** + +\- 接口从更深层次的理解,应是定义(规范,约束)与实现(名实分离的原则)的分离。 \- 接口的本身反映了系统设计人员对系统的抽象理解。 \- 接口应有两类: \- 第一类是对一个个体的抽象,它可对应为一个抽象体(abstract class); \- 第二类是对一个个体某一方面的抽象,即形成一个抽象面(interface); \- 一个体有可能有多个抽象面。抽象体与抽象面是有区别的。 + +**三个面向区别** + +\- 面向对象是指,我们考虑问题时,以对象为单位,考虑它的属性及方法 . \- 面向过程是指,我们考虑问题时,以一个具体的流程(事务过程)为单位,考虑它的实现 . \- 接口设计与非接口设计是针对复用技术而言的,与面向对象(过程)不是一个问题.更多的体现就是对系统整体的架构 + +### 8.2、使用注解开发 + +1. 注解在接口上实现 + + ```java + @Select("select * from user") + List getUsers(); + ``` + +2. 需要再核心配置文件中绑定接口! + + ```xml + +
                +
                  + + ``` + +3. 测试 + +本质:反射机制实现 + +底层:动态代理! + +![1569898830704](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/2d57e8fc31319f6e80b80dfb788ffa86-1569898830704.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +**Mybatis详细的执行流程!** + +![1569898830704](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/37a7474b1456ce9afd222b6df5ac0fa4-Temp.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +### 8.3、CRUD + +我们可以在工具类创建的时候实现自动提交事务! + +```java +public static SqlSession getSqlSession(){ + return sqlSessionFactory.openSession(true); +} +``` + +编写接口,增加注解 + +```java +public interface UserMapper { + + @Select("select * from user") + List getUsers(); + + // 方法存在多个参数,所有的参数前面必须加上 @Param("id")注解 + @Select("select * from user where id = #{id}") + User getUserByID(@Param("id") int id); + + + @Insert("insert into user(id,name,pwd) values (#{id},#{name},#{password})") + int addUser(User user); + + + @Update("update user set name=#{name},pwd=#{password} where id = #{id}") + int updateUser(User user); + + + @Delete("delete from user where id = #{uid}") + int deleteUser(@Param("uid") int id); +} +``` + +测试类 + +【注意:我们必须要讲接口注册绑定到我们的核心配置文件中!】 + +**关于@Param() 注解** + +- 基本类型的参数或者String类型,需要加上 +- 引用类型不需要加 +- 如果只有一个基本类型的话,可以忽略,但是建议大家都加上! +- 我们在SQL中引用的就是我们这里的 @Param() 中设定的属性名! + +**#{} ${} 区别** + +## 9、Lombok + +```java +Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. +Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more. +``` + +- java library +- plugs +- build tools +- with one annotation your class + +使用步骤: + +1. 在IDEA中安装Lombok插件! + +2. 在项目中导入lombok的jar包 + + ```xml + + org.projectlombok + lombok + 1.18.10 + + ``` + +3. 在实体类上加注解即可! + + ```java + @Data + @AllArgsConstructor + @NoArgsConstructor + ``` + +```java +@Getter and @Setter +@FieldNameConstants +@ToString +@EqualsAndHashCode +@AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor +@Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger +@Data +@Builder +@Singular +@Delegate +@Value +@Accessors +@Wither +@SneakyThrows +``` + +说明: + +``` +@Data:无参构造,get、set、tostring、hashcode,equals +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode +@ToString +@Getter +``` + +## 10、多对一处理 + +多对一: + +![1569909163944](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/d2ea5654e5be2dba16a53faa70e7a49a-1569909163944.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +- 多个学生,对应一个老师 +- 对于学生这边而言, **关联** .. 多个学生,关联一个老师 【多对一】 +- 对于老师而言, **集合** , 一个老师,有很多学生 【一对多】 + +![1569909422471](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/38d001e3c458a7843a4ee9e57b84c91d-1569909422471.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +SQL: + +```sql +CREATE TABLE `teacher` ( + `id` INT(10) NOT NULL, + `name` VARCHAR(30) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=INNODB DEFAULT CHARSET=utf8 + +INSERT INTO teacher(`id`, `name`) VALUES (1, '秦老师'); + +CREATE TABLE `student` ( + `id` INT(10) NOT NULL, + `name` VARCHAR(30) DEFAULT NULL, + `tid` INT(10) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `fktid` (`tid`), + CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) +) ENGINE=INNODB DEFAULT CHARSET=utf8 + + +INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1'); +INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '1'); +INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1'); +INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1'); +INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1'); +``` + +### 测试环境搭建 + +1. 导入lombok +2. 新建实体类 Teacher,Student +3. 建立Mapper接口 +4. 建立Mapper.XML文件 +5. 在核心配置文件中绑定注册我们的Mapper接口或者文件!【方式很多,随心选】 +6. 测试查询是否能够成功! + +### 按照查询嵌套处理 + +```xml + + + + + + + + + + + + +``` + +### 按照结果嵌套处理 + +```xml + + + + + + + + + + +``` + +回顾Mysql 多对一查询方式: + +- 子查询 +- 联表查询 + +## 11、一对多处理 + +比如:一个老师拥有多个学生! + +对于老师而言,就是一对多的关系! + +### 环境搭建 + +1. 环境搭建,和刚才一样 + +**实体类** + +```java +@Data +public class Student { + + private int id; + private String name; + private int tid; + +} +@Data +public class Teacher { + private int id; + private String name; + + //一个老师拥有多个学生 + private List students; +} +``` + +### 按照结果嵌套处理 + +```xml + + + + + + + + + + + + + +``` + +### 按照查询嵌套处理 + +```xml + + + + + + + +``` + +### 小结 + +1. 关联 - association 【多对一】 +2. 集合 - collection 【一对多】 +3. javaType & ofType + 1. JavaType 用来指定实体类中属性的类型 + 2. ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型! + +注意点: + +- 保证SQL的可读性,尽量保证通俗易懂 +- 注意一对多和多对一中,属性名和字段的问题! +- 如果问题不好排查错误,可以使用日志 , 建议使用 Log4j + +**慢SQL 1s 1000s** + +面试高频 + +- Mysql引擎 +- InnoDB底层原理 +- 索引 +- 索引优化! + +## 12、动态 SQL + +==**什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句**== + +利用动态 SQL 这一特性可以彻底摆脱这种痛苦。 + +```xml +动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。 + +if +choose (when, otherwise) +trim (where, set) +foreach +``` + +### 搭建环境 + +```sql +CREATE TABLE `blog` ( + `id` varchar(50) NOT NULL COMMENT '博客id', + `title` varchar(100) NOT NULL COMMENT '博客标题', + `author` varchar(30) NOT NULL COMMENT '博客作者', + `create_time` datetime NOT NULL COMMENT '创建时间', + `views` int(30) NOT NULL COMMENT '浏览量' +) ENGINE=InnoDB DEFAULT CHARSET=utf8 +``` + +创建一个基础工程 + +1. 导包 + +2. 编写配置文件 + +3. 编写实体类 + + ```java + @Data + public class Blog { + private int id; + private String title; + private String author; + private Date createTime; + private int views; + + + } + ``` + +4. 编写实体类对应Mapper接口 和 Mapper.XML文件 + +### IF + +```xml + +``` + +### choose (when, otherwise) + +```xml + +``` + +### trim (where,set) + +```xml +select * from mybatis.blog + + + title = #{title} + + + and author = #{author} + + + + update mybatis.blog + + + title = #{title}, + + + author = #{author} + + + where id = #{id} + +``` + +==**所谓的动态SQL,本质还是SQL语句 , 只是我们可以在SQL层面,去执行一个逻辑代码**== + +if + +where , set , choose ,when + +### SQL片段 + +有的时候,我们可能会将一些功能的部分抽取出来,方便复用! + +1. 使用SQL标签抽取公共的部分 + + ```xml + + + title = #{title} + + + and author = #{author} + + + ``` + +2. 在需要使用的地方使用Include标签引用即可 + + ```xml + + ``` + +注意事项: + +- 最好基于单表来定义SQL片段! +- 不要存在where标签 + +### Foreach + +```sql +select * from user where 1=1 and + + + #{id} + + +(id=1 or id=2 or id=3) +``` + +![1569979229205](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/df897eebcc807823bf2c4110006ab8f0-1569979229205.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +![1569979339190](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/2d3c0be3e548e4b9e1891bed567ca1cb-1569979339190.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +```xml + + +``` + +==动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就可以了== + +建议: + +- 现在Mysql中写出完整的SQL,再对应的去修改成为我们的动态SQL实现通用即可! + +## 13、缓存 (了解) + +### 13.1、简介 + +``` +查询 : 连接数据库 ,耗资源! + 一次查询的结果,给他暂存在一个可以直接取到的地方!--> 内存 : 缓存 + +我们再次查询相同数据的时候,直接走缓存,就不用走数据库了 +``` + +1. 什么是缓存 [ Cache ]? + - 存在内存中的临时数据。 + - 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。 +2. 为什么使用缓存? + - 减少和数据库的交互次数,减少系统开销,提高系统效率。 +3. 什么样的数据能使用缓存? + - 经常查询并且不经常改变的数据。【可以使用缓存】 + +### 13.2、Mybatis缓存 + +- MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。 + +- MyBatis系统中默认定义了两级缓存: + + 一级缓存 + + 和 + + 二级缓存 + + - 默认情况下,只有一级缓存开启。(SqlSession级别的缓存,也称为本地缓存) + - 二级缓存需要手动开启和配置,他是基于namespace级别的缓存。 + - 为了提高扩展性,MyBatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存 + +### 13.3、一级缓存 + +- 一级缓存也叫本地缓存: SqlSession + - 与数据库同一次会话期间查询到的数据会放在本地缓存中。 + - 以后如果需要获取相同的数据,直接从缓存中拿,没必须再去查询数据库; + +测试步骤: + +1. 开启日志! +2. 测试在一个Sesion中查询两次相同记录 +3. 查看日志输出 + +![1569983650437](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/e133b7499f4933232938f12583ee26bc-1569983650437.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +缓存失效的情况: + +1. 查询不同的东西 + +2. 增删改操作,可能会改变原来的数据,所以必定会刷新缓存! + + ![1569983952321](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/d4705e13803ab40522d67e87a7666e7d-1569983952321.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +3. 查询不同的Mapper.xml + +4. 手动清理缓存! + + ![1569984008824](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/6ab19cf2322702c500ae088d13b82358-1569984008824.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +小结:一级缓存默认是开启的,只在一次SqlSession中有效,也就是拿到连接到关闭连接这个区间段! + +一级缓存就是一个Map。 + +### 13.4、二级缓存 + +- 二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存 +- 基于namespace级别的缓存,一个名称空间,对应一个二级缓存; +- 工作机制 + - 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中; + - 如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中; + - 新的会话查询信息,就可以从二级缓存中获取内容; + - 不同的mapper查出的数据会放在自己对应的缓存(map)中; + +步骤: + +1. 开启全局缓存 + + ```xml + + + ``` + +2. 在要使用二级缓存的Mapper中开启 + + ```xml + + + ``` + + 也可以自定义参数 + + ```xml + + + ``` + +3. 测试 + + 1. 问题:我们需要将实体类序列化!否则就会报错! + + ``` + Caused by: java.io.NotSerializableException: com.kuang.pojo.User + ``` + +小结: + +- 只要开启了二级缓存,在同一个Mapper下就有效 +- 所有的数据都会先放在一级缓存中; +- 只有当会话提交,或者关闭的时候,才会提交到二级缓冲中! + +### 13.5、缓存原理 + +![1569985541106](http://localhost:63342/4f800f8a-bbed-4dd8-b03c-00449c9f6698/144978614/fileSchemeResource/bb40d828a0b5e1524a0ef5072dfe8955-1569985541106.png?_ijt=k1pivg8056emkpiahkrb7m62mh) + +### 13.6、自定义缓存-ehcache + +```xml +Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存 +``` + +要在程序中使用ehcache,先要导包! + +```xml + + + org.mybatis.caches + mybatis-ehcache + 1.1.0 + +``` + +在mapper中指定使用我们的ehcache缓存实现! + +```xml + + +``` + +ehcache.xml + +```xml + + + + + + + + + + + + +``` + +Redis数据库来做缓存! K-V + +## 练习:29道练习题实战! \ No newline at end of file diff --git a/study-notes/Spring5.md b/study-notes/Spring5.md new file mode 100644 index 0000000..925a922 --- /dev/null +++ b/study-notes/Spring5.md @@ -0,0 +1,2217 @@ +# 1、Spring + +## 1.1、简介 + +- Spring:春天----->给软件行业带来了春天! + +- 2002,首次推出了Spring框架的雏形:interface21框架! + +- Spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版。 + +- **Rod Johnson**,Spring Framework创始人,著名作者。很难想象Rod Johnson的学历,真的让好多人大吃一惊,他是[悉尼大学](https://baike.baidu.com/item/悉尼大学)的博士,然而他的专业不是计算机,而是音乐学。 + +- Spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架 + + + +- SSH:Struct2+Spring+Hibernate! + +- SSM:SpringMVC+Spring+Mybatis! + + + +官网:https://spring.io/projects/spring-framework + +官方下载地址:https://repo.spring.io/release/org/springframework/spring/ + +GitHub:https://github.com/spring-projects/spring-framework/releases + +Maven: + +```xml + + + org.springframework + spring-webmvc + 5.2.2.RELEASE + + +``` + +```xml + + + org.springframework + spring-jdbc + 5.2.2.RELEASE + + + +``` + +## 1.2、优点 + +- Spring是一个开源的免费的框架(容器) +- Spring是一个轻量级的、非入侵式的框架 +- 控制反转(IOC),面向切面编程(AOP) +- 支持事务的处理,对框架整合的支持! + + + +**总结一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程** + + + +## 1.3、组成 + +![image-20191206113513363](../../../../image-20191206113513363.png) + + + +## 1.4、拓展 + +在Spring的官网有这个介绍:现代化的Java开发!说白了就是基于Spring开发! + +![image-20191206113717441](../study-notes-imgs/image-20191206113717441.png) + + + +- SpringBoot + - 一个快速开发的脚手架。 + - 基于SpringBoot可以快速的开发单个微服务。 + - 约定大于配置! +- SpringCloud + - SpringCloud是基于SpringBoot实现的。 + +现在大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要完全掌握Spring及SpringMVC!承上启下的作用! + + + +**弊端:发展了太久,违背了原来的理念!配置十分繁琐,人称:"配置地狱!"** + + + +# 2、IOC理论推导 + +1. UserDao接口 +2. UserDaoImpl实现类 +3. UserService业务接口 +4. UserServiceImpl业务实现类 + +在我们之间的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原来的代码!如果程序代码量十分大,修改一次的成本十分昂贵! + + + +我们使用一个Set接口实现,已经发生了革命性的变化! + +![image-20191206122027709](../study-notes-imgs/image-20191206122027709.png) + + + +```JAVA +private UserDao userDao; + +//利用set进行动态实现值的注入 +public void setUserDao(UserDao userDao) { + this.userDao = userDao; +} +``` + +- 之前,程序是主动创建对象!控制权在程序员手上! +- 使用了set注入后,程序不在具有主动性,而是变成了被动的接收对象! + +这种思想,从本质上解决了问题,我们程序员不用再去管理对象的创建了。系统的耦合性大大降低,可以更加专注的在业务的实现上!这是IOC的原型! + +![image-20191206122053933](../study-notes-imgs/image-20191206122053933.png) + +- IOC本质 + + **控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法**,也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。 + + + + + + 采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。 + + **控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。** + + + + # 3、HelloSpring + + + + + + ### 思考 + + - Hello 对象是谁创建的 ? 【 hello 对象是由Spring创建的 】 + - Hello 对象的属性是怎么设置的 ? 【hello 对象的属性是由Spring容器设置的 】 + + 这个过程就叫控制反转 : + + - 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的 + - 反转 : 程序本身不创建对象 , 而变成被动的接收对象 . + + 依赖注入 : 就是利用set方法来进行注入的. + + **IOC是一种编程思想,由主动的编程变成被动的接收** + + 可以通过newClassPathXmlApplicationContext去浏览一下底层源码 . + + + + ### 修改案例一 + + 我们在案例一中, 新增一个Spring配置文件beans.xml + + ```xml + + + + + + + + + + + + + ``` + + 测试! + + ```java + public class Mytest { + + public static void main(String[] args) { + //获取ApplicationContext;拿到Spring的容器 + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + //容器里有万物,需要什么,就直接get什么! + UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); + userServiceImpl.getUser(); + } + } + ``` + + OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 ! + + # 4、IOC创建对象的方式 + + 1. 使用无参构造创建对象,默认! + + 2. 假设我们要使用有参构造创建对象 + + 1. 下标赋值 + + ```XML + + + + + ``` + + 2. 通过类型赋值 + + ```XML + + + + --> + ``` + + 3. 通过属性名赋值 + + ```XML + + + + + ``` + + 总结:在配置文件加载的时候,容器中管理的对象就已经初始化了! + + # 5、Spring配置 + + ## 5.1、别名 + + ```XML + + + ``` + + ## 5.2、bean的配置 + + ```Xml + + + ``` + + ## 5.3、import + + 这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个 + + 假设现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的! + + - 张三 + - 李四 + - 王五 + - applicationContext.xml + + ```XMl + + + + + + + ``` + + 使用的时候,直接使用总的配置就可以了 + + + + # 6、依赖注入 + + ## 6.1、构造器注入 + + 前面已经说过 + + ## 6.2、Set方式注入【重点】 + + - 依赖注入:Set注入! + - 依赖:bean对象的创建依赖于容器 + - 注入:bean对象中的所有属性,由容器来注入! + + 【环境搭建】 + + 1. 复杂类型 + + ```JAVA + public class Address { + private String address; + + public String getAddress() { + return this.address; + } + + public void setAddress(final String address) { + this.address = address; + } + } + ``` + + 2. 真实测试对象 + + ```java + package com.clevermis.pojo; + + import java.util.Arrays; + import java.util.List; + import java.util.Map; + import java.util.Properties; + import java.util.Set; + + /** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:12 + * @since JDK 1.8 + **/ + public class Student { + + private String name; + private Address address; + private String[] books; + private List hobbies; + private Map card; + private Set games; + private String wife; + private Properties info; + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + public Address getAddress() { + return this.address; + } + + public void setAddress(final Address address) { + this.address = address; + } + + public String[] getBooks() { + return this.books; + } + + public void setBooks(final String[] books) { + this.books = books; + } + + public List getHobbies() { + return this.hobbies; + } + + public void setHobbies(final List hobbies) { + this.hobbies = hobbies; + } + + public Map getCard() { + return this.card; + } + + public void setCard(final Map card) { + this.card = card; + } + + public Set getGames() { + return this.games; + } + + public void setGames(final Set games) { + this.games = games; + } + + public String getWife() { + return this.wife; + } + + public void setWife(final String wife) { + this.wife = wife; + } + + public Properties getInfo() { + return this.info; + } + + public void setInfo(final Properties info) { + this.info = info; + } + + @Override + public String toString() { + return "Student{" + + "name='" + name + '\'' + + ", address=" + address + + ", books=" + Arrays.toString(books) + + ", hobbies=" + hobbies + + ", card=" + card + + ", games=" + games + + ", wife='" + wife + '\'' + + ", info=" + info + + '}'; + } + } + + ``` + + 3. beans.xml + + ```XML + + + + + + + + + + ``` + + 4. 测试类 + + ```JAVA + import com.clevermis.pojo.Student; + import org.springframework.context.ApplicationContext; + import org.springframework.context.support.ClassPathXmlApplicationContext; + + /** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:18 + * @since JDK 1.8 + **/ + public class Mytest { + + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + Student student = (Student) context.getBean("student"); + System.out.println(student.getName()); + } + } + + ``` + + 完善注入信息 + + ```XML + + + + + + + + + + + + + + + + + + + + 红楼梦 + 西游记 + 水浒传 + 三国演义 + + + + + + 吃饭 + 睡觉 + 打豆豆 + + + + +
                    + + +
                  +
                  + + + + + LOL + COC + + + + + + + + + + 123456 + + 小明 + root + root + + +
                  + + +
                  + ``` + + ## 6.3、拓展方式注入 + + 我们可以使用p命名空间和c命名空间进行注入 + + 官方解释: + + ![image-20230110174123453](../study-notes-imgs/image-20230110174123453.png) + + xml: + + ```xml + + + + + + + + + ``` + + + + 注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束! + + ```xml + xmlns:p="http://www.springframework.org/schema/p" + xmlns:c="http://www.springframework.org/schema/c" + ``` + + ## 6.4、bean的作用域 + + ![image-20230110174146729](../study-notes-imgs/image-20230110174146729.png) + + 1. 单例模式(Spring默认机制) + + ```xml + + ``` + + + + 2. 原型模式:每次从容器中get的时候,都会产生一个新对象 + + ```xml + + + ``` + + + + 3. 其余的request、session、application、这些只能在web开发中用到! + + + + # 7、bean的自动装配 + + - 自动装配是Spring满足bean依赖的一种方式! + - Spring会在上下文中自动寻找,并自动给bean装配属性! + + 在Spring中有三种装配的方式 + + 1. 在xml中显示的配置 + 2. 在java中显示配置 + 3. 隐式的自动装配bean【重要】 + + ## 7.1、自动装配 + + 1. 环境搭建 + - 一个人有两个宠物! + + ## 7.2、ByName自动装配 + + ```XML + + + + + + + ``` + + ## 7.3、ByType自动装配 + + ```xml + + + + + + + + + ``` + + 小结: + + - byname的时候,需要保证所有bean的id唯一,并且这个bean需要和注入的属性的set方法的值一致 + - bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和注入的属性的类型一致 + + ## 7.4、使用注解实现自动装配 + + jdk1.5支持的注解,Spring2.5就支持注解了! + + 要使用注解须知: + + 1. 导入约束 context约束 + + 2. **配置注解的支持: ** + + ```xml + + + + + + + ``` + + + + @Autowired + + 直接在属性上用即可!也可以在set方式上使用! + + 使用Autowired我们可以不用使用Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字(ByName) + + 科普: + + ```xml + @Nullable 字段标记了这个注解,说明这个字段可以为null + ``` + + ```JAVA + public @interface Autowired { + boolean required() default true; + } + ``` + + 测试代码: + + ```JAVA + public class People { + //如果显式的定义了AutoWired的required属性为false,说明这个对象可以为null,否则不允许为空 + @Autowired(required = false) + private Cat cat; + @Autowired + private Dog dog; + private String name; + ``` + + 如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value = "xxx")去配合@Autowired的使用,指定一个唯一的bean对象注入 + + + + **@Autowired注解** + + ```JAVA + public class people{ + @Autowired + private Cat cat; + @Autowired + @Qualifier(value = "dog") + private Dog dog; + private String name; + } + ``` + + + + **@Resource注解** + + ```java + public class people{ + @Resource(name="cat") + private Cat cat; + @Resource + private Dog dog; + } + ``` + + + + 小结 + + @Resource和@Autowired的区别: + + - 都是用来自动装配的,都可以放在属性字段上 + - @Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】 + - @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错! + - 执行顺序不同:@Autowired通过byType的方式实现 @Resource默认通过byname的方式实现 + + + + # 8、使用注解开发 + + 在Spring4之后,要使用注解开发,必须要保证AOP包已经导入了 + + ![image-20191209152153995](../study-notes-imgs/image-20191209152153995-16733603410251.png) + + 使用注解需要导入context约束,增加注解的支持! + + ```xml + + + + + + + + + ``` + + 1. bean + + 2. 属性如何注入 + + ```JAVA + @Component + public class User { + //相当于 + @Value("clevermis") + public String name ; + + } + + ``` + + 3. 衍生的注解 + @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层! + + - dao 【@Repository】 + - service 【@Service】 + - controller 【@Controller】 + 这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean + + 4. 自动装配 + + ``` + @Autowired:自动装配通过类型、名字 + 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx") + @Nullable:字段标记了这个注解,说明这个字段可以为null + @Resource:自动装配通过名字、类型 + @Component:自动装配通过名字、类型 + ``` + + + + 5. 作用域 + + ```java + @Component + @Scope("prototype") + public class User { + //相当于 + @Value("clevermis") + public String name; + } + ``` + + + + 6. 小结 + xml与注解: + + - xml更加万能,适用于任何场合!维护简单方便 + - 注解 不是自己类使用不了,维护相对复杂! + + xml与注解最佳实践 + + - xml用来管理bean + - 注解只负责完成属性的注入 + - 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持 + + # 9、使用Java的方式配置Spring + + 我们现在要完全不使用Spring的xml配置了,全权交给Java来做! + + JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能 + + **实体类** + + ```java + package com.clevermis.pojo; + + @Data + @NoArgsConstructor + @AllArgsConstructor + //这里这个注解的意思是。就是说明这个类被spring接管了,注册到了容器中。 + @Component + public class User { + + private String name; + + public String getName() { + return this.name; + } + @Value("clevermis")// 属性注入值 + public void setName(final String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + '}'; + } + } + ``` + + **配置文件** + + ```java + package com.clevermis.config; + + import com.clevermis.pojo.User; + import org.springframework.context.annotation.Bean; + import org.springframework.context.annotation.ComponentScan; + import org.springframework.context.annotation.Configuration; + import org.springframework.context.annotation.Scope; + + @Configuration //这个也会被spring容器注册到容器中,因为他本来就是一个Component + // 代表这是一个配置类 就和我们之前看的beans.xml是一样的 + @ComponentScan("com.clevermis.pojo") + public class ClevermisConfig { + //注册一个bean , 就相当于我们之前写的一个bean标签 + //这个方法的名字 就相当于bean标签的id属性; + // 这个方法的返回值 就相当于bean标签中的class属性 + @Bean + public User user(){ + return new User();//就是返回要注入到bean的对象 + + } + + ``` + + **测试类** + + ```java + public class MyTest { + public static void main(String[] args) { + + //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载! + ApplicationContext context = new AnnotationConfigApplicationContext(kuangConfig.class); + User getUser = (User) context.getBean("User"); + System.out.println(getUser.getName()); + } + } + ``` + + 这种纯Java的配置方式,在SpringBoot中随处可见! + + + + # 10、代理模式 + + 为什么要学习代理模式?因为这就是SpringAOP的底层【SpringAOP和SpringMVC】 + + 代理模式的分类: + + - 静态代理 + - 动态代理 + + ## 10.1、静态代理 + + 角色分析: + + - 抽象角色:一般会使用接口或者抽象类来解决 + - 真实角色:被代理的角色 + - 代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作 + - 客户:访问代理对象的人! + + 代码步骤: + + 1. 接口 + + ```JAVA + //租房接口 + public interface Rent { + public void rent(); + } + ``` + + 2. 真实角色 + + ```JAVA + //房东 + public class Host implements Rent { + @Override + public void rent(){ + System.out.println("房东要出租房子!🏠"); + } + } + ``` + + 3. 代理角色 + + ```JAVA + package com.kuang.demo01; + + public class Proxy implements Rent { + private Host host; + + public Proxy() { + } + + public Proxy(Host host) { + this.host = host; + } + + @Override + public void rent() { + seeHouse(); + host.rent(); + hetong(); + fare(); + } + //看房 + public void seeHouse(){ + System.out.println("中介带你看房"); + } + + //签署合同 + public void hetong(){ + System.out.println("签租赁合同"); + } + + //收取中介费 + public void fare(){ + System.out.println("收取中介费"); + } + } + ``` + + 4. 客户端访问代理角色 + + ```JAVA + package com.kuang.demo01; + + public class Client { + public static void main(String[] args) { + //房东要租房子 + Host host = new Host(); + //代理,中介帮房东租房子,但是呢代理角色一般会有一些附属操作! + Proxy proxy = new Proxy(host); + //你不用面对房东,直接找中介租房即可! + proxy.rent(); + } + } + ``` + + 代理模式的好处: + + - 可以使真实角色的操作更加纯粹!不用关注一些公共的业务 + - 公共事情就交给代理角色!实现了业务的分工 + - 公共业务发生扩展的时候,方便集中管理 + + 缺点: + + - 一个真实角色就会产生一个代理角色;代码量会翻倍〜开发效率会变低 + + ## 10.2、加深理解 + + 代码:对应08-demo02 + + 聊聊AOP + + ![image-20191210134209304](../study-notes-imgs/image-20191210134209304-16733603410252.png) + + + + ## 10.3、动态代理 + + - 动态代理和静态代理角色一样 + - 动态代理的代理类是动态生成的,不是我们直接写好的! + - 动态代理也分为两大类:基于接口的动态代理,基于类的动态代理 + - 基于接口----JDK动态代理 + - 基于类:cglib + - java字节码实现:javasist + + 需要了解两个类:Proxy(代理)、InvocationHandler(调用处理程序) + + **InvocationHandler** + + 动态代理的好处: + + - 可以使真实角色的操作更加纯粹!不用关注一些公共的业务 + - 公共事情就交给代理角色!实现了业务的分工 + - 公共业务发生扩展的时候,方便集中管理 + - 一个动态代理类代理的是一个接口,一般就是对应的一类业务 + - 一个动态代理类可以代理多个类,只要是实现了同一个接口即可 + + + + # 11、AOP + + ## 11.1、什么事AOP + + AOP意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 + + ![image-20191210172248020](../../study-spring/Spring笔记/image-20191210172248020.png) + + ## 11.2、AOP在Spring中的作用 + + **提供声明式事务,允许用户自定义切面** + + - 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等.... + - 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。 + - 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。 + - 目标(Target):被通知对象。 + - 代理(Proxy):向目标对象应用通知之后创建的对象。 + - 切入点(PointCut):切面通知执行的"地点"的定义 + - 连接点(JointPoint):与切入点匹配的执行点 + + + + ## 11.3、使用Spring实现AOP + + 【重点】使用AOP织入,需要导入一个依赖包! + + ```xml + + + org.aspectj + aspectjweaver + 1.9.4 + + ``` + + + + 方式一:使用Spring的API接口【主要SpringAPI接口实现】 + + 方式一:自定义类来实现AOP【主要是切面的定义】 + + + + # 12、整合Mybatis + + 步骤: + + 1. 导入相关jar包 + - junit + - mybatis + - mysql数据库 + - spring相关 + - aop织入 + - mybatis-spring【new】 + 2. 编写配置文件 + 3. 测试 + + ## 12.1、会议mybatis + + 1. 编写实体类 + 2. 编写核心配置文件 + 3. 编写接口 + 4. 编写Mapper.xml + 5. 测试 + + + + ## 12.2、Mybatis-Spring + + 1. 编写数据源配置 + 2. sqlSessionFactory + 3. sqlSessionTemplate + 4. 需要给接口加实现类 + 5. 将自己写的实现类,注入到spring中 + 6. 测试 + + - 导入相关jar包 + + - 编写配置文件 + - 测试 + + # 13、声明式事务 + + ## 1、回顾事务 + + - 把一组业务当成一个业务来做;要么都成功,要么都失败! + - 事务在项目开发中,十分重要,涉及到数据的一致性问题,不能马虎! + - 确保完整性和一致性 + + + + 事务的ACID原则: + + - 原子性 + - 一致性 + - 隔离性 + - 多个业务可以操作同一个资源,防止数据损坏 + - 持久性 + - 事务一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久化的写到存储器中! + + + + ## 2、spring中的事务管理 + + - 声明式事务:AOP + - 编程式事务:需要在代码中,进行事务的管理 + + + + 思考:为什么需要事务? + + - 如果不配置事务,可能存在数据提交不一致的情况下; + - 如果我们不在spring中去配置声明式事务,我们就需要在代码中手动配置事务! + - 事务在项目的开发中十分重要,设计到数据的一致性和完 + +### IOC本质 + +**控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法**,也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。 + + + + + +采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。 + +**控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。** + + + +# 3、HelloSpring + + + + + +### 思考 + +- Hello 对象是谁创建的 ? 【 hello 对象是由Spring创建的 】 +- Hello 对象的属性是怎么设置的 ? 【hello 对象的属性是由Spring容器设置的 】 + +这个过程就叫控制反转 : + +- 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的 +- 反转 : 程序本身不创建对象 , 而变成被动的接收对象 . + +依赖注入 : 就是利用set方法来进行注入的. + +**IOC是一种编程思想,由主动的编程变成被动的接收** + +可以通过newClassPathXmlApplicationContext去浏览一下底层源码 . + + + +### 修改案例一 + +我们在案例一中, 新增一个Spring配置文件beans.xml + +```xml + + + + + + + + + + + + +``` + +测试! + +```java +public class Mytest { + + public static void main(String[] args) { + //获取ApplicationContext;拿到Spring的容器 + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + //容器里有万物,需要什么,就直接get什么! + UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); + userServiceImpl.getUser(); + } +} +``` + +OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 ! + +# 4、IOC创建对象的方式 + +1. 使用无参构造创建对象,默认! + +2. 假设我们要使用有参构造创建对象 + + 1. 下标赋值 + + ```XML + + + + + ``` + + 2. 通过类型赋值 + + ```XML + + + + --> + ``` + + 3. 通过属性名赋值 + + ```XML + + + + + ``` + +总结:在配置文件加载的时候,容器中管理的对象就已经初始化了! + +# 5、Spring配置 + +## 5.1、别名 + +```XML + + +``` + +## 5.2、bean的配置 + +```Xml + + +``` + +## 5.3、import + +这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个 + +假设现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的! + +- 张三 +- 李四 +- 王五 +- applicationContext.xml + +```XMl + + + + + + +``` + +使用的时候,直接使用总的配置就可以了 + + + +# 6、依赖注入 + +## 6.1、构造器注入 + +前面已经说过 + +## 6.2、Set方式注入【重点】 + +- 依赖注入:Set注入! + - 依赖:bean对象的创建依赖于容器 + - 注入:bean对象中的所有属性,由容器来注入! + +【环境搭建】 + +1. 复杂类型 + + ```JAVA + public class Address { + private String address; + + public String getAddress() { + return this.address; + } + + public void setAddress(final String address) { + this.address = address; + } + } + ``` + +2. 真实测试对象 + + ```java + package com.clevermis.pojo; + + import java.util.Arrays; + import java.util.List; + import java.util.Map; + import java.util.Properties; + import java.util.Set; + + /** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:12 + * @since JDK 1.8 + **/ + public class Student { + + private String name; + private Address address; + private String[] books; + private List hobbies; + private Map card; + private Set games; + private String wife; + private Properties info; + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + public Address getAddress() { + return this.address; + } + + public void setAddress(final Address address) { + this.address = address; + } + + public String[] getBooks() { + return this.books; + } + + public void setBooks(final String[] books) { + this.books = books; + } + + public List getHobbies() { + return this.hobbies; + } + + public void setHobbies(final List hobbies) { + this.hobbies = hobbies; + } + + public Map getCard() { + return this.card; + } + + public void setCard(final Map card) { + this.card = card; + } + + public Set getGames() { + return this.games; + } + + public void setGames(final Set games) { + this.games = games; + } + + public String getWife() { + return this.wife; + } + + public void setWife(final String wife) { + this.wife = wife; + } + + public Properties getInfo() { + return this.info; + } + + public void setInfo(final Properties info) { + this.info = info; + } + + @Override + public String toString() { + return "Student{" + + "name='" + name + '\'' + + ", address=" + address + + ", books=" + Arrays.toString(books) + + ", hobbies=" + hobbies + + ", card=" + card + + ", games=" + games + + ", wife='" + wife + '\'' + + ", info=" + info + + '}'; + } + } + + ``` + +3. beans.xml + + ```XML + + + + + + + + + + ``` + +4. 测试类 + + ```JAVA + import com.clevermis.pojo.Student; + import org.springframework.context.ApplicationContext; + import org.springframework.context.support.ClassPathXmlApplicationContext; + + /** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:18 + * @since JDK 1.8 + **/ + public class Mytest { + + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + Student student = (Student) context.getBean("student"); + System.out.println(student.getName()); + } + } + + ``` + +完善注入信息 + +```XML + + + + + + + + + + + + + + + + + + + + 红楼梦 + 西游记 + 水浒传 + 三国演义 + + + + + + 吃饭 + 睡觉 + 打豆豆 + + + + +
                    + + +
                  +
                  + + + + + LOL + COC + + + + + + + + + + 123456 + + 小明 + root + root + + +
                  + + +
                  +``` + +## 6.3、拓展方式注入 + +我们可以使用p命名空间和c命名空间进行注入 + +官方解释: + +![image-20230110174123453](../study-notes-imgs/image-20230110174123453-16733603516724.png) + +xml: + +```xml + + + + + + + + +``` + + + +注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束! + +```xml +xmlns:p="http://www.springframework.org/schema/p" +xmlns:c="http://www.springframework.org/schema/c" +``` + +## 6.4、bean的作用域 + +![image-20230110174146729](../study-notes-imgs/image-20230110174146729-16733603516735.png) + +1. 单例模式(Spring默认机制) + + ```xml + + ``` + + + +2. 原型模式:每次从容器中get的时候,都会产生一个新对象 + + ```xml + + + ``` + + + +3. 其余的request、session、application、这些只能在web开发中用到! + + + +# 7、bean的自动装配 + +- 自动装配是Spring满足bean依赖的一种方式! +- Spring会在上下文中自动寻找,并自动给bean装配属性! + +在Spring中有三种装配的方式 + +1. 在xml中显示的配置 +2. 在java中显示配置 +3. 隐式的自动装配bean【重要】 + +## 7.1、自动装配 + +1. 环境搭建 + - 一个人有两个宠物! + +## 7.2、ByName自动装配 + +```XML + + + + + + +``` + +## 7.3、ByType自动装配 + +```xml + + + + + + + + +``` + +小结: + +- byname的时候,需要保证所有bean的id唯一,并且这个bean需要和注入的属性的set方法的值一致 +- bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和注入的属性的类型一致 + +## 7.4、使用注解实现自动装配 + +jdk1.5支持的注解,Spring2.5就支持注解了! + +要使用注解须知: + +1. 导入约束 context约束 + +2. **配置注解的支持: ** + + ```xml + + + + + + + ``` + + + +@Autowired + +直接在属性上用即可!也可以在set方式上使用! + +使用Autowired我们可以不用使用Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字(ByName) + +科普: + +```xml +@Nullable 字段标记了这个注解,说明这个字段可以为null +``` + +```JAVA +public @interface Autowired { + boolean required() default true; +} +``` + +测试代码: + +```JAVA +public class People { + //如果显式的定义了AutoWired的required属性为false,说明这个对象可以为null,否则不允许为空 + @Autowired(required = false) + private Cat cat; + @Autowired + private Dog dog; + private String name; +``` + +如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value = "xxx")去配合@Autowired的使用,指定一个唯一的bean对象注入 + + + +**@Autowired注解** + +```JAVA +public class people{ +@Autowired +private Cat cat; +@Autowired +@Qualifier(value = "dog") +private Dog dog; +private String name; +} +``` + + + +**@Resource注解** + +```java +public class people{ +@Resource(name="cat") +private Cat cat; +@Resource +private Dog dog; +} +``` + + + +小结 + +@Resource和@Autowired的区别: + +- 都是用来自动装配的,都可以放在属性字段上 +- @Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】 +- @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错! + - 执行顺序不同:@Autowired通过byType的方式实现 @Resource默认通过byname的方式实现 + + + +# 8、使用注解开发 + +在Spring4之后,要使用注解开发,必须要保证AOP包已经导入了 + +![image-20191209152153995](../study-notes-imgs/image-20191209152153995-16733603516736.png) + +使用注解需要导入context约束,增加注解的支持! + +```xml + + + + + + + + +``` + +1. bean + +2. 属性如何注入 + + ```JAVA + @Component + public class User { + //相当于 + @Value("clevermis") + public String name ; + + } + + ``` + +3. 衍生的注解 + @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层! + + - dao 【@Repository】 + - service 【@Service】 + - controller 【@Controller】 + 这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean + +4. 自动装配 + + ``` + @Autowired:自动装配通过类型、名字 + 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx") + @Nullable:字段标记了这个注解,说明这个字段可以为null + @Resource:自动装配通过名字、类型 + @Component:自动装配通过名字、类型 + ``` + + + +5. 作用域 + + ```java + @Component + @Scope("prototype") + public class User { + //相当于 + @Value("clevermis") + public String name; + } + ``` + + + +6. 小结 + xml与注解: + + - xml更加万能,适用于任何场合!维护简单方便 + - 注解 不是自己类使用不了,维护相对复杂! + + xml与注解最佳实践 + + - xml用来管理bean + - 注解只负责完成属性的注入 + - 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持 + +# 9、使用Java的方式配置Spring + +我们现在要完全不使用Spring的xml配置了,全权交给Java来做! + +JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能 + +**实体类** + +```java +package com.clevermis.pojo; + +@Data +@NoArgsConstructor +@AllArgsConstructor +//这里这个注解的意思是。就是说明这个类被spring接管了,注册到了容器中。 +@Component +public class User { + + private String name; + + public String getName() { + return this.name; + } + @Value("clevermis")// 属性注入值 + public void setName(final String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + '}'; + } +} +``` + +**配置文件** + +```java +package com.clevermis.config; + +import com.clevermis.pojo.User; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration //这个也会被spring容器注册到容器中,因为他本来就是一个Component +// 代表这是一个配置类 就和我们之前看的beans.xml是一样的 +@ComponentScan("com.clevermis.pojo") +public class ClevermisConfig { + //注册一个bean , 就相当于我们之前写的一个bean标签 + //这个方法的名字 就相当于bean标签的id属性; + // 这个方法的返回值 就相当于bean标签中的class属性 + @Bean + public User user(){ + return new User();//就是返回要注入到bean的对象 + +} + +``` + +**测试类** + +```java +public class MyTest { + public static void main(String[] args) { + + //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载! + ApplicationContext context = new AnnotationConfigApplicationContext(kuangConfig.class); + User getUser = (User) context.getBean("User"); + System.out.println(getUser.getName()); + } +} +``` + +这种纯Java的配置方式,在SpringBoot中随处可见! + + + +# 10、代理模式 + +为什么要学习代理模式?因为这就是SpringAOP的底层【SpringAOP和SpringMVC】 + +代理模式的分类: + +- 静态代理 +- 动态代理 + +## 10.1、静态代理 + +角色分析: + +- 抽象角色:一般会使用接口或者抽象类来解决 +- 真实角色:被代理的角色 +- 代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作 +- 客户:访问代理对象的人! + +代码步骤: + +1. 接口 + + ```JAVA + //租房接口 + public interface Rent { + public void rent(); + } + ``` + +2. 真实角色 + + ```JAVA + //房东 + public class Host implements Rent { + @Override + public void rent(){ + System.out.println("房东要出租房子!🏠"); + } + } + ``` + +3. 代理角色 + + ```JAVA + package com.kuang.demo01; + + public class Proxy implements Rent { + private Host host; + + public Proxy() { + } + + public Proxy(Host host) { + this.host = host; + } + + @Override + public void rent() { + seeHouse(); + host.rent(); + hetong(); + fare(); + } + //看房 + public void seeHouse(){ + System.out.println("中介带你看房"); + } + + //签署合同 + public void hetong(){ + System.out.println("签租赁合同"); + } + + //收取中介费 + public void fare(){ + System.out.println("收取中介费"); + } + } + ``` + +4. 客户端访问代理角色 + + ```JAVA + package com.kuang.demo01; + + public class Client { + public static void main(String[] args) { + //房东要租房子 + Host host = new Host(); + //代理,中介帮房东租房子,但是呢代理角色一般会有一些附属操作! + Proxy proxy = new Proxy(host); + //你不用面对房东,直接找中介租房即可! + proxy.rent(); + } + } + ``` + +代理模式的好处: + +- 可以使真实角色的操作更加纯粹!不用关注一些公共的业务 +- 公共事情就交给代理角色!实现了业务的分工 +- 公共业务发生扩展的时候,方便集中管理 + +缺点: + +- 一个真实角色就会产生一个代理角色;代码量会翻倍〜开发效率会变低 + +## 10.2、加深理解 + +代码:对应08-demo02 + +聊聊AOP + +![image-20191210134209304](../study-notes-imgs/image-20191210134209304-16733603516737.png) + + + +## 10.3、动态代理 + +- 动态代理和静态代理角色一样 +- 动态代理的代理类是动态生成的,不是我们直接写好的! +- 动态代理也分为两大类:基于接口的动态代理,基于类的动态代理 + - 基于接口----JDK动态代理 + - 基于类:cglib + - java字节码实现:javasist + +需要了解两个类:Proxy(代理)、InvocationHandler(调用处理程序) + +**InvocationHandler** + +动态代理的好处: + +- 可以使真实角色的操作更加纯粹!不用关注一些公共的业务 +- 公共事情就交给代理角色!实现了业务的分工 +- 公共业务发生扩展的时候,方便集中管理 +- 一个动态代理类代理的是一个接口,一般就是对应的一类业务 +- 一个动态代理类可以代理多个类,只要是实现了同一个接口即可 + + + +# 11、AOP + +## 11.1、什么是AOP + +AOP意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 + +![image-20191210172248020](../study-notes-imgs/image-20191210172248020-16733603516738.png) + +## 11.2、AOP在Spring中的作用 + +**提供声明式事务,允许用户自定义切面** + +- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等.... +- 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。 +- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。 +- 目标(Target):被通知对象。 +- 代理(Proxy):向目标对象应用通知之后创建的对象。 +- 切入点(PointCut):切面通知执行的"地点"的定义 +- 连接点(JointPoint):与切入点匹配的执行点 + + + +## 11.3、使用Spring实现AOP + +【重点】使用AOP织入,需要导入一个依赖包! + +```xml + + + org.aspectj + aspectjweaver + 1.9.4 + +``` + + + +方式一:使用Spring的API接口【主要SpringAPI接口实现】 + +方式一:自定义类来实现AOP【主要是切面的定义】 + + + +# 12、整合Mybatis + +步骤: + +1. 导入相关jar包 + - junit + - mybatis + - mysql数据库 + - spring相关 + - aop织入 + - mybatis-spring【new】 +2. 编写配置文件 +3. 测试 + +## 12.1、会议mybatis + +1. 编写实体类 +2. 编写核心配置文件 +3. 编写接口 +4. 编写Mapper.xml +5. 测试 + + + +## 12.2、Mybatis-Spring + +1. 编写数据源配置 +2. sqlSessionFactory +3. sqlSessionTemplate +4. 需要给接口加实现类 +5. 将自己写的实现类,注入到spring中 +6. 测试 + +- 导入相关jar包 + +- 编写配置文件 +- 测试 + +# 13、声明式事务 + +## 1、回顾事务 + +- 把一组业务当成一个业务来做;要么都成功,要么都失败! +- 事务在项目开发中,十分重要,涉及到数据的一致性问题,不能马虎! +- 确保完整性和一致性 + + + +事务的ACID原则: + +- 原子性 +- 一致性 +- 隔离性 + - 多个业务可以操作同一个资源,防止数据损坏 +- 持久性 + - 事务一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久化的写到存储器中! + + + +## 2、spring中的事务管理 + +- 声明式事务:AOP +- 编程式事务:需要在代码中,进行事务的管理 + + + +思考:为什么需要事务? + +- 如果不配置事务,可能存在数据提交不一致的情况下; +- 如果我们不在spring中去配置声明式事务,我们就需要在代码中手动配置事务! +- 事务在项目的开发中十分重要,设计到数据的一致性和完 \ No newline at end of file diff --git a/study-notes/javaweb.md b/study-notes/javaweb.md index b1aedf5..531a6c1 100644 --- a/study-notes/javaweb.md +++ b/study-notes/javaweb.md @@ -5251,5 +5251,300 @@ public class LoginServlet extends HttpServlet { ![image-20230105212355028](../study-notes-imgs/image-20230105212355028.png) +# 10. Session Cookie +### 10.1 什么是session和cookie + +回话:用户打开一个浏览器,然后点击了很多超链接,访问了多个web资源,关闭浏览器,这个过程就叫做一个会话 + +客户端 服务端 + + 1、服务器给客户端一个新建,客户端下载访问服务端带上新建就可以了; cookie + + 2、服务器登记你来过了,下次你来得时候我来匹配你 session + +``` + 1,session 在服务器端,cookie 在客户端(浏览器) + 2,session 默认被存在在服务器的一个文件里(不是内存) + 3,session 的运行依赖 session id,而 session id 是存在 cookie 中的,也就是说,如果浏览器禁用了 cookie ,同时 session 也会失效(但是可以通过其它方式实现,比如在 url 中传递 session_id) + 4,session 可以放在 文件、数据库、或内存中都可以。 + 5,用户验证这种场合一般会用 session + +因此,维持一个会话的核心就是客户端的唯一标识,即 session id +``` + + + +### 10.2 保存会话的两种技术 + +cookie + + - 客户端技术,通过响应和请求 + +session + +``` +- 服务器技术,利用这个技术,可以保存用户的会话信息?我们可以吧信息或者数据保存在session +``` + +常见的用力: 网站登录后,你下次就不用继续登录了 + +### 10.3 示例代码 + +``` +/*服务器,告诉你,你来得时间,把这个时间封装成为一个新建,你下次带来,我就知道你来了*/ +public class CookieDemo01Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //解决中文的乱码问题 + req.setCharacterEncoding("utf-8"); + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); //不加这个,在浏览器访问时候会显示为乱码 + + PrintWriter out = resp.getWriter(); + // + Cookie[] cookies = req.getCookies(); + if (cookies!=null){ + //如果存在怎么办?我们取出来数据 + out.write("你上一次访问的时间是:"); + for (int i = 0; i < cookies.length; i++) { + Cookie cookie = cookies[i]; + if (cookie.getName().equals("lastLoginTime")){ + //获取cookie的值 + String value = cookie.getValue(); //拿出来的是字符串,要转换成长整型才能变成时间 + System.out.println(value); + long lastLoginTime = Long.parseLong(value); //使用Long包装类转换成长整型 + Date date = new Date(lastLoginTime); //使用Date转化为时间 变为date对象 + out.write(date.toLocaleString()); + } + } + }else { + out.write("你是第一次访问!"); + } + + //服务器给客户端响应一个cookie + Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+""); + cookie.setMaxAge(24*60*60); + resp.addCookie(cookie); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); + } +} +``` + +1、从请求中获取cookie信息 + +2、服务器响应诶客户端cookie + +``` +Cookie[] cookies = req.getCookies(); //获取cookie +cookie.getName() //获取cookie中的key +cookie.getValue() //获取cookie中的value +Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一个cookie +cookie.setMaxAge(24*60*60); //设置cookie的有效期 +resp.addCookie(cookie); //响应给客户端添加一个cookie +``` + +一个网站cookie是否存在上限!聊聊细节问题 + +- 一个cookie只能保存一个信息 +- 一个web站点可以给浏览器发送多个,最多存20个 +- cookie大小限制为4kb +- 300个cookie浏览器上线 + +删除cookie + +- 不设置有效期,关闭浏览器,自动失效,设置有效期为0,就可以实现这个目标 + +``` +/*通过另一个请求删除掉cookie*/ +public class CookieDemo02Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + //创建一个cookie,名字必须和要删除的cookie一样 + Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+""); + //将cookie有效期设置为0,立马过期 + cookie.setMaxAge(0); + resp.addCookie(cookie); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); + } +} +``` + +案例:遇到中文乱码问题如何解决 + +> ``` +> URLDecoder.decode(cookie.getValue(),"utf-8") 解码 +> URLEncoder.encode("秦僵","utf-8") 编码 +> ``` + +``` +/*服务器,告诉你,你来得时间,把这个时间封装成为一个新建,你下次带来,我就知道你来了*/ +public class CookieDemo03Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //解决中文的乱码问题 + req.setCharacterEncoding("utf-8"); + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); //不加这个,在浏览器访问时候会显示为乱码 + + PrintWriter out = resp.getWriter(); + // + Cookie[] cookies = req.getCookies(); + if (cookies!=null){ + //如果存在怎么办?我们取出来数据 + out.write("你上一次访问的时间是:"); + for (int i = 0; i < cookies.length; i++) { + Cookie cookie = cookies[i]; + if (cookie.getName().equals("name")){ + //获取cookie的值 ,因为读取是乱码,所以先进行解码 + String value = URLDecoder.decode(cookie.getValue(),"utf-8"); //拿出来的是字符串,要转换成长整型才能变成时间 + System.out.println(value); + out.write(value); + } + } + }else { + out.write("你是第一次访问!"); + } + + //服务器给客户端响应一个cookie + Cookie cookie = new Cookie("name", URLEncoder.encode("秦僵","utf-8")); + cookie.setMaxAge(24*60*60); + resp.addCookie(cookie); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); + } +} +``` + +### 10.4 session(重点) + +![image-20230106233833715](../study-notes-imgs/image-20230106233833715.png) + +什么是session + +``` +- 服务器会给每一个用户(浏览器)创建一个Session对象 +- 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在 +- 用户登录之后,整个网站都可以访问他-->保存购物车的信息 +``` + +HttpSession的相关重点方法 + + + +Session和cookie的区别: + +``` +- cookie是把用户数据写给用户的浏览器,浏览器保存(可以保存多个) +- session是把用户的数据写到用户独占的session中,在服务器端保存(保存重要的信息,减少服务器资源浪费); +- session对象由服务器创建, +``` + +session使用场景: + + 保存用户的登录信息,只要用户不关闭浏览器,信息都存在 + + 购物车信息也可以保存在session + + 经常在整个项目中被使用的数据,我们将它保存在Session中 + +使用session的示例代码: + +``` +public class SessionDemoServlet1 extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //解决乱码 + req.setCharacterEncoding("utf-8"); + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html; charset=utf-8"); + //得到session + HttpSession session = req.getSession(); + //给Session中存东西 + session.setAttribute("name", new Person("秦僵",18)); + //获取session的id + String sessionId = session.getId(); + //判断是否是新创建的Session + if (session.isNew()) { + resp.getWriter().write("Session创建成功了! Session的ID:" + sessionId); + } else { + resp.getWriter().write("Session 已经在服务器中存在了!sessionId: " + sessionId); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); + } +} +public class SessionDemoServlet2 extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //解决乱码 + req.setCharacterEncoding("utf-8"); + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html; charset=utf-8"); + //得到session + HttpSession session = req.getSession(); + //给Session中存东西 + Person person = (Person)session.getAttribute("name"); + //获取session的id + String sessionId = session.getId(); + //判断是否是新创建的Session + if (session.isNew()) { + resp.getWriter().write("Session创建成功了! Session的ID:" + sessionId+"\n"); + } else { + resp.getWriter().write("Session 已经在服务器中存在了!sessionId: " + sessionId); + resp.getWriter().write("session.getAttribute(\"name\"):"+person.toString()); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); + } +} +public class SessionDemoServlet3 extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //解决乱码 + req.setCharacterEncoding("utf-8"); + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html; charset=utf-8"); + //得到session + HttpSession session = req.getSession(); + //移除name这个属性 + session.removeAttribute("name"); + //手动注销session,一旦注销sessionid就么有了,浏览器再访问就是生成新的sessionid + session.invalidate(); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); + } +} +``` + +会话自动过期,Web.xml配置 + +``` + + + + 15 + +``` diff --git "a/study-notes/345円217円215円345円260円204円345円222円214円346円263円250円350円247円243円.md" "b/study-notes/345円217円215円345円260円204円345円222円214円346円263円250円350円247円243円.md" new file mode 100644 index 0000000..1fa66c5 --- /dev/null +++ "b/study-notes/345円217円215円345円260円204円345円222円214円346円263円250円350円247円243円.md" @@ -0,0 +1,1154 @@ +# 1、注解(Annotation) + +## **1.1 什么是注解(Annotation)** + +注解不是程序本身,可以在程序编译、类加载和运行时被读取,并执行相应的处理。注解的格式为"@注释名(参数值)",可以附加在包、类、方法和字段上,通过反射机制实现实现注解的访问。 + +**注解(Annotation)相当于一种标记,在程序中加入注解就等于为程序打上某种标记**,没有加,则等于没有任何标记,以后,javac编译器、开发工具和其他程序可以通过反射来了解你的类及各种元素上有无何种标记,看你的程序有什么标记,就去干相应的事,**标记可以加在包、类,属性、方法,方法的参数以及局部变量上。** + +## 1.2内置注解 + +JDK1.5之后内部提供的三个注解 + + @Deprecated 意思是"**废弃的,过时的**" + + @Override 意思是"**重写、覆盖**" + + @SuppressWarnings 意思是"**压缩警告**",作用:用于抑制编译器产生警告信息。 + +​ 该注解作用的类、方法和属性会取消显示编译器警告,其参数主要是进行警告说明以及取消(unchecked)等。 + +```java + +public class AnnotationTest { + /** + * @param args + * 这里就是注解,称为压缩警告,这是JDK内部自带的一个注解,一个注解就是一个类,在这里使用了这个注解就是创建了 SuppressWarnings类的一个实例对象 + */ + public static void main(String[] args) { + sayHello(); + //这里的sayHello()方法画了一条横线表示此方法已经过时了,不建议使用了 + + @SuppressWarnings("unused") + String str = "sdfd"; + } + + /** + * 这也是JDK内部自带的一个注解,意思就是说这个方法已经废弃了,不建议使用了 + 该注解表示某个属性、方法或类等已过时(程序员不鼓励使用的程序元素,通常是因为它是危险的,或者因为存在更好的替代方法),当其他程序使用已过时的属性、方法或者类时,编译器会给出警告(删除线)。 + +/** + +* 该注解表示此方法已过时,存在危险,不推荐使用,其有代替方法,如果继续使用会通过删除线进行标识 + +*/ + */ + @Deprecated + public static void sayHello() { + System.out.println("hi,xxxx"); + } + + /** + * 这也是JDK1.5之后内部提供的一个注解,意思就是要重写(覆盖)JDK内部的toString()方法 + * @return + */ + @Override + public String toString() { + return "xxxx"; + } +} +``` + +## 1.3自定义注解,元注解 + +**元注解** + +> 元注解就是用来注解其他注解,java提供了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型作说明. +> +> 这些类型和他们所支持的类在`java.lang.annotation`包中可以找到(**`@Target`, `@Retention`,`@Documented`, `@Inherited`** ) + +- **@Target**:用于描述注解的使用范围(即被描述的注解可以用在什么地方) + +- **@Retention**:表示需要在什么级别保存该注释信息,用于描述注解的声明周期(**SOURCE 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。 + +## 2.1反射机制是什么 + +> 面试有可能会问到,这句话不管你能不能理解,但是你只要记住就可以了 + +反射机制就是在运行状态中,**对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性**;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。 + +用一句话总结就是反射可以实现在**运行**时可以知道**任意一个类**的**属性和方法** + +**动态语言**,是一种在运行时可以改变其结构的语言,例如新的函数、对象甚至代码可以被引进,他有的函数可以被删除或是其他结构上的变化,通俗点说就是代码可以根据某些条件改变自身结构 + +主要动态语言 Object-c c# javascript php python等 + +**静态语言**相对应的,运行时结构不可变的语言就是静态语言,比如java,c,c++ java不是动态语言,但是java可以称之为动态准语言,既JVA有一定的动态性,我么可以利用反射机制获得类似动态语言的特性,JAVA的动态性让编程的时候更加灵活。 + +Reflection(反射) 是java被视为动态语言的关键,反射机制允许程序在执行期间借助于Reflection API获取任何类的内部信息,并能直接操作任意对象的内部属性及方法 + + Class c = Class.forName("Java.lang.String") + +加载完成之后,在堆内存的方法区就会产生一个Class类型的对象,一个类只有一个Class对象,这个对象就包含了完整的类的结构信息,我们可以通过这个对象看到类的结构,这个对象就像一面镜子,透过这个镜子看到类的结构,所以我们形象的称之为:反射 + +**正常的方式** : 引入需要的包类名称---->通过new实例化--->取得实例化对象 + +**反射方式**:实例化对象---getClass()方法--->获得完整的包类名称 + +**JAVA反射机制提供的功能**: + +``` +- 在运行时判断任意一个对象所属的类 +- 在运行时构造任意一个类的对象 +- 在运行时判断任意一个类所具有的成员变量和方法 +- 在运行时获取泛型的信息 +- 在运行时调用任意一个对象的成员变量和方法 +- 在运行时处理注解 +- 生成动态代理 +- .... +``` + +## 2.2 **JAVA反射机制的优点和缺点** + +``` +- 优点 可以实现动态创建对象和编译,体现了很大的灵活性 +- 缺点,对性能有影响,使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且他满足我们的要求,这类操作总是慢于直接执行相同的操作。 +``` + +## 2.3 **JAVA反射相关的主要API** + +``` +- java.lang.Class 代表一个类 +- java.lang.reflect.Method 代表类的方法 +- java.lang.reflect.Field 代表类的成员变量 +- java.lang.reflect.Constructor 代表类的构造器 +- .................... +``` + +示例代码 + +``` +public class User{ + public static void main(String[] args) throws ClassNotFoundException { + //通过反射获取类的Class对象 + Class c1 = Class.forName("top.aigoo.reflection.pojoUser"); + System.out.println(c1); + System.out.println(c1.hashCode()); + + //通过C2,C3,C4打印的hashCode一样,可以证明一个类在内存中只会有一个Class对象 + Class c2 = Class.forName("top.aigoo.reflection.pojoUser"); + System.out.println(c2.hashCode()); + Class c3 = Class.forName("top.aigoo.reflection.pojoUser"); + System.out.println(c3.hashCode()); + + Class c4 = Class.forName("top.aigoo.reflection.pojoUser"); + System.out.println(c4.hashCode()); + + } +} + +### //实体类 pojo extity什么叫做实体类 ,只有属性的类叫做实体类 +class pojoUser { + private String name; + private int id; + private int age; + + public pojoUser() { + } + + public pojoUser(String name, int id, int age) { + this.name = name; + this.id = id; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} + +``` + +Class类 + +在Object类中定义了以下的方法,此方法被所有子类继承 + + Public final Class getClass() + +以上的方法返回值的类型是一个Class类,此类是Java反射的源头,实际上所谓反射从程序的运行结果来看也很好理解,既可通过对象反射求出类的名称, + +## 2.4 理解Class类并获取Classs实例 + +**Class类的基本概念** + +对象照镜子后可以得到的信息,某个类的属性、方法和构造器,某个类到底实现了哪些接口。对于每个类而言,JRE都保留一个不变的Class类型的对象,一个Class对象包含了特定某个结构(class/interface/enum/annotation/primitive type/void[]) 的有关信息。 + +``` +- Class本身也是一个类; +- Class对象只能由系统建立对象; +- 一个加载的类在JVM中只会有一个Class实例; +- 一个Class 对象对应的是一个加载到JVM的一个.class文件; +- 每个类的实例都会记得自己是由哪个Class实例所生成 +- 通过Class可以完整的得到一个类中所有被加载的结构 +- Class类是Reflection的根源,针对任何你想动态加载、运行的类,唯有先获得相应的Class对象 +``` + +**Classs类的常用方法** + +```java +static ClassforName(String name) 返回指定类名name的Class对象 + +Object newInstance() 调用缺省构造函数,返回Class对象的一个实例 + +getName() 返回此Class对象所表示的实体(类,接口,,数组类或void)的名称 + +Class getSuperClass() 返回当前Class对象的父类的Class对象 + +Class [] getinterfaces() 获取当前Class对象的接口 + +ClassLoader getClassLoader() 返回该类的类加载器 + +Constrtuctor [] getConstructors() 返回一个包含某些Constructor对象的数组 + +Method getMethed(String name,Class...T) 返回一个Method对象,此对象的形参类型为paramType + +Field[] getDeclaredFields() 返回Field对象的一个数组 +``` + +**获取Class类的实例** + +- 如果已知具体的类名,通过类的class属性获取,该方法最为安全可靠,程序性能最高,Class clazz = Person.class; +- 已知某个类的实例,调用该实例的getClass()方法获取Class对象 Class clazz = person.getClass(); +- 已知一个类的类名和存储路径,且该类在类路径下,可通过Class类的静态方法forName()获取,可能抛出ClassNotFoundException Class clazz =Class.forName("demo01.Student"); +- 内置基本类型数据可以直接用类名 .Type +- 还可以利用ClassLoader我们之后讲解 + +实例代码: + +``` +public class test03 { + public static void main(String[] args) throws ClassNotFoundException { + Person person = new Student(); + System.out.println("这个人是" + person.name); + + //方式一:通过Person实例对象获取类Class + Class c1 = person.getClass(); + System.out.println(c1.hashCode()); + + //方式二:通过Class.forName()获取class + Class c2 = Class.forName("top.aigoo.reflection.Student"); + System.out.println(c2.hashCode()); + + //方式三:通过类名获取Class对象 + Class c3 = Student.class; + System.out.println(c3.hashCode()); + + //方式四:基本内置类型的包装类都有一个Type属性 + Class c4 = Integer.TYPE; + System.out.println(c4); + + //获取父类类型 通过对象person中person.getClass()获取person的类Student ,然后通过这个c1的c1.getSupercLass()获取父类 + Class c5 = c1.getSuperclass(); + System.out.println(c5); + } +} + +class Person{ + String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } +} + +class Student extends Person{ + public Student() { + this.name="-->学生"; + } +} + +class Teacher extends Person{ + public Teacher() { + this.name="-->老师"; + } +} + + + +460141958 +460141958 +460141958 +int +class Person +``` + +**那些类型可以有Class对象** + +```java + class 外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类 + + interface:接口 + + [] 数组 + + enum 枚举 + + annotation 注解@interface + + primitive type: 基本数据类型 + + void +``` + +示例代码 + +``` +public class test04 { + private int age; + + public static void main(String[] args) { + Class c1 = test04.class; + Class c2 = String[].class; + Class c3 = int[][].class; + Class c4 = Object.class; + Class c5 = Comparable.class; + Class c6 = Override.class; + Class c7 = ElementType.class; + Class c8 = void.class; + Class c9 = Integer.class; + +// System.out.println("Class的getSuperclass:"+c1.getSuperclass());//Class的getSuperclass:class java.lang.Object +// System.out.println("Class的getName:"+c1.getName());//Class的getName:top.aigoo.reflection.test04 +// System.out.println("Class的getSimpleName:"+c1.getSimpleName());//Class的getSimpleName:test04 +// System.out.println("Class的getTypeName:"+c1.getTypeName());//Class的getTypeName:top.aigoo.reflection.test04 +// System.out.println("Class的getClasses:"+c1.getClasses()); +// System.out.println("Class的getFields:"+c1.getFields()); +// System.out.println("Class的getClassLoader():"+c1.getClassLoader()); +// System.out.println("Class的getClassLoader():"+c1.getMethods()[0]); + + + System.out.println(c1); + System.out.println(c2); + System.out.println(c3); + System.out.println(c4); + System.out.println(c5); + System.out.println(c6); + System.out.println(c7); + System.out.println(c8); + System.out.println("c9"+c9); + + //只要元素类型和维度一样,就是同一个Class + int[] a = new int[10]; + int[] b = new int[100]; + System.out.println(a.getClass().getSimpleName()); + System.out.println(b.getClass().getSimpleName()); + } +} +``` + +> 只要元素类型和维度一样,就是同一个class + +## 2.5 类的加载与ClassLoader + +### 一、什么是ClassLoader? + +大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程序的一个入口函数来调用系统的相关功能,而这些功能都被封装在不同的class文件当中,所以经常要从这个class文件中要调用另外一个class文件中的方法,如果另外一个文件不存在的,则会引发系统异常。而程序在启动的时候,并不会一次性加载程序所要用的所有class文件,而是根据程序的需要,通过Java的类加载机制(ClassLoader)来动态加载某个class文件到内存当中的,从而只有class文件被载入到了内存之后,才能被其它class所引用。所以ClassLoader就是用来动态加载class文件到内存当中用的。 + +### 二、Java默认提供的三个ClassLoader + +1. **BootStrap ClassLoader**:称为启动类加载器,是Java类加载层次中最顶层的类加载器,负责加载JDK中的核心类库,如:rt.jar、resources.jar、charsets.jar等,可通过如下程序获得该类加载器从哪些地方加载了相关的jar或class文件: + + + +```jsx +URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs(); +for (int i = 0; i < urls.length; i++) { + System.out.println(urls[i].toExternalForm()); +} + +以下内容是上述程序从本机JDK环境所获得的结果: +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/resources.jar +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/rt.jar +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/sunrsasign.jar +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/jsse.jar +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/jce.jar +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/lib/charsets.jar +file:/C:/Program%20Files/Java/jdk1.6.0_22/jre/classes/ +``` + +其实上述结果也是通过查找sun.boot.class.path这个系统属性所得知的。 + `System.out.println(System.getProperty("sun.boot.class.path"));` + + + +```css +打印结果:C:\Program Files\Java\jdk1.6.0_22\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_22\jre\classes +``` + +1. **Extension ClassLoader**:称为扩展类加载器,负责加载Java的扩展类库,默认加载`JAVA_HOME/jre/lib/ext/`目下的所有jar。 +2. **App ClassLoader**:称为系统类加载器,负责加载应用程序`classpath`目录下的所有jar和class文件。 + 注意: 除了Java默认提供的三个ClassLoader之外,用户还可以根据需要定义自已的ClassLoader,而这些自定义的ClassLoader都**必须继承**自java.lang.ClassLoader类,也包括Java提供的另外二个ClassLoader(Extension ClassLoader和App ClassLoader)在内,但是Bootstrap ClassLoader不继承自ClassLoader,因为它不是一个普通的Java类,底层由C++编写,已嵌入到了JVM内核当中,当JVM启动后,Bootstrap ClassLoader也随着启动,负责加载完核心类库后,并构造Extension ClassLoader和App ClassLoader类加载器。 + +### 三、ClassLoader加载类的原理 + +##### 1、原理介绍 + +> 双亲委派模型工作过程是:如果一个类加载器收到类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器完成。每个类加载器都是如此,只有当父加载器在自己的搜索范围内找不到指定的类时(即ClassNotFoundException),子加载器才会尝试自己去加载。 + +ClassLoader使用的是*双亲委托模型*来搜索类的,每个ClassLoader实例都有一个父类加载器的引用(不是继承的关系,是一个包含的关系),虚拟机内置的类加载器(Bootstrap ClassLoader)本身没有父类加载器,但可以用作其它ClassLoader实例的的父类加载器。当一个ClassLoader实例需要加载某个类时,它会试图亲自搜索某个类之前,先把这个任务委托给它的父类加载器,这个过程是由上至下依次检查的,首先由最顶层的类加载器Bootstrap ClassLoader试图加载,如果没加载到,则把任务转交给Extension ClassLoader试图加载,如果也没加载到,则转交给App ClassLoader 进行加载,如果它也没有加载得到的话,则返回给委托的发起者,由它到指定的文件系统或网络等URL中加载该类。如果它们都没有加载到这个类时,则抛出ClassNotFoundException异常。否则将这个找到的类生成一个类的定义,并将它加载到内存当中,最后返回这个类在内存中的Class实例对象。 + +##### 2、为什么要使用双亲委托这种模型呢? + +因为这样可以避免重复加载,当父亲已经加载了该类的时候,就没有必要子ClassLoader再加载一次。考虑到安全因素,我们试想一下,如果不使用这种委托模式,那我们就可以随时使用自定义的String来动态替代java核心api中定义的类型,这样会存在非常大的安全隐患,而双亲委托的方式,就可以避免这种情况,因为String已经在启动时就被引导类加载器(Bootstrcp ClassLoader)加载,所以用户自定义的ClassLoader永远也无法加载一个自己写的String,除非你改变JDK中ClassLoader搜索类的默认算法。 + + + +**什么时候回发生类的初始化** + +- 类的主动引用(一定会发生类的初始化) + - 当虚拟机启动时,会初始化main方法所在的类 + - new一个新的对象的时候 + - d调用类的静态成员(除了final常量)和静态方法 + - 使用java.lang.reflec包的方法对垒进行反射调用 + - 当初始化一个类,如果其父类没有被初始化,则贤惠初始化它的父类 +- 类的被动引用(不会发生类的初始化) + - 当访问一个静态域时候,只有真正声明这个域的类才会被初始化,如当通过子类引用父类的静态变量,不会导致子类初始化 + - 通过数组定义类引用,不会触发此类的初始化 + - 引用常量不会触发此类的初始化(常量在连接阶段就存入调用类的常量池中了) + +演示代码 + +``` +package com.kuang.reflection; + +public class Test06 { + + static { + System.out.println("main类被初始化加载"); // 当虚拟机启动,先初始化main方法所在的类 + } + public static void main(String[] args) throws ClassNotFoundException { + //下面会导致类的初始化 + + // Son son = new Son(); //1.new 一个类的对象,发生类的初始化 + Class aClass = Class.forName("com.kuang.reflection.Son");//反射产生类的初始化 + System.out.println(Son.b); //调用类的静态成员和静态方法 发生类的初始化 + //初始化一个类,如果父类没有被初始化,会先初始化他的父类 + //不会产生类的引用的方法 + //System.out.println(Son.b); //调用父类的静态变量、方法不会产生类的初始化 + //Son[] array = new Son[5]; //生成数组,不会产生类的初始化 + //System.out.println(Son.M);//引用常量,也不会产生此类的初始化(常量在连接阶段就存入调用类的常量池h) + } +} + +class Father{ + static int b =2; + static { + System.out.println("static方法:父类被加载!"); + } +} + +class Son extends Father{ + static { + System.out.println("static方法: 子类被加载"); + m= 300; + } + static int m =100; + static final int M = 1; +} +``` + +## 2.6 **类加载器的作用** + +``` +- 将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区运行时的数据结构。然后再堆中生成一个代表这个类的java.lang.Class对象,作为方法区中数据的访问入口 +- 类缓存 标准的JAVASE类加载器可以按要求查找类,但一旦某个类被加载到类加载器中,它将维持加载(缓存)一段时间,不过JVM垃圾回收机制可以回收这些class对象 +``` + +类加载器的作用 : + +就是把类装载进入内存中,JVM规范定了如下类型的类的加载器 + +- **引导类加载器**,用C++编写的,是JVM自带的类加载器,负责JAVA平台和辛苦,用来装载核心类库,该加载器无法直接获取 + +- 扩展类加载器 + + ,负责jre/lib/ext目录下jar包或者 D Java.ext.dirs指定目录下的jar包装入工作裤 + + - **系统类加载器** 负责java-classpath 或者 -D java.class.path 所指的目录下类与jar包装入工作,是最常用的加载器 + +示例代码: + +``` +public class test05 { + public static void main(String[] args) throws ClassNotFoundException { + //获取系统类的加载器 + ClassLoader loader = ClassLoader.getSystemClassLoader(); + System.out.println(loader); + //获取系统类加载器的父类加载器-->扩展类加载器 + ClassLoader loader1 = loader.getParent(); + System.out.println(loader1); + //获取扩展类加载器的父类加载器--->根加载器(c/c++) + ClassLoader loader2 = loader1.getParent(); + System.out.println(loader2); + + //测试当前类是哪个类加载的 + ClassLoader loader3 = Class.forName("top.aigoo.reflection.test05").getClassLoader(); + System.out.println(loader3); +// 测试JDK内置的类是谁加载的 + ClassLoader loader4 = Class.forName("java.lang.Object").getClassLoader(); + System.out.println(loader4); + //获取系统类加载器都可以加载哪些路径 + System.out.println(System.getProperty("java.class.path")); + } +} +输出结果: +sun.misc.Launcher$AppClassLoader@18b4aac2 +sun.misc.Launcher$ExtClassLoader@74a14482 +null +sun.misc.Launcher$AppClassLoader@18b4aac2 +``` + +## 2.7 创建运行时类的对象 + +通过反射获取运行时类的完整结果 Field、Method、Constructor、Superclass、Interface、Annotation + +``` +- 实现的全部接口 +- 所继承的父类 +- 全部的构造器 +- 全部的方法 +- 全部的Field +- 注解..... +- +``` + +示例代码 + +``` +public class test06 { + public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException { + Class c1 = Class.forName("top.aigoo.reflection.pojoUser"); + + System.out.println(c1.getName());//获得全路径类名 + System.out.println(c1.getSimpleName()); //获得类名 + + //获取类的属性 + Field[] fields = c1.getFields();//只能获取Public属性 + for (Field field : fields) { + System.out.println(field); + } + + Field[] fields1 = c1.getDeclaredFields(); //获得所有属性 + for (Field field : fields1) { + System.out.println(field); + } + + Field field = c1.getDeclaredField("name"); + System.out.println(field); + //获得类的方法 + Method[] methods = c1.getMethods();//获取本类及其父类的全部Public方法 + + for (Method method : methods) { + System.out.println(method); + } + Method[] methods1 = c1.getDeclaredMethods();//获取本类的全部方法 + for (Method method : methods1) { + System.out.println("本类:::"+method); + } + //获取指定方法 + Method getName = c1.getMethod("getName", null); + Method setName = c1.getMethod("setName", String.class); + + System.out.println("getName:"+getName); + System.out.println("setName:"+setName); + + } +} +运行结果 + top.aigoo.reflection.pojoUser + pojoUser + private java.lang.String top.aigoo.reflection.pojoUser.name + private int top.aigoo.reflection.pojoUser.id + private int top.aigoo.reflection.pojoUser.age + private java.lang.String top.aigoo.reflection.pojoUser.name + + Process finished with exit code 0 +``` + +小结 + +``` +- 在实际的操作中,取得类的信息的操作代码,并不会经常开发 +- 一定要熟悉java.lang.reflect包的作用,反射机制 +- 如何取得属性、方法、构造器的名称,修饰符等 +``` + +## 2.8 获取运行时类的完整结构 + +``` +public class Test02 { + public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException { + Class c1 = Class.forName("com.kuang.reflection.User"); + System.out.println("==============================="); + System.out.println(c1.getName()); + System.out.println(c1.getSimpleName()); + //获取类的属性 + System.out.println("==============================="); + + Field[] fields = c1.getFields(); //获取本类的所有属性 + for (Field field : fields) { + System.out.println(field); + } + fields = c1.getDeclaredFields(); //获取本类的所有属性 + for (Field field : fields) { + System.out.println(field); + } + + //获得类的方法 + System.out.println("==============================="); + Method[] methods = c1.getMethods();//获得本类及其父类的全部public方法 + for (Method method : methods) { + System.out.println("正常的:"+method); + } + methods =c1.getDeclaredMethods();//获取本类的所有方法 + for (Method method : methods) { + System.out.println("getDeclaredMethods():"+method); + } + + //获得指定的方法 + //重载 + System.out.println("==============================="); + Method getName = c1.getMethod("getName", null); + Method setName = c1.getMethod("setName", String.class); + System.out.println(getName); + System.out.println(setName); + + //获得指定的构造器 + System.out.println("==============================="); + Constructor[] constructors = c1.getConstructors();//获得public构造器 + for (Constructor constructor : constructors) { + System.out.println(constructor); + } + constructors= c1.getDeclaredConstructors(); //获得所有的构造器 + for (Constructor constructor : constructors) { + System.out.println("getDeclaredConstructors():"+constructor); + } + + Constructor constructor = c1.getConstructor(String.class, String.class, int.class);//获取指定构造器 + System.out.println(constructor); + + } +} +``` + +**有了Class对象可以做什么?** + +- 创建类的对象可调用Class对象的newInstance()方法, +- 类必须有一个无参数的构造器, + - 类的构造器的访问权限需要足够 + +``` +//动态的创建对象,通过反射 +public class Test09 { + public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + //获得Class对象 + Class c1 = Class.forName("com.kuang.reflection.User"); + //构造一个对象 + User user = (User) c1.newInstance();//本质这种是调用类的无参构造器来创建一个User的实例 + System.out.println(user); + + } +} +``` + +**思考**,难道没有无参构造器就不能创建对象了吗,只要在操作的时候明确的调用类中的构造器,并将参数传递进入之后,就可以实例化操作 + +- 步骤 + - 通过Class类的getDeclaredConstructor(Class ...parameterTypes)取得本类的指定形参类型的构造器 + - 向构造器的形参中传递一个对象数组进去,里面包含了构造器中所需要的的各个参数 + - 通过Constructor实例化对象 + +示例代码 + +``` +public class Test07 { + public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { + Class c1 = Class.forName("top.aigoo.reflection.pojoUser"); + //#1 使用镜像创建对象 + pojoUser user = (pojoUser) c1.newInstance(); + System.out.println(user); + + //#2 通过构造器创建对象 + Constructor constructor = c1.getConstructor(String.class, int.class, int.class); + pojoUser user2 = (pojoUser) constructor.newInstance("秦僵", 001, 18); + System.out.println(user2.getName()); + + pojoUser user3 = (pojoUser) c1.newInstance(); + + //#3 通过反射获取普通方法并使用 + Method setName = c1.getDeclaredMethod("setName", String.class); //先获取类的方法 + setName.invoke(user3,"狂神"); //通过invoke将方法setName和对象user3绑定,同时传进去方法实参 + System.out.println(user3.getName()); + + //#4 通过反射操作属性 + pojoUser user4 = (pojoUser) c1.newInstance(); + Field name = c1.getDeclaredField("name"); + name.setAccessible(true); //关闭权限检测,就可以操作private属性了 + name.set(user4,"狂神2"); //通过set将对象和属性绑定 + System.out.println(user4.getName()); + } +} +``` + +**调用指定方法** + + Object invoke(Object obj,Object.... args) + +``` +- Object对应原方法的返回值,若原方法无返回值,此时返回null +- 若原方法为静态方法,此时形参Object obj可以是null +- 若原方法形参列表为空则Object[]arg为null +- 若原方法声明为private,则需要在调用此invode()方法前,显示调用方法对象的SetAccessable(true)方法,将可访问private方法 +``` + +**setAccessible()方法** + +Method和Field、Constructor对象都设置SetAccessible()方法 + +作用 启动和金庸访问安全设置的开关 + +参数为true则指示反射的对象在使用时应该取消java语言检查访问,这样可提高反射效率,如果代码中必须反射,则该句代码需要频繁的被调用,那么清设置为true,使得原本无法访问的私有成员也可以被访问 + +参数值为false则指示反射的对象应该实施JAVA语言的访问检查 + +设定前后,性能影响很明显 + +``` +public class Test8 { + //普通方式调用十亿次 + public static void test1(){ + pojoUser user = new pojoUser(); + long starttime = System.currentTimeMillis(); + for (int i = 0; i < 1000000000; i++) { + user.getName(); + } + long endtime = System.currentTimeMillis(); + System.out.println("普通方式执行:"+(endtime - starttime)+" ms"); + } + + //使用反射方式调用十亿次 + public static void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { + pojoUser user = new pojoUser(); + + Class c1 = user.getClass(); //获取Class对象 + pojoUser user_mirror = (pojoUser)c1.newInstance(); //根据Class对象获取pojoUser实例 + Method getName = c1.getDeclaredMethod("getName", null); //通过Class对象获取pojoUser的方法 + + + long starttime = System.currentTimeMillis(); + for (int i = 0; i < 1000000000; i++) { + getName.invoke(user_mirror,null); //通过Method 的invode激活方法 + } + long endtime = System.currentTimeMillis(); + System.out.println("反射方式执行:"+(endtime - starttime)+"ms"); + } + + //关闭检测调用十亿次 + public static void test3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { + pojoUser user = new pojoUser(); + + Class c1 = user.getClass(); //获取Class对象 + pojoUser user_mirror = (pojoUser)c1.newInstance(); //根据Class对象获取pojoUser实例 + Method getName = c1.getDeclaredMethod("getName", null); //通过Class对象获取pojoUser的方法 + getName.setAccessible(true);//关闭检测 + + long starttime = System.currentTimeMillis(); + for (int i = 0; i < 1000000000; i++) { + getName.invoke(user_mirror,null); //通过Method 的invode激活方法 + } + long endtime = System.currentTimeMillis(); + System.out.println("关闭检测执行:"+(endtime - starttime)+"ms"); + } + + public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + test1(); + test2(); + test3(); + } +} +输出结果: + 普通方式执行:4ms + 反射方式执行:1623ms + 关闭检测执行:1059ms +``` + +## 2.9 调用运行时类的指定结构 + +**反射操作泛型** + +- JAVA采用泛型擦除的机制来引入泛型,JAVA中的泛型仅仅是给编译器JAVAC使用的,确保数据的安全性和免去强制类型转换的问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除 +- 为了通过反射操作这些类型,JAVA新增了parameterizedType,GenericArrayType,TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原生类型齐名的类型 +- ParameterizedType 表示一种参数化类型,比如Collection +- GenericArrayType 表示一种元素类型是参数化类型或者类型变量的数组元素 +- TypeVariable 是各种类型变量的公共父接口 +- WildcardType 代表一种通配符类型表达式 + +实例代码 + +``` +//通过反射获取泛型 +public class Test09 { + //带有泛型参数的方法 + public void test1(Map map, List list){ + System.out.println("我是test1方法"); + } + //带有泛型返回值的方法 + public List test1(){ + System.out.println("我是test2方法"); + + return null; + } + + public static void main(String[] args) throws NoSuchMethodException { + Method method = Test09.class.getMethod("test1", Map.class, List.class); + //获取方法的返回泛型 + Type[] genericParameterTypes = method.getGenericParameterTypes(); + + for (Type genericParameterType : genericParameterTypes) { + System.out.println("#-->" + genericParameterType); + System.out.println("===="); + if (genericParameterType instanceof ParameterizedType){ + Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments(); + for (Type actualTypeArgument : actualTypeArguments) { + System.out.println(actualTypeArgument); + } + } + } + } + +} +``` + +## 2.10 **反射操作注解** + +示例代码 + +``` +//练习反射操作注解 +public class Test10 { + public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { + Class c1 = Test10.class; + //step1 或者Class类对象 + Class c2 = Class.forName("top.aigoo.reflection.Student2"); + //通过反射获得注解 + Annotation[] annotations = c2.getAnnotations(); + for (Annotation annotation : annotations) { + System.out.println("annotation-->"+annotation); + } + + //通过反射获得注解的value值 + Tablekuang tablekuang = (Tablekuang)c2.getAnnotation(Tablekuang.class); + String value = tablekuang.value(); + System.out.println("tablekuang.Value()-->"+tablekuang.Value()); //通过.Value()直接获取指定注解的值 + + //通过反射 获得类的 指定属性的 注解 + Field field = c2.getDeclaredField("name"); + Fieldkuang annotation = (Fieldkuang)field.getAnnotation(Fieldkuang.class); + System.out.println("annotation.columenName-->"+annotation.columenName()); + System.out.println("annotation.type-->"+annotation.type()); + System.out.println("annotation.length-->"+annotation.length()); + + Field field2 = c2.getDeclaredField("id"); + Fieldkuang annotation2 = (Fieldkuang)field.getAnnotation(Fieldkuang.class); + System.out.println("annotation2.columenName-->"+annotation2.columenName()); + System.out.println("annotation2.type-->"+annotation2.type()); + System.out.println("annotation2.length-->"+annotation2.length()); + } +} +@Tablekuang(Value = "db_student") +class Student2{ + @Fieldkuang(columenName = "db_id",type = "int",length = 10) + private int id; + @Fieldkuang(columenName = "db_age",type = "int",length = 10) + private int age; + @Fieldkuang(columenName = "db_name",type = "varchar",length = 3) + private String name; + + public Student2(int id, int age, String name) { + this.id = id; + this.age = age; + this.name = name; + } + + public Student2() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Student2{" + + "id=" + id + + ", age=" + age + + ", name='" + name + '\'' + + '}'; + } +} + +//注解类名 +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@interface Tablekuang{ + String Value(); +} + +//属性注解 +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@interface Fieldkuang{ + String columenName(); + String type(); + int length(); +} +``` \ No newline at end of file diff --git "a/study-notes/350円256円276円350円256円241円346円250円241円345円274円217円.md" "b/study-notes/350円256円276円350円256円241円346円250円241円345円274円217円.md" new file mode 100644 index 0000000..64c9546 --- /dev/null +++ "b/study-notes/350円256円276円350円256円241円346円250円241円345円274円217円.md" @@ -0,0 +1,110 @@ +设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。 + + + +具体模式如下: + +**1,创建型模式**:这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。 + +工厂模式(Factory Pattern) + +抽象工厂模式(Abstract Factory Pattern) + +单例模式(Singleton Pattern) + +建造者模式(Builder Pattern) + +原型模式(Prototype Pattern) + +**2,结构型模式:**这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。 + +适配器模式(Adapter Pattern) + +桥接模式(Bridge Pattern) + +过滤器模式(Filter、Criteria Pattern) + +组合模式(Composite Pattern) + +装饰器模式(Decorator Pattern) + +外观模式(Facade Pattern) + +享元模式(Flyweight Pattern) + +代理模式(Proxy Pattern) + +**3,行为型模式:**这些设计模式特别关注对象之间的通信。 + +责任链模式(Chain of Responsibility Pattern) + +命令模式(Command Pattern) + +解释器模式(Interpreter Pattern) + +迭代器模式(Iterator Pattern) + +中介者模式(Mediator Pattern) + +备忘录模式(Memento Pattern) + +观察者模式(Observer Pattern) + +状态模式(State Pattern) + +空对象模式(Null Object Pattern) + +策略模式(Strategy Pattern) + +模板模式(Template Pattern) + +访问者模式(Visitor Pattern) + + + + + +### 创建型模式:关注对象的创建过程 + +1. **[单例模式](http://www.cnblogs.com/meet/p/5116398.html)**:保证一个类只有一个实例,并且提供一个访问该实例的全局访问点 主要: 饿汉式(线程安全,调用效率高,但是不能延时加载) 懒汉式(线程安全,调用效率不高,但是可以延时加载) 其他: 双重检测锁式(由于JVM底层内部模型原因,偶尔会出问题。不建议使用) 静态内部类式(线程安全,调用效率高。但是可以延时加载) 枚举单例(线程安全,调用效率高,不能延时加载) +2. **[工厂模式](http://www.cnblogs.com/meet/p/5116400.html)**: 简单工厂模式:用来生产同一等级结构中的任意产品(对已有产品新增功能,需要修改源代码)虽然能通过工厂来创建对象,但是违反了开闭原则。一旦增加功能需要在原有基础上修改代码。 工厂方法模式:用来生产同一等级结构中的固定产品(支持增加任意产品,不用修改源代码)将工厂类调整为工厂接口,需要什么类型的工厂就使用该类实现该工厂,创建相应的产品。 +3. **[抽象工厂模式](http://www.cnblogs.com/meet/p/5116400.html)**:用来生产不同产品族的全部产品(对于增加新的产品,无能为力。可增加一系列的产品族)抽象工厂模式用来生产不同产品族的全部产品,对于只增加产品某一部分则不适用。抽象工厂模式是工厂模式的一种升级版本。 +4. **[建造者模式](http://www.cnblogs.com/meet/p/5116401.html)**:分离了对象子组件的单独构造(由Builder来负责构建,由Director来负责组装),从而可以构造出复杂的对象。 假如一个对象的构建很复杂,需要很多步骤。如果使用了建造者模式,会将其构建对象,和组装成一个对象这两步给分开来. 构建部分为(builder)和组织部分(director),实现了构建和装配的解耦。 +5. **[原型模式](http://www.cnblogs.com/meet/p/5116404.html)**:通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。所谓原型模式就是构建一个对象的原型,通过原型来创建出新的对象。 + +### 行为型模式:关注系统中对象之间的相互交互,研究系统在运行时对象之间的相互通信和协作,进一步明确对象的职责。 + +1. **[模板方法模式](http://www.cnblogs.com/meet/p/5116417.html)**:定义一个操作中的算法骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的 某些特定步骤。也就是说:假如某些操作代码基本相同,只是其中一部分会经常改变,则可以使用模板方法,将不变的部分作为一个模板,将容易变动的 部分让子类来实现。 +2. **[命令模式](http://www.cnblogs.com/meet/p/5116430.html)**(不常用):将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志。以及支持可撤销的操作。说白了,就是将一系列的请求命令封装起来,不直接调用真正执行者的方法,这样比较好扩展。 +3. **[迭代器模式](http://www.cnblogs.com/meet/p/5116437.html)**:提供一种方法顺序的访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。也就是开发一个用于迭代某个对象中元素的功能。 一般我们开发时很少自己开发,因为jdk内置了迭代器。 +4. **[观察者模式](http://www.cnblogs.com/meet/p/5116411.html)**:也叫(发布-订阅模式)定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生变化时,会通知所有观察者对象。使它们能够自动更新自己。例如:发广播,游戏中大喇叭,群聊 jdk中提供了抽象主题和抽象观察者的接口,我们可以使用这两个接口来方便的定义自己的观察者模式 +5. **[中介者模式](http://www.cnblogs.com/meet/p/5116432.html)**:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显示地相互引用。从而使其耦合松散,而且可以独立地改变它们之间的交互。中介者模式核心:1.就是将网状结构处理成星型结构 2.将多对多处理成一对多 +6. **[备忘录模式](http://www.cnblogs.com/meet/p/5116407.html)**:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。 也就是说,不破坏源数据的情况下,将源数据进行一次或者多次的备份。 +7. **[解释器模式](http://www.cnblogs.com/meet/p/5116424.html)**(不常用):给定一个语言,定义它的文法一种表示。并定义一个解释器,这个解释器使用该表示来解释语言中的句子。 +8. **[状态模式](http://www.cnblogs.com/meet/p/5116415.html)**:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。当遇到不同的状态遇到不同的行为的时候,可以使用状态模式 例如:酒店房间的状态,是空闲,预定,还是已入住呢?而且这三种状态都对应着不同的行为,空闲的话就可预定,可预定的话就可入住,已入住的话 就不能显示空闲。 +9. **[策略模式](http://www.cnblogs.com/meet/p/5116419.html)**:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户。 例如:商场中的打折商品,针对不同的客户可能有不同的折扣。那这些算法该怎么定义呢?可以使用策略模式,给每个会员或者商品都指定一个特定的算法,根据不同的算法,打不同的折扣。 +10. **[职责链模式](http://www.cnblogs.com/meet/p/5116440.html)**:将能够处理同一类请求的对象连成一条链,使这些对象都有机会处理请求,所提交的请求沿着链传递。从而避免请求的 发送者和接受者之间的耦合关系。链上的对象逐个判断是否有能力处理该请求,如果能则就处理,如果不能,则传给链上的下一个对象。 直到有一个对象处理它为止。比如:请假申请,审批人连成一条链,每个人都有机会处理这个假条,但是这个请假申请最终肯定会经过审核(不管审批是否通过)。 +11. **[访问者模式](http://www.cnblogs.com/meet/p/5116422.html)**(不常用):表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。 + +### 结构型模式:关注对象和类的组织 + +1. **[适配器模式](http://www.cnblogs.com/meet/p/5116467.html)**:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的类可以一起工作。 例如:NBA中的球员来自不同国家,而世界标准语言是英语。那他们不会英语,也不能各自学习所有国家的语言。所以,最好的办法就是请不同的翻译 将这些球员国家的语言翻译成英语。而这些翻译就是适配器!! 适配器模式分类: 1、适配器模式(不推荐使用):由于类适配器模式需要多重继承对一个接口对另一个接口进行适配,而C#,Java不支持多重继承。 2、对象适配器模式:采用组合的方式 +2. **[桥接模式](http://www.cnblogs.com/meet/p/5116458.html)**:将抽象部分与它的实现部分分离,使它们都可以独立的变化。而不会直接影响到其他部分。桥接模式解决了多层继承的结构,处理多维度变化的场景,将各个维度设计成独立的继承结构。使各个维度可以独立的扩展在抽象层建立联系。 +3. **[装饰模式](http://www.cnblogs.com/meet/p/5116450.html)**:也叫装饰者模式或者装饰器模式,表示动态的给一个对象添加一些新的功能(利用子类继承父类也可以实现),但是比生成子类方式更灵活。例如:我们每个人身上穿的衣服,鞋子,领带,披风都可以理解为是对人的装饰。 +4. **[组合模式](http://www.cnblogs.com/meet/p/5116455.html)**:将对象组合成树形结构以表示:部分--整体 的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。基本上见到的树形结构都使用到了组合模式。 +5. **[外观模式](http://www.cnblogs.com/meet/p/5116447.html)**:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口。这个接口使得这一子系统更加容易使用。遵守迪米特法则(最少知识原则)。说白了,就可以理解为封装。外观模式的核心:为子系统提供统一的入口,封装子系统的复杂性,便于客户端调用 +6. **[享元模式](http://www.cnblogs.com/meet/p/5116445.html)**:运用共享技术有效的支持大量细粒度对象的重用。它能做到共享的关键就是区分了内部状态(可以共享,不会随环境变化而变化)和外部状态(不可以共享,会随环境变化而变化)。如果项目中有很多完全相同或相似的对象,则可以使用享元模式,节省内存。 +7. **[代理模式](http://www.cnblogs.com/meet/p/5116464.html)**:为其他对象提供一种代理以便控制对这个对象的访问。 可以详细控制访问某个类(对象)的方法,在调用这个方法前作的前置处理(统一的流程代码放到代理中处理)。调用这个方法后做后置处理。 例如:明星的经纪人,租房的中介等等都是代理 代理模式分类: 1、静态代理(静态定义代理类,我们自己静态定义的代理类。比如我们自己定义一个明星的经纪人类) 2、动态代理(通过程序动态生成代理类,该代理类不是我们自己定义的。而是由程序自动生成)比较重要!! + + + +## OOP七大原则简介 + +**开闭原则**:一个实体是允许在不改变它的源代码的前提下变更它的行为(对扩展开放、对修改关闭) +**里氏替换原则**:继承必须确保父类所拥有的性质在子类中仍然成立(例如:长方形不是正方形) +**依赖倒置原则**:程序要依赖于抽象接口,不要依赖于具体实现。(要面向接口编程,不要面向实现编程) +**单一职责原则**:控制类的粒度大小、将对象解耦、提高其内聚性(一个方法干好一件事(原子性)) +**接口隔离原则**:客户端不应该依赖它不需要的接口。一个类对另一个类的依赖应该建立在最小的接口上。(要为各个类建立他们需要的专用接口) +**迪米特法则**:又叫作最少知识原则,一个类对于其他类知道的越少越好,就是说一个对象应当对其他对象有尽可能少的了解,只与你的直接朋友交谈,不跟"陌生人"说话。(不越级汇报。解耦) +**合成复用原则**:尽量先使用组合或者聚合等关联关系来实现,其次才考虑使用继承关系来实现。 + diff --git a/studyDesignCode/.gitignore b/studyDesignCode/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyDesignCode/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyDesignCode/pom.xml b/studyDesignCode/pom.xml new file mode 100644 index 0000000..25a8786 --- /dev/null +++ b/studyDesignCode/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + com.clevermis + studyDesignCode + war + 1.0-SNAPSHOT + studyDesignCode Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + studyDesignCode + + diff --git a/studyDesignCode/src/main/java/com/clevermis/Singleton/Hungry.java b/studyDesignCode/src/main/java/com/clevermis/Singleton/Hungry.java new file mode 100644 index 0000000..af6441f --- /dev/null +++ b/studyDesignCode/src/main/java/com/clevermis/Singleton/Hungry.java @@ -0,0 +1,21 @@ +package com.clevermis.Singleton; + +/** + * @program: java-study + * @description: 饿汉式单例 + * @author: Clevermis + * @create: 2023年01月10日 14:16 + * @since JDK 1.8 + **/ +public class Hungry { + + private static final Hungry HUNGRY = new Hungry(); + + private Hungry() { + } + + public static Hungry getInstance() { + return Hungry.HUNGRY; + } + +} diff --git a/studyDesignCode/src/main/webapp/WEB-INF/web.xml b/studyDesignCode/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..e69de29 diff --git a/studyDesignCode/src/main/webapp/index.jsp b/studyDesignCode/src/main/webapp/index.jsp new file mode 100644 index 0000000..e69de29 diff --git a/studyJavaseCode/reflection/.gitignore b/studyJavaseCode/reflection/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyJavaseCode/reflection/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyJavaseCode/reflection/pom.xml b/studyJavaseCode/reflection/pom.xml new file mode 100644 index 0000000..7190da2 --- /dev/null +++ b/studyJavaseCode/reflection/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + org.example + reflection + war + 1.0-SNAPSHOT + reflection Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + reflection + + diff --git a/studyJavaseCode/reflection/src/main/java/Test10.java b/studyJavaseCode/reflection/src/main/java/Test10.java new file mode 100644 index 0000000..926e9c6 --- /dev/null +++ b/studyJavaseCode/reflection/src/main/java/Test10.java @@ -0,0 +1,108 @@ +import java.lang.annotation.*; +import java.lang.reflect.Field; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月06日 23:15 + * @since JDK 1.8 + **/ +//练习反射操作注解 +public class Test10 { + public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { + Class c1 = Test10.class; + //step1 或者Class类对象 + Class c2 = Class.forName("Student2"); + //通过反射获得注解 + Annotation[] annotations = c2.getAnnotations(); + for (Annotation annotation : annotations) { + System.out.println("annotation-->"+annotation); + } + + //通过反射获得注解的value值 + Tablekuang tablekuang = (Tablekuang)c2.getAnnotation(Tablekuang.class); + String value = tablekuang.Value(); + System.out.println("tablekuang.Value()-->"+tablekuang.Value()); //通过.Value()直接获取指定注解的值 + + //通过反射 获得类的 指定属性的 注解 + Field field = c2.getDeclaredField("name"); + Fieldkuang annotation = (Fieldkuang)field.getAnnotation(Fieldkuang.class); + System.out.println("annotation.columenName-->"+annotation.columenName()); + System.out.println("annotation.type-->"+annotation.type()); + System.out.println("annotation.length-->"+annotation.length()); + + Field field2 = c2.getDeclaredField("id"); + Fieldkuang annotation2 = (Fieldkuang)field.getAnnotation(Fieldkuang.class); + System.out.println("annotation2.columenName-->"+annotation2.columenName()); + System.out.println("annotation2.type-->"+annotation2.type()); + System.out.println("annotation2.length-->"+annotation2.length()); + } +} +@Tablekuang(Value = "db_student") +class Student2{ + @Fieldkuang(columenName = "db_id",type = "int",length = 10) + private int id; + @Fieldkuang(columenName = "db_age",type = "int",length = 10) + private int age; + @Fieldkuang(columenName = "db_name",type = "varchar",length = 3) + private String name; + + public Student2(int id, int age, String name) { + this.id = id; + this.age = age; + this.name = name; + } + + public Student2() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Student2{" + + "id=" + id + + ", age=" + age + + ", name='" + name + '\'' + + '}'; + } +} + +//注解类名 +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@interface Tablekuang{ + String Value(); +} + +//属性注解 +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@interface Fieldkuang{ + String columenName(); + String type(); + int length(); +} diff --git a/studyJavaseCode/reflection/src/main/java/User.java b/studyJavaseCode/reflection/src/main/java/User.java new file mode 100644 index 0000000..1ca8cb1 --- /dev/null +++ b/studyJavaseCode/reflection/src/main/java/User.java @@ -0,0 +1,66 @@ +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月06日 21:33 + * @since JDK 1.8 + **/ +public class User{ + public static void main(String[] args) throws ClassNotFoundException { + //通过反射获取类的Class对象 + Class c1 = Class.forName("pojoUser"); + System.out.println(c1); + System.out.println(c1.hashCode()); + + //通过C2,C3,C4打印的hashCode一样,可以证明一个类在内存中只会有一个Class对象 + Class c2 = Class.forName("pojoUser"); + System.out.println(c2.hashCode()); + Class c3 = Class.forName("pojoUser"); + System.out.println(c3.hashCode()); + + Class c4 = Class.forName("pojoUser"); + System.out.println(c4.hashCode()); + + } +} + +//实体类 pojo extity什么叫做实体类 ,只有属性的类叫做实体类 +class pojoUser { + private String name; + private int id; + private int age; + + public pojoUser() { + } + + public pojoUser(String name, int id, int age) { + this.name = name; + this.id = id; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} + diff --git a/studyJavaseCode/reflection/src/main/java/test03.java b/studyJavaseCode/reflection/src/main/java/test03.java new file mode 100644 index 0000000..c5787af --- /dev/null +++ b/studyJavaseCode/reflection/src/main/java/test03.java @@ -0,0 +1,56 @@ +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月06日 21:46 + * @since JDK 1.8 + **/ +public class test03 { + public static void main(String[] args) throws ClassNotFoundException { + Person person = new Student(); + System.out.println("这个人是" + person.name); + + //方式一:通过Person实例对象获取类Class + Class c1 = person.getClass(); + System.out.println(c1.hashCode()); + + //方式二:通过Class.forName()获取class + Class c2 = Class.forName("Student"); + System.out.println(c2.hashCode()); + + //方式三:通过类名获取Class对象 + Class c3 = Student.class; + System.out.println(c3.hashCode()); + + //方式四:基本内置类型的包装类都有一个Type属性 + Class c4 = Integer.TYPE; + System.out.println(c4); + + //获取父类类型 通过对象person中person.getClass()获取person的类Student ,然后通过这个c1的c1.getSupercLass()获取父类 + Class c5 = c1.getSuperclass(); + System.out.println(c5); + } +} + +class Person{ + String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } +} + +class Student extends Person{ + public Student() { + this.name="-->学生"; + } +} + +class Teacher extends Person{ + public Teacher() { + this.name="-->老师"; + } +} diff --git a/studyJavaseCode/reflection/src/main/java/test04.java b/studyJavaseCode/reflection/src/main/java/test04.java new file mode 100644 index 0000000..9b65300 --- /dev/null +++ b/studyJavaseCode/reflection/src/main/java/test04.java @@ -0,0 +1,50 @@ +import java.lang.annotation.ElementType; + +/** + * @program: java-study + * @description:所有类型的CLASS + * @author: Clevermis + * @create: 2023年01月06日 21:53 + * @since JDK 1.8 + **/ +public class test04 { + private int age; + + public static void main(String[] args) { + Class c1 = test04.class;//class + Class c2 = String[].class;//一维数组 + Class c3 = int[][].class;//二维数组 + Class c4 = Object.class; //类 + Class c5 = Comparable.class;//接口 + Class c6 = Override.class;//注解 + Class c7 = ElementType.class;//枚举 + Class c8 = void.class;//空类型 + Class c9 = Integer.class;//基本数据类型 + +// System.out.println("Class的getSuperclass:"+c1.getSuperclass());//Class的getSuperclass:class java.lang.Object +// System.out.println("Class的getName:"+c1.getName());//Class的getName:top.aigoo.reflection.test04 +// System.out.println("Class的getSimpleName:"+c1.getSimpleName());//Class的getSimpleName:test04 +// System.out.println("Class的getTypeName:"+c1.getTypeName());//Class的getTypeName:top.aigoo.reflection.test04 +// System.out.println("Class的getClasses:"+c1.getClasses()); +// System.out.println("Class的getFields:"+c1.getFields()); +// System.out.println("Class的getClassLoader():"+c1.getClassLoader()); +// System.out.println("Class的getClassLoader():"+c1.getMethods()[0]); + + + System.out.println(c1); + System.out.println(c2); + System.out.println(c3); + System.out.println(c4); + System.out.println(c5); + System.out.println(c6); + System.out.println(c7); + System.out.println(c8); + System.out.println("c9"+c9); + + //只要元素类型和维度一样,就是同一个Class + int[] a = new int[10]; + int[] b = new int[100]; + System.out.println(a.getClass().getSimpleName()); + System.out.println(b.getClass().getSimpleName()); + } +} diff --git a/studyJavaseCode/reflection/src/main/webapp/WEB-INF/web.xml b/studyJavaseCode/reflection/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyJavaseCode/reflection/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyJavaseCode/reflection/src/main/webapp/index.jsp b/studyJavaseCode/reflection/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyJavaseCode/reflection/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + +
                  +

                  Hello World!

                  +

                  AltStyle によって変換されたページ (->オリジナル) /

                  + diff --git a/studyMybatisCode.iml b/studyMybatisCode.iml new file mode 100644 index 0000000..b106b2a --- /dev/null +++ b/studyMybatisCode.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/studyMybatisCode/.gitignore b/studyMybatisCode/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-01/.gitignore b/studyMybatisCode/mybatis-01/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-01/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-01/pom.xml b/studyMybatisCode/mybatis-01/pom.xml new file mode 100644 index 0000000..03fd496 --- /dev/null +++ b/studyMybatisCode/mybatis-01/pom.xml @@ -0,0 +1,37 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-01 + war + mybatis-01 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + org.testng + testng + RELEASE + test + + + junit + junit + 4.13.1 + test + + + + mybatis-01 + + diff --git a/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/dao/UserMapper.java b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/dao/UserMapper.java new file mode 100644 index 0000000..5f147fc --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/dao/UserMapper.java @@ -0,0 +1,55 @@ +package com.clevermis.dao; + +import com.clevermis.pojo.User; +import java.util.List; +import java.util.Map; + +public interface UserMapper { + /** + * 模糊查询 + * @return + */ + List getUserLike(String value); + /** + * 获取全部查询用户 + * @return 查询所有用户 + */ + List getUserList(); + + /** + * 获取ID查询用户 + * @param id + * @return 查询id用户 + */ + User getUserById(int id); + + /** + * insert 一个用户 + * @param user + * @return 插入是否成功 + */ + int addUser(User user); + + + /** + * 万能的map + * @param map + * @return + */ + int addUser2(Map map); + + + /** + * 修改用户 + * @param user + * @return 修改是否成功 + */ + int updateUser(User user); + + /** + * 删除一个用户 + * @param id + * @return + */ + int deleteUser(int id); +} diff --git a/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/dao/UserMapper.xml b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/dao/UserMapper.xml new file mode 100644 index 0000000..d0fe4a4 --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/dao/UserMapper.xml @@ -0,0 +1,39 @@ + + + +
                    + + + + + + + + + + insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd}); + + + + + insert into mybatis.user (id, name, pwd) values (#{userid},#{userName},#{password}); + + + + update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id}; + + + + delete from mybatis.user where id = #{id}; + + \ No newline at end of file diff --git a/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/pojo/User.java b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/pojo/User.java new file mode 100644 index 0000000..f335aee --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/pojo/User.java @@ -0,0 +1,50 @@ +package com.clevermis.pojo; + +//实体类 +public class User { + private int id; + private String name; + private String pwd; + + public User() { + } + + public User(int id, String name, String pwd) { + this.id = id; + this.name = name; + this.pwd = pwd; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", pwd='" + pwd + '\'' + + '}'; + } +} diff --git a/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/utils/MybatisUtils.java b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/utils/MybatisUtils.java new file mode 100644 index 0000000..8e2eb13 --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/java/com/clevermis/utils/MybatisUtils.java @@ -0,0 +1,33 @@ +package com.clevermis.utils; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; + +//sqlSessionFactory --> sqlSession +public class MybatisUtils { + private static SqlSessionFactory sqlSessionFactory; + + static{ + try { + //使用Mybatis第一步:获取sqlSessionFactory对象 + String resource = "mybatis-config.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + //既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。 + // SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。 + public static SqlSession getSqlSession(){ + return sqlSessionFactory.openSession(); + } +} + diff --git a/studyMybatisCode/mybatis-01/src/main/resources/mybatis-config.xml b/studyMybatisCode/mybatis-01/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..2e3b39e --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/resources/mybatis-config.xml @@ -0,0 +1,24 @@ + + + + + + + +
                    + + + + + + + + + +
                      +
                        + + \ No newline at end of file diff --git a/studyMybatisCode/mybatis-01/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-01/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-01/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-01/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-01/src/test/java/UserDaoTest.java b/studyMybatisCode/mybatis-01/src/test/java/UserDaoTest.java new file mode 100644 index 0000000..d1f63c4 --- /dev/null +++ b/studyMybatisCode/mybatis-01/src/test/java/UserDaoTest.java @@ -0,0 +1,130 @@ +import com.clevermis.dao.UserMapper; +import com.clevermis.pojo.User; +import com.clevermis.utils.MybatisUtils; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.ibatis.session.SqlSession; +import org.junit.Test; + + +public class UserDaoTest { + + @Test + public void getUserLike() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + List userLike = mapper.getUserLike("%李%"); + + for (User user : userLike) { + System.out.println(user); + } + + sqlSession.close(); + } + @Test + public void test(){ + //第一步:获得SqlSession对象 + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + + //方式一:getMapper + UserMapper userMapper = sqlSession.getMapper(UserMapper.class); + List userList = userMapper.getUserList(); + + //方式二: + //List userList = sqlSession.selectList("nuc.ss.dao.UserMapper.getUserList"); + + for (User user : userList) { + System.out.println(user); + } + + //关闭SqlSession + sqlSession.close(); + } + @Test + public void getUserById() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + User user = mapper.getUserById(1); + System.out.println(user); + + sqlSession.close(); + } + + // 增删改查需要提交事务 + @Test + public void addUser() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + int i = mapper.addUser(new User(4, "哈哈", "123123")); + + if (i> 0) { + System.out.println("插入成功"); + } + + sqlSession.commit(); + + sqlSession.close(); + } + + @Test + public void addUser2() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + Map map = new HashMap(); + + map.put("userid", 5); + map.put("userName", "哈行昂"); + map.put("password", "465789"); + + int i = mapper.addUser2(map); + + if (i> 0) { + System.out.println("插入成功"); + } + + sqlSession.commit(); + + sqlSession.close(); + } + @Test + public void updateUser() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + int i = mapper.updateUser(new User(4, "呵呵", "123123")); + + if (i> 0) { + System.out.println("修改成功"); + } + + sqlSession.commit(); + sqlSession.close(); + } + + @Test + public void deleteUser() { + SqlSession sqlSession = MybatisUtils.getSqlSession(); + + UserMapper mapper = sqlSession.getMapper(UserMapper.class); + + int i = mapper.deleteUser(4); + + if (i> 0) { + System.out.println("删除成功"); + } + + sqlSession.commit(); + sqlSession.close(); + } +} diff --git a/studyMybatisCode/mybatis-02/.gitignore b/studyMybatisCode/mybatis-02/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-02/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-02/pom.xml b/studyMybatisCode/mybatis-02/pom.xml new file mode 100644 index 0000000..0d09309 --- /dev/null +++ b/studyMybatisCode/mybatis-02/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-02 + war + mybatis-02 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-02 + + diff --git a/studyMybatisCode/mybatis-02/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-02/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-02/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-02/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-02/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-02/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-03/.gitignore b/studyMybatisCode/mybatis-03/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-03/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-03/pom.xml b/studyMybatisCode/mybatis-03/pom.xml new file mode 100644 index 0000000..ff815c0 --- /dev/null +++ b/studyMybatisCode/mybatis-03/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-03 + war + mybatis-03 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-03 + + diff --git a/studyMybatisCode/mybatis-03/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-03/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-03/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-03/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-03/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-03/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-04/.gitignore b/studyMybatisCode/mybatis-04/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-04/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-04/pom.xml b/studyMybatisCode/mybatis-04/pom.xml new file mode 100644 index 0000000..ff2984e --- /dev/null +++ b/studyMybatisCode/mybatis-04/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-04 + war + mybatis-04 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-04 + + diff --git a/studyMybatisCode/mybatis-04/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-04/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-04/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-04/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-04/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-04/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-05/.gitignore b/studyMybatisCode/mybatis-05/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-05/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-05/pom.xml b/studyMybatisCode/mybatis-05/pom.xml new file mode 100644 index 0000000..9f841a5 --- /dev/null +++ b/studyMybatisCode/mybatis-05/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-05 + war + mybatis-05 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-05 + + diff --git a/studyMybatisCode/mybatis-05/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-05/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-05/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-05/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-05/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-05/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-06/.gitignore b/studyMybatisCode/mybatis-06/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-06/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-06/pom.xml b/studyMybatisCode/mybatis-06/pom.xml new file mode 100644 index 0000000..4e2b76e --- /dev/null +++ b/studyMybatisCode/mybatis-06/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-06 + war + mybatis-06 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-06 + + diff --git a/studyMybatisCode/mybatis-06/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-06/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-06/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-06/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-06/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-06/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-07/.gitignore b/studyMybatisCode/mybatis-07/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-07/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-07/pom.xml b/studyMybatisCode/mybatis-07/pom.xml new file mode 100644 index 0000000..25d6859 --- /dev/null +++ b/studyMybatisCode/mybatis-07/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-07 + war + mybatis-07 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-07 + + diff --git a/studyMybatisCode/mybatis-07/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-07/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-07/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-07/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-07/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-07/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-08/.gitignore b/studyMybatisCode/mybatis-08/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-08/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-08/pom.xml b/studyMybatisCode/mybatis-08/pom.xml new file mode 100644 index 0000000..cc8756f --- /dev/null +++ b/studyMybatisCode/mybatis-08/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-08 + war + mybatis-08 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-08 + + diff --git a/studyMybatisCode/mybatis-08/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-08/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-08/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-08/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-08/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-08/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-09/.gitignore b/studyMybatisCode/mybatis-09/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-09/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-09/pom.xml b/studyMybatisCode/mybatis-09/pom.xml new file mode 100644 index 0000000..fe294f1 --- /dev/null +++ b/studyMybatisCode/mybatis-09/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-09 + war + mybatis-09 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-09 + + diff --git a/studyMybatisCode/mybatis-09/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-09/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-09/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-09/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-09/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-09/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/mybatis-10/.gitignore b/studyMybatisCode/mybatis-10/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studyMybatisCode/mybatis-10/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studyMybatisCode/mybatis-10/pom.xml b/studyMybatisCode/mybatis-10/pom.xml new file mode 100644 index 0000000..a5d17e9 --- /dev/null +++ b/studyMybatisCode/mybatis-10/pom.xml @@ -0,0 +1,25 @@ + + + studyMybatisCode + com.clevermis + 1.0-SNAPSHOT + + 4.0.0 + mybatis-10 + war + mybatis-10 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mybatis-10 + + diff --git a/studyMybatisCode/mybatis-10/src/main/webapp/WEB-INF/web.xml b/studyMybatisCode/mybatis-10/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studyMybatisCode/mybatis-10/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studyMybatisCode/mybatis-10/src/main/webapp/index.jsp b/studyMybatisCode/mybatis-10/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studyMybatisCode/mybatis-10/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studyMybatisCode/pom.xml b/studyMybatisCode/pom.xml new file mode 100644 index 0000000..87d72af --- /dev/null +++ b/studyMybatisCode/pom.xml @@ -0,0 +1,71 @@ + + 4.0.0 + com.clevermis + studyMybatisCode + pom + 1.0-SNAPSHOT + + mybatis-01 + mybatis-02 + mybatis-03 + mybatis-04 + mybatis-05 + mybatis-06 + mybatis-07 + mybatis-08 + mybatis-09 + mybatis-10 + + studyMybatisCode Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + mysql + mysql-connector-java + 8.0.31 + + + + + org.mybatis + mybatis + 3.5.2 + + + + junit + junit + 3.8.1 + test + + + + + + + src/main/resources + + **/*.properties + **/*.xml + + true + + + src/main/java + + **/*.properties + **/*.xml + + true + + + + diff --git a/studySpringCode/.gitignore b/studySpringCode/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/pom.xml b/studySpringCode/pom.xml new file mode 100644 index 0000000..4d80e38 --- /dev/null +++ b/studySpringCode/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + org.example + studySpringCode + pom + 1.0-SNAPSHOT + + spring-01-ioc1 + spring-02-hellospring + spring-03-ioc2 + spring-04-DI + spring-05-Autowired + spring-06-anno + spring-07-appconfig + spring-08-proxy + + studySpringCode Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + org.springframework + spring-webmvc + 5.3.2 + + + + org.springframework + spring-jdbc + 5.3.2 + + + org.projectlombok + lombok + RELEASE + compile + + + junit + junit + 4.13.1 + test + + + + + studySpringCode + + diff --git a/studySpringCode/spring-01-ioc1/.gitignore b/studySpringCode/spring-01-ioc1/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-01-ioc1/pom.xml b/studySpringCode/spring-01-ioc1/pom.xml new file mode 100644 index 0000000..f3bb6b5 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/pom.xml @@ -0,0 +1,36 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-01-ioc1 + war + spring-01-ioc1 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + spring-01-ioc1 + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDao.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDao.java new file mode 100644 index 0000000..eeeae75 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDao.java @@ -0,0 +1,14 @@ +package com.clevermis.dao; + +import java.util.PriorityQueue; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 13:51 + **/ +public interface UserDao { + + void getUser(); +} diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoImpl.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoImpl.java new file mode 100644 index 0000000..e46554b --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoImpl.java @@ -0,0 +1,17 @@ +package com.clevermis.dao; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 13:51 + * @since JDK 1.8 + **/ +public class UserDaoImpl implements UserDao{ + + + @Override + public void getUser() { + + } +} diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoMysqlImpl.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoMysqlImpl.java new file mode 100644 index 0000000..a7cbaf6 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoMysqlImpl.java @@ -0,0 +1,9 @@ +package com.clevermis.dao; + +public class UserDaoMysqlImpl implements UserDao { + + @Override + public void getUser() { + System.out.println("MySql获取用户数据"); + } +} diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoOracleImpl.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoOracleImpl.java new file mode 100644 index 0000000..39fec7c --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoOracleImpl.java @@ -0,0 +1,9 @@ +package com.clevermis.dao; + +public class UserDaoOracleImpl implements UserDao { + + @Override + public void getUser() { + System.out.println("Oracle获取用户数据"); + } +} diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoSqlServerImpl.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoSqlServerImpl.java new file mode 100644 index 0000000..457dadd --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/dao/UserDaoSqlServerImpl.java @@ -0,0 +1,9 @@ +package com.clevermis.dao; + +public class UserDaoSqlServerImpl implements UserDao { + + @Override + public void getUser() { + System.out.println("Server获取用户数据"); + } +} diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/service/UserService.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/service/UserService.java new file mode 100644 index 0000000..b890752 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/service/UserService.java @@ -0,0 +1,12 @@ +package com.clevermis.service; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 13:51 + **/ +public interface UserService { +void getUser(); + +} diff --git a/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/service/UserServiceImpl.java b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/service/UserServiceImpl.java new file mode 100644 index 0000000..dfcd5af --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/java/com/clevermis/service/UserServiceImpl.java @@ -0,0 +1,30 @@ +package com.clevermis.service; + +import com.clevermis.dao.UserDao; +import java.sql.SQLOutput; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 13:52 + * @since JDK 1.8 + **/ +public class UserServiceImpl implements UserService { + + private UserDao userDao; + + + //利用set进行动态实现值的注入 + public void setUserDao(UserDao userDao) { + System.out.println("1:"+userDao); + this.userDao =userDao; + System.out.println("2:"+userDao); + } + + @Override + public void getUser() { + System.out.println("3:"+userDao); + userDao.getUser(); + } +} diff --git a/studySpringCode/spring-01-ioc1/src/main/resources/beans.xml b/studySpringCode/spring-01-ioc1/src/main/resources/beans.xml new file mode 100644 index 0000000..ed70f26 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/resources/beans.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-01-ioc1/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-01-ioc1/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-01-ioc1/src/main/webapp/index.jsp b/studySpringCode/spring-01-ioc1/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-01-ioc1/src/test/java/Mytest.java b/studySpringCode/spring-01-ioc1/src/test/java/Mytest.java new file mode 100644 index 0000000..ce7d045 --- /dev/null +++ b/studySpringCode/spring-01-ioc1/src/test/java/Mytest.java @@ -0,0 +1,23 @@ +import com.clevermis.service.UserServiceImpl; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 15:54 + * @since JDK 1.8 + **/ +public class Mytest { + + public static void main(String[] args) { + //获取ApplicationContext;拿到Spring的容器 + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + System.out.println("context::"+context); + //容器里有万物,需要什么,就直接get什么! + UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); + System.out.println("userServiceImpl:::"+userServiceImpl); + userServiceImpl.getUser(); + } +} diff --git a/studySpringCode/spring-02-hellospring/.gitignore b/studySpringCode/spring-02-hellospring/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-02-hellospring/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-02-hellospring/pom.xml b/studySpringCode/spring-02-hellospring/pom.xml new file mode 100644 index 0000000..c33381d --- /dev/null +++ b/studySpringCode/spring-02-hellospring/pom.xml @@ -0,0 +1,26 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-02-hellospring + war + spring-02-hellospring Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + spring-02-hellospring + + diff --git a/studySpringCode/spring-02-hellospring/src/main/java/com/clevermis/pojo/Hello.java b/studySpringCode/spring-02-hellospring/src/main/java/com/clevermis/pojo/Hello.java new file mode 100644 index 0000000..a46d543 --- /dev/null +++ b/studySpringCode/spring-02-hellospring/src/main/java/com/clevermis/pojo/Hello.java @@ -0,0 +1,30 @@ +package com.clevermis.pojo; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 15:33 + * @since JDK 1.8 + **/ +public class Hello { + private String str; + + public Hello() { + } + + public String getStr() { + return this.str; + } + + public void setStr(final String str) { + this.str = str; + } + + @Override + public String toString() { + return "Hello{" + + "str='" + str + '\'' + + '}'; + } +} diff --git a/studySpringCode/spring-02-hellospring/src/main/resources/beans.xml b/studySpringCode/spring-02-hellospring/src/main/resources/beans.xml new file mode 100644 index 0000000..0aed283 --- /dev/null +++ b/studySpringCode/spring-02-hellospring/src/main/resources/beans.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-02-hellospring/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-02-hellospring/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-02-hellospring/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-02-hellospring/src/main/webapp/index.jsp b/studySpringCode/spring-02-hellospring/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-02-hellospring/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-02-hellospring/src/test/java/Mytest.java b/studySpringCode/spring-02-hellospring/src/test/java/Mytest.java new file mode 100644 index 0000000..972f093 --- /dev/null +++ b/studySpringCode/spring-02-hellospring/src/test/java/Mytest.java @@ -0,0 +1,22 @@ +import com.clevermis.pojo.Hello; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 15:37 + * @since JDK 1.8 + **/ +public class Mytest { + + public static void main(String[] args) { + //获取Spring的上下文对象 + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + //我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出就可以了! + Hello hello = (Hello) context.getBean("hello"); + System.out.println(hello.toString()); + } + +} diff --git a/studySpringCode/spring-03-ioc2/.gitignore b/studySpringCode/spring-03-ioc2/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-03-ioc2/pom.xml b/studySpringCode/spring-03-ioc2/pom.xml new file mode 100644 index 0000000..f7937fb --- /dev/null +++ b/studySpringCode/spring-03-ioc2/pom.xml @@ -0,0 +1,26 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-03-ioc2 + war + spring-03-ioc2 Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + spring-03-ioc2 + + diff --git a/studySpringCode/spring-03-ioc2/src/main/java/com/clevermis/pojo/User.java b/studySpringCode/spring-03-ioc2/src/main/java/com/clevermis/pojo/User.java new file mode 100644 index 0000000..249d1c4 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/java/com/clevermis/pojo/User.java @@ -0,0 +1,27 @@ +package com.clevermis.pojo; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 16:25 + * @since JDK 1.8 + **/ +public class User { + private String name; + + public User() { + } + + public User(final String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } +} diff --git a/studySpringCode/spring-03-ioc2/src/main/java/com/clevermis/pojo/UserT.java b/studySpringCode/spring-03-ioc2/src/main/java/com/clevermis/pojo/UserT.java new file mode 100644 index 0000000..832bba2 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/java/com/clevermis/pojo/UserT.java @@ -0,0 +1,29 @@ +package com.clevermis.pojo; + +import java.sql.SQLOutput; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 16:47 + * @since JDK 1.8 + **/ +public class UserT { + private String name; + + public String getName() { + return this.name; + } + + public UserT(final String name) { + this.name = name; + } + + public void setName(final String name) { + this.name = name; + } + + public UserT() { + } +} diff --git a/studySpringCode/spring-03-ioc2/src/main/resources/applicationContext.xml b/studySpringCode/spring-03-ioc2/src/main/resources/applicationContext.xml new file mode 100644 index 0000000..cac33d3 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/resources/applicationContext.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-03-ioc2/src/main/resources/beans.xml b/studySpringCode/spring-03-ioc2/src/main/resources/beans.xml new file mode 100644 index 0000000..fa9dca6 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/resources/beans.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-03-ioc2/src/main/resources/beans2.xml b/studySpringCode/spring-03-ioc2/src/main/resources/beans2.xml new file mode 100644 index 0000000..84ec510 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/resources/beans2.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-03-ioc2/src/main/resources/beans3.xml b/studySpringCode/spring-03-ioc2/src/main/resources/beans3.xml new file mode 100644 index 0000000..84ec510 --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/resources/beans3.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-03-ioc2/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-03-ioc2/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-03-ioc2/src/main/webapp/index.jsp b/studySpringCode/spring-03-ioc2/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-03-ioc2/src/test/java/Mytest.java b/studySpringCode/spring-03-ioc2/src/test/java/Mytest.java new file mode 100644 index 0000000..17f67ff --- /dev/null +++ b/studySpringCode/spring-03-ioc2/src/test/java/Mytest.java @@ -0,0 +1,21 @@ +import com.clevermis.pojo.User; +import com.clevermis.pojo.UserT; +import javax.naming.Context; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 16:26 + * @since JDK 1.8 + **/ +public class Mytest { + + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + UserT user = (UserT) context.getBean("userT"); + System.out.println(user.getName()); + } +} diff --git a/studySpringCode/spring-04-DI/.gitignore b/studySpringCode/spring-04-DI/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-04-DI/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-04-DI/pom.xml b/studySpringCode/spring-04-DI/pom.xml new file mode 100644 index 0000000..5b62723 --- /dev/null +++ b/studySpringCode/spring-04-DI/pom.xml @@ -0,0 +1,26 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-04-DI + war + spring-04-DI Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + spring-04-DI + + diff --git a/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/Address.java b/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/Address.java new file mode 100644 index 0000000..05ac950 --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/Address.java @@ -0,0 +1,27 @@ +package com.clevermis.pojo; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:12 + * @since JDK 1.8 + **/ +public class Address { + private String address; + + public String getAddress() { + return this.address; + } + + public void setAddress(final String address) { + this.address = address; + } + + @Override + public String toString() { + return "Address{" + + "address='" + address + '\'' + + '}'; + } +} diff --git a/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/Student.java b/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/Student.java new file mode 100644 index 0000000..6d06325 --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/Student.java @@ -0,0 +1,104 @@ +package com.clevermis.pojo; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:12 + * @since JDK 1.8 + **/ +public class Student { + + private String name; + private Address address; + private String[] books; + private List hobbies; + private Map card; + private Set games; + private String wife; + private Properties info; + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + public Address getAddress() { + return this.address; + } + + public void setAddress(final Address address) { + this.address = address; + } + + public String[] getBooks() { + return this.books; + } + + public void setBooks(final String[] books) { + this.books = books; + } + + public List getHobbies() { + return this.hobbies; + } + + public void setHobbies(final List hobbies) { + this.hobbies = hobbies; + } + + public Map getCard() { + return this.card; + } + + public void setCard(final Map card) { + this.card = card; + } + + public Set getGames() { + return this.games; + } + + public void setGames(final Set games) { + this.games = games; + } + + public String getWife() { + return this.wife; + } + + public void setWife(final String wife) { + this.wife = wife; + } + + public Properties getInfo() { + return this.info; + } + + public void setInfo(final Properties info) { + this.info = info; + } + + @Override + public String toString() { + return "Student{" + + "name='" + name + '\'' + + ", address=" + address + + ", books=" + Arrays.toString(books) + + ", hobbies=" + hobbies + + ", card=" + card + + ", games=" + games + + ", wife='" + wife + '\'' + + ", info=" + info + + '}'; + } +} diff --git a/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/User.java b/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/User.java new file mode 100644 index 0000000..17e8e77 --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/java/com/clevermis/pojo/User.java @@ -0,0 +1,46 @@ +package com.clevermis.pojo; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:15 + * @since JDK 1.8 + **/ +public class User { + private String name; + private int age; + + + public User() { + } + + public User(final String name, final int age) { + this.name = name; + this.age = age; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + public int getAge() { + return this.age; + } + + public void setAge(final int age) { + this.age = age; + } +} diff --git a/studySpringCode/spring-04-DI/src/main/resources/beans.xml b/studySpringCode/spring-04-DI/src/main/resources/beans.xml new file mode 100644 index 0000000..13c5aea --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/resources/beans.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + 红楼梦 + 西游记 + 水浒传 + 三国演义 + + + + + + 吃饭 + 睡觉 + 打豆豆 + + + + +
                          + + +
                        +
                        + + + + + LOL + COC + + + + + + + + + + 123456 + + 小明 + root + root + + +
                        + + +
                        \ No newline at end of file diff --git a/studySpringCode/spring-04-DI/src/main/resources/userbeans.xml b/studySpringCode/spring-04-DI/src/main/resources/userbeans.xml new file mode 100644 index 0000000..3bd08f8 --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/resources/userbeans.xml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-04-DI/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-04-DI/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-04-DI/src/main/webapp/index.jsp b/studySpringCode/spring-04-DI/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-04-DI/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-04-DI/src/test/java/Mytest.java b/studySpringCode/spring-04-DI/src/test/java/Mytest.java new file mode 100644 index 0000000..88c8936 --- /dev/null +++ b/studySpringCode/spring-04-DI/src/test/java/Mytest.java @@ -0,0 +1,19 @@ +import com.clevermis.pojo.Student; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 17:18 + * @since JDK 1.8 + **/ +public class Mytest { + + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + Student student = (Student) context.getBean("student"); + System.out.println(student.toString()); + } +} diff --git a/studySpringCode/spring-05-Autowired/.gitignore b/studySpringCode/spring-05-Autowired/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-05-Autowired/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-05-Autowired/pom.xml b/studySpringCode/spring-05-Autowired/pom.xml new file mode 100644 index 0000000..f58b273 --- /dev/null +++ b/studySpringCode/spring-05-Autowired/pom.xml @@ -0,0 +1,38 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-05-Autowired + war + spring-05-Autowired Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + org.testng + testng + RELEASE + test + + + org.projectlombok + lombok + RELEASE + compile + + + + spring-05-Autowired + + diff --git a/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/Cat.java b/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/Cat.java new file mode 100644 index 0000000..b460674 --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/Cat.java @@ -0,0 +1,17 @@ +package com.clevermis.pojo; + +/** + * @methodname 的功能描述 TODO + * @Param: * @param null + * @return: + * @throw: + * @Author: Clevermis + * @version: V1.0.0 + * @Date: 2023年1月10日 20:28 + */ +public class Cat { + + public void shout() { + System.out.println("miao~"); + } +} diff --git a/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/Dog.java b/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/Dog.java new file mode 100644 index 0000000..323f7ed --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/Dog.java @@ -0,0 +1,18 @@ +package com.clevermis.pojo; + +/** + * @methodname 的功能描述 TODO + * @Param: * @param null + * @return: + * @throw: + * @Author: Clevermis + * @version: V1.0.0 + * @Date: 2023年1月10日 20:28 + */ +public class Dog { + + public void shout() { + System.out.println("wang~"); + } + +} diff --git a/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/People.java b/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/People.java new file mode 100644 index 0000000..1fb2b17 --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/main/java/com/clevermis/pojo/People.java @@ -0,0 +1,18 @@ +package com.clevermis.pojo; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; + + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class People { + @Autowired + private Cat cat; + @Autowired + private Dog dog; + private String name; + +} diff --git a/studySpringCode/spring-05-Autowired/src/main/resources/beans.xml b/studySpringCode/spring-05-Autowired/src/main/resources/beans.xml new file mode 100644 index 0000000..f760af0 --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/main/resources/beans.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/studySpringCode/spring-05-Autowired/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-05-Autowired/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-05-Autowired/src/main/webapp/index.jsp b/studySpringCode/spring-05-Autowired/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-05-Autowired/src/test/java/Mytest.java b/studySpringCode/spring-05-Autowired/src/test/java/Mytest.java new file mode 100644 index 0000000..41df6c0 --- /dev/null +++ b/studySpringCode/spring-05-Autowired/src/test/java/Mytest.java @@ -0,0 +1,25 @@ +import com.clevermis.pojo.People; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 20:33 + * @since JDK 1.8 + **/ +public class Mytest { + + + public static void main(String[] args) { + ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml"); + + People people = context.getBean("people", People.class); + people.getDog().shout(); + people.getCat().shout(); + } + + +} diff --git a/studySpringCode/spring-06-anno/.gitignore b/studySpringCode/spring-06-anno/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-06-anno/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-06-anno/pom.xml b/studySpringCode/spring-06-anno/pom.xml new file mode 100644 index 0000000..6b9fb55 --- /dev/null +++ b/studySpringCode/spring-06-anno/pom.xml @@ -0,0 +1,32 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-06-anno + war + spring-06-anno Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + junit + junit + 4.13.1 + test + + + + spring-06-anno + + diff --git a/studySpringCode/spring-06-anno/src/main/java/com/clevermis/controller/UserController.java b/studySpringCode/spring-06-anno/src/main/java/com/clevermis/controller/UserController.java new file mode 100644 index 0000000..edb1b15 --- /dev/null +++ b/studySpringCode/spring-06-anno/src/main/java/com/clevermis/controller/UserController.java @@ -0,0 +1,15 @@ +package com.clevermis.controller; + +import org.springframework.stereotype.Controller; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 21:34 + * @since JDK 1.8 + **/ +@Controller +public class UserController { + +} diff --git a/studySpringCode/spring-06-anno/src/main/java/com/clevermis/dao/User.java b/studySpringCode/spring-06-anno/src/main/java/com/clevermis/dao/User.java new file mode 100644 index 0000000..1d2ebb8 --- /dev/null +++ b/studySpringCode/spring-06-anno/src/main/java/com/clevermis/dao/User.java @@ -0,0 +1,21 @@ +package com.clevermis.dao; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 21:12 + * @since JDK 1.8 + **/ + +@Component +@Scope("singleton") +public class User { + @Value("clevermis") + public String name ; + +} diff --git a/studySpringCode/spring-06-anno/src/main/java/com/clevermis/service/UserService.java b/studySpringCode/spring-06-anno/src/main/java/com/clevermis/service/UserService.java new file mode 100644 index 0000000..54a77a1 --- /dev/null +++ b/studySpringCode/spring-06-anno/src/main/java/com/clevermis/service/UserService.java @@ -0,0 +1,15 @@ +package com.clevermis.service; + +import org.springframework.stereotype.Service; + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 21:33 + * @since JDK 1.8 + **/ +@Service +public class UserService { + +} diff --git a/studySpringCode/spring-06-anno/src/main/resources/beans.xml b/studySpringCode/spring-06-anno/src/main/resources/beans.xml new file mode 100644 index 0000000..5bf9eb1 --- /dev/null +++ b/studySpringCode/spring-06-anno/src/main/resources/beans.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/studySpringCode/spring-06-anno/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-06-anno/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-06-anno/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-06-anno/src/main/webapp/index.jsp b/studySpringCode/spring-06-anno/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-06-anno/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-06-anno/src/test/java/Mytest.java b/studySpringCode/spring-06-anno/src/test/java/Mytest.java new file mode 100644 index 0000000..7010bc3 --- /dev/null +++ b/studySpringCode/spring-06-anno/src/test/java/Mytest.java @@ -0,0 +1,21 @@ +import com.clevermis.dao.User; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.junit.Test; +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 21:14 + * @since JDK 1.8 + **/ +public class Mytest { + + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext( + "beans.xml"); + User user = context.getBean("user", User.class); + System.out.println(user.name); + } + +} diff --git a/studySpringCode/spring-07-appconfig/.gitignore b/studySpringCode/spring-07-appconfig/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-07-appconfig/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-07-appconfig/pom.xml b/studySpringCode/spring-07-appconfig/pom.xml new file mode 100644 index 0000000..7b3ac43 --- /dev/null +++ b/studySpringCode/spring-07-appconfig/pom.xml @@ -0,0 +1,38 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-07-appconfig + war + spring-07-appconfig Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + org.testng + testng + RELEASE + test + + + junit + junit + 4.13.1 + test + + + + spring-07-appconfig + + diff --git a/studySpringCode/spring-07-appconfig/src/main/java/com/clevermis/config/ClevermisConfig.java b/studySpringCode/spring-07-appconfig/src/main/java/com/clevermis/config/ClevermisConfig.java new file mode 100644 index 0000000..4f046d5 --- /dev/null +++ b/studySpringCode/spring-07-appconfig/src/main/java/com/clevermis/config/ClevermisConfig.java @@ -0,0 +1,25 @@ +package com.clevermis.config; + +import com.clevermis.pojo.User; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + + +@Configuration //这个也会被spring容器注册到容器中,因为他本来就是一个Component +// 代表这是一个配置类 就和我们之前看的beans.xml是一样的 +@ComponentScan("com.clevermis.pojo") +public class ClevermisConfig { + + + + //注册一个bean , 就相当于我们之前写的一个bean标签 + //这个方法的名字 就相当于bean标签的id属性; + // 这个方法的返回值 就相当于bean标签中的class属性 + @Bean + public User user(){ + return new User();//就是返回要注入到bean的对象 + } + +} diff --git a/studySpringCode/spring-07-appconfig/src/main/java/com/clevermis/pojo/User.java b/studySpringCode/spring-07-appconfig/src/main/java/com/clevermis/pojo/User.java new file mode 100644 index 0000000..16c564b --- /dev/null +++ b/studySpringCode/spring-07-appconfig/src/main/java/com/clevermis/pojo/User.java @@ -0,0 +1,33 @@ +package com.clevermis.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + + +@Data +@NoArgsConstructor +@AllArgsConstructor +//这里这个注解的意思是。就是说明这个类被spring接管了,注册到了容器中。 +@Component +public class User { + + private String name; + + public String getName() { + return this.name; + } + @Value("clevermis")// 属性注入值 + public void setName(final String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/studySpringCode/spring-07-appconfig/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-07-appconfig/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-07-appconfig/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-07-appconfig/src/main/webapp/index.jsp b/studySpringCode/spring-07-appconfig/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-07-appconfig/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + + diff --git a/studySpringCode/spring-07-appconfig/src/test/java/Mytest.java b/studySpringCode/spring-07-appconfig/src/test/java/Mytest.java new file mode 100644 index 0000000..a45f4c7 --- /dev/null +++ b/studySpringCode/spring-07-appconfig/src/test/java/Mytest.java @@ -0,0 +1,23 @@ +import com.clevermis.config.ClevermisConfig; +import com.clevermis.pojo.User; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.junit.Test; + + +/** + * @program: java-study + * @description: + * @author: Clevermis + * @create: 2023年01月10日 21:46 + * @since JDK 1.8 + **/ +public class Mytest { + @Test + public void test1(){ + //如果完全使用配置类方式去做,我们就只能通过AnnotationConfig 上下文来获取容器 , 通过配置类的class对象加载 + ApplicationContext context = new AnnotationConfigApplicationContext(ClevermisConfig.class); + User user = (User) context.getBean("user"); + System.out.println(user.getName()); + } +} diff --git a/studySpringCode/spring-08-proxy/.gitignore b/studySpringCode/spring-08-proxy/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/studySpringCode/spring-08-proxy/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/studySpringCode/spring-08-proxy/pom.xml b/studySpringCode/spring-08-proxy/pom.xml new file mode 100644 index 0000000..5d5197a --- /dev/null +++ b/studySpringCode/spring-08-proxy/pom.xml @@ -0,0 +1,36 @@ + + + studySpringCode + org.example + 1.0-SNAPSHOT + + 4.0.0 + com.clevermis + spring-08-proxy + war + spring-08-proxy Maven Webapp + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + + spring-08-proxy + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Client.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Client.java new file mode 100644 index 0000000..dae8074 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Client.java @@ -0,0 +1,15 @@ +package com.clevermis.demo01; + + +public class Client { + + public static void main(String[] args) { + Host host = new Host(); + Proxy proxy = new Proxy(host); + proxy.seeHouse(); + proxy.rent(); + proxy.hetong(); + proxy.fare(); + } + +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Host.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Host.java new file mode 100644 index 0000000..f8e95cd --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Host.java @@ -0,0 +1,10 @@ +package com.clevermis.demo01; + +public class Host implements Rent { + + + @Override + public void rent() { + System.out.println("房东要出租房子!"); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Proxy.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Proxy.java new file mode 100644 index 0000000..79ee893 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Proxy.java @@ -0,0 +1,28 @@ +package com.clevermis.demo01; + +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor +public class Proxy implements Rent { + + private Host host; + + @Override + public void rent() { + host.rent(); + } + + public void seeHouse(){ + System.out.println("中介带你看房!"); + } + + public void fare(){ + System.out.println("收中介费!"); + } + + public void hetong(){ + System.out.println("签租赁合同!"); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Rent.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Rent.java new file mode 100644 index 0000000..8d85996 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo01/Rent.java @@ -0,0 +1,6 @@ +package com.clevermis.demo01; + +public interface Rent { + + void rent(); +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/Client.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/Client.java new file mode 100644 index 0000000..d207e8d --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/Client.java @@ -0,0 +1,11 @@ +package com.clevermis.demo02; + +public class Client { + + public static void main(String[] args) { + UserService userService = new UserServiceImpl(); + UserServicePorxy userServicePorxy = new UserServicePorxy(); + userServicePorxy.setUserService(userService); + userServicePorxy.add(); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserService.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserService.java new file mode 100644 index 0000000..99ec785 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserService.java @@ -0,0 +1,9 @@ +package com.clevermis.demo02; + +public interface UserService { + + void add(); + void delete(); + void update(); + void query(); +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserServiceImpl.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserServiceImpl.java new file mode 100644 index 0000000..568fef5 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserServiceImpl.java @@ -0,0 +1,24 @@ +package com.clevermis.demo02; + + +public class UserServiceImpl implements UserService { + @Override + public void add() { + System.out.println("增加了一个用户"); + } + + @Override + public void delete() { + System.out.println("删除了一个用户"); + } + + @Override + public void update() { + System.out.println("修改了一个用户"); + } + + @Override + public void query() { + System.out.println("查询了一个用户"); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserServicePorxy.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserServicePorxy.java new file mode 100644 index 0000000..780db11 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo02/UserServicePorxy.java @@ -0,0 +1,40 @@ +package com.clevermis.demo02; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class UserServicePorxy implements UserService { + + private UserService userService; + @Override + public void add() { + log("add"); + userService.add(); + } + + @Override + public void delete() { + log("delete"); + userService.delete(); + } + + @Override + public void update() { + log("update"); + userService.update(); + } + + @Override + public void query() { + log("query"); + userService.query(); + } + + public void log(String msg){ + System.out.println("使用了" + msg + "方法"); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Client.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Client.java new file mode 100644 index 0000000..4de8e72 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Client.java @@ -0,0 +1,21 @@ +package com.clevermis.demo03; + +/** + * @author :ltb + * @date :2020年7月15日 + */ +public class Client { + + public static void main(String[] args) { + //真实角色 + Host host = new Host(); + //代理角色:现在没有 + ProxyInvocationHandler pih = new ProxyInvocationHandler(); + //通过调用程序处理角色来处理我们要调用的接口对象 + pih.setRent(host); + Rent proxy = (Rent) pih.getProxy(); + proxy.rent(); + } + + +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Host.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Host.java new file mode 100644 index 0000000..999d22d --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Host.java @@ -0,0 +1,14 @@ +package com.clevermis.demo03; + +/** + * @author :ltb + * @date :2020年7月14日 + */ +public class Host implements Rent { + + + @Override + public void rent() { + System.out.println("房东要出租房子!"); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/ProxyInvocationHandler.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/ProxyInvocationHandler.java new file mode 100644 index 0000000..900db6f --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/ProxyInvocationHandler.java @@ -0,0 +1,45 @@ +package com.clevermis.demo03; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * @author :ltb + * @date :2020年7月15日 + */ + +//稍后用这个类自动生成代理类! +public class ProxyInvocationHandler implements InvocationHandler { + + private Rent rent; + + public void setRent(Rent rent){ + this.rent = rent; + } + + + //生成得到代理对象 + public Object getProxy(){ + return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(), this); + } + + //处理代理实例并返回结果 + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + + seeHouse(); + //动态代理的本质就是使用反射机制实现 + Object result = method.invoke(rent, args); + fare(); + return result; + } + + public void seeHouse(){ + System.out.println("中介带看房子"); + } + + public void fare(){ + System.out.println("收中介费"); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Rent.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Rent.java new file mode 100644 index 0000000..8068a1a --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo03/Rent.java @@ -0,0 +1,10 @@ +package com.clevermis.demo03; + +/** + * @author :ltb + * @date :2020年7月14日 + */ +public interface Rent { + + void rent(); +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo04/Client.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo04/Client.java new file mode 100644 index 0000000..8fb85aa --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo04/Client.java @@ -0,0 +1,27 @@ +package com.clevermis.demo04; + + +import com.clevermis.demo02.UserService; +import com.clevermis.demo02.UserServiceImpl; + +/** + * @author :ltb + * @date :2020年7月15日 + */ +public class Client { + public static void main(String[] args) { + //真实角色 + UserService userService = new UserServiceImpl(); + //代理角色,不存在 + ProxyInvocationHandler pih = new ProxyInvocationHandler(); + //设置要代理的对象 + pih.setTarget(userService); + //动态生成代理类 + UserService proxy = (UserService) pih.getProxy(); + + proxy.add(); + proxy.delete(); + proxy.query(); + proxy.update(); + } +} diff --git a/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo04/ProxyInvocationHandler.java b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo04/ProxyInvocationHandler.java new file mode 100644 index 0000000..750abe8 --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/java/com/clevermis/demo04/ProxyInvocationHandler.java @@ -0,0 +1,42 @@ +package com.clevermis.demo04; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * @author :ltb + * @date :2020年7月15日 + */ + +//稍后用这个类自动生成代理类! +public class ProxyInvocationHandler implements InvocationHandler { + + //被代理的接口 + private Object target; + + + public void setTarget(Object target) { + this.target = target; + } + + //生成得到代理对象 + public Object getProxy() { + return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this); + } + + //处理代理实例并返回结果 + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + log(method.getName()); + + //动态代理的本质就是使用反射机制实现 + Object result = method.invoke(target, args); + return result; + } + + public void log(String msg) { + System.out.println("执行了" + msg + "方法"); + } + +} diff --git a/studySpringCode/spring-08-proxy/src/main/webapp/WEB-INF/web.xml b/studySpringCode/spring-08-proxy/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/studySpringCode/spring-08-proxy/src/main/webapp/index.jsp b/studySpringCode/spring-08-proxy/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/studySpringCode/spring-08-proxy/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

                        Hello World!

                        + +