# java_spring **Repository Path**: enzoism/java_spring ## Basic Information - **Project Name**: java_spring - **Description**: Java学习Spring使用 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-12-22 - **Last Updated**: 2020-12-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 1、写作背景 > 总想走捷径的人,往往因找不到捷径而固步自封,一步不前! -- --- # 2、参考网址 - [IT楠老师](https://www.itnanls.cn/) - [IT楠老师-BiliBili](https://www.bilibili.com/video/BV1yK4y1t79b?p=1&t=18) - [IT楠老师-gitee资料](https://gitee.com/zhangnan716/document-blibli) -- -- - [控制反转和依赖注入的理解](https://blog.csdn.net/sinat_21843047/article/details/80297951) - [IT楠老师-gitee资料-mybatis教程](https://gitee.com/zhangnan716/document-blibli/raw/master/2020%E8%B5%84%E6%96%99/ssm%E6%96%87%E6%A1%A3/IT%E6%A5%A0%E8%80%81%E5%B8%88mybatis%E6%95%99%E7%A8%8B.pdf) - [IT楠老师-gitee资料-spring教程](https://gitee.com/zhangnan716/document-blibli/blob/master/2020%E8%B5%84%E6%96%99/ssm%E6%96%87%E6%A1%A3/IT%E6%A5%A0%E8%80%81%E5%B8%88spring%E6%95%99%E7%A8%8B.pdf) - [IT楠老师-gitee资料-springmvc教程](https://gitee.com/zhangnan716/document-blibli/blob/master/2020%E8%B5%84%E6%96%99/ssm%E6%96%87%E6%A1%A3/IT%E6%A5%A0%E8%80%81%E5%B8%88springmvc%E6%95%99%E7%A8%8B.pdf) -- -- # 3、学习目的 - SSH和SSM - 什么是IOC容器?什么是序列化?什么是依赖注入? - 如何进行Spring对象注入和获取 - ApplicatonContext能力 - XML_Schema了解 - 什么是ClassPath -- -- # 4、核心操作 ## 1)SSH和SSM SSH: Struts+Spring+Hibernate【是16年之前较为流行的一种Web应用程序开源框架】 SSM:Spring+SpringMVC+MyBatis > ![](https://upload-images.jianshu.io/upload_images/1096351-6bd6b90194f86f80.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -- -- ## 2)什么是IOC容器?什么是序列化?什么是依赖注入? | id | name | function| description| |----|----------|-----|--------| | 1 | 对象序列化| implements Serializable | 方便对象在网络传输中进行格式的转化 | | 2 | 控制反转IOC | Inversion of Control,即“控制反转” | 通过IOC进行属性读取 [以前是new对象,现在是读取配置] | | 3 | 依赖注入DI | Dependency Injection,即“依赖注入” | 通过IOC进行属性注入,以前是new对象,显示是配置文件注入属性 | - 【序列化】VS【依赖注入】 > ![](https://upload-images.jianshu.io/upload_images/1096351-99539f477d0917f8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 什么是【IOC】 > ![](https://upload-images.jianshu.io/upload_images/1096351-9a38531498c3a60b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) > 说明一点:Spring目前只需要导入一个包既可以完成所有的包导入 >![](https://upload-images.jianshu.io/upload_images/1096351-7539780cc7775d62.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ``` junit junit 4.7 test org.springframework spring-webmvc 5.2.6.RELEASE org.projectlombok lombok 1.18.12 org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 UTF-8 ``` -- -- ## 3)如何进行Spring对象注入和获取? 1、添加pom依赖 ``` junit junit 4.13 test org.springframework spring-webmvc 5.2.6.RELEASE org.projectlombok lombok 1.18.12 org.junit.jupiter junit-jupiter RELEASE compile ``` 2、编写Spring注入的配置文件 ``` ``` 3、进行Spring对象获取 ``` @Test public void testSpringBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user = context.getBean("userBeanTest", User.class); System.out.println(user); } ``` > ![](https://upload-images.jianshu.io/upload_images/1096351-2d1cbf57988b5468.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -- -- ## 4)ApplicatonContext能力 > 核心功能:就是JavaBean的获取 > ![](https://upload-images.jianshu.io/upload_images/1096351-36001752c637f9f8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -- -- ## 5)XML_Schema了解 ``` ``` - xmlns:xml-namespace:后面的namespace是前缀,进行对象定义的约束 > 举例说明:xmlns:xsi,xsi在【属性定义的时候,添加说明前缀的】 > ![](https://upload-images.jianshu.io/upload_images/1096351-ee680c24bffe1479.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - xsi:schemaLocation:schemaLocation的位置,这里是真实的URL链接,可以直接进行网页访问,用来进行真实格式约束的,内部定义了我们可以使用的标签 > ![](https://upload-images.jianshu.io/upload_images/1096351-5d201af95c1f8fd9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) > ![](https://upload-images.jianshu.io/upload_images/1096351-eff5ff7a5dbf2857.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -- -- ## 6)什么是ClassPath > 参考网址:[java项目中的classpath到底是什么](https://segmentfault.com/a/1190000015802324) > 当前项目不是web项目,只是使用Spring进行对象获取[就是Java的编译而已],所以就是代指target目录下的文件 > ![](https://upload-images.jianshu.io/upload_images/1096351-ce7326ac1c6664c9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -- -- # 5、课后习题 ## 1)什么是【SSH和SSM】? > SSH: Struts+Spring+Hibernate【是16年之前较为流行的一种Web应用程序开源框架】 > SSM:Spring+SpringMVC+MyBatis -- -- ## 2)什么是IOC容器?什么是序列化?什么是依赖注入? | id | name | function| description| |----|----------|-----|--------| | 1 | 对象序列化| implements Serializable | 方便对象在网络传输中进行格式的转化 | | 2 | 控制反转IOC | Inversion of Control,即“控制反转” | 通过IOC进行属性读取 [以前是new对象,现在是读取配置] | | 3 | 依赖注入DI | Dependency Injection,即“依赖注入” | 通过IOC进行属性注入,以前是new对象,显示是配置文件注入属性 | -- -- ## 3)如何进行Spring对象注入和获取? ``` @Test public void testSpringBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user = context.getBean("userBeanTest", User.class); System.out.println(user); } ``` -- -- ## 4)XML_Schemas内部构成 - xmlns:xml-namespace:后面的namespace是前缀,进行对象定义的约束 - xsi:schemaLocation:schemaLocation的位置,这里是真实的URL链接,可以直接进行网页访问,用来进行真实格式约束的,内部定义了我们可以使用的标签 -- -- ## 5)引入Spring测试框架 - 添加依赖 ``` org.springframework spring-test 5.3.1 junit junit 4.12 test ``` - 进行功能测试 ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:beans.xml") public class TestApplication { @Resource User user; @Test public void test(){ System.out.println("----------->"+user); } } ``` -- -- ## 6)Spring使用JdbcTemplate > 如果想使用@Configuration,一定要配置和对象的头文件引入 - 引入依赖[当前没有使用MyBatis,而是使用Spring原生操作数据库的方法] ``` com.alibaba druid 1.2.2 org.springframework spring-jdbc 5.3.1 mysql mysql-connector-java 8.0.22 ``` - 添加数据库对象到Spring中 ``` ``` - 进行数据库操作 ``` @Test public void testSpringJdbc() { // 进行数据库插入 template.execute("INSERT INTO USER (`name`, `age`) VALUES ( '夏明', '21');"); System.out.println("----------->进行数据库语句插入"); // 进行数据库查询 // 这玩意就是建立映射关系,避免不了,这里是测试,实际上还是新建一个类好。 RowMapper rowMapper = new RowMapper() { public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setUserName(resultSet.getString("name")); user.setAge(resultSet.getInt("age")); return user; } }; List queryList = template.query("select * from user", rowMapper); System.out.println("----------->"+queryList); } ```