1、写作背景
总想走捷径的人,往往因找不到捷径而固步自封,一步不前!
2、参考网址
3、学习目的
SSH和SSM
什么是IOC容器?什么是序列化?什么是依赖注入?
如何进行Spring对象注入和获取
ApplicatonContext能力
XML_Schema了解
什么是ClassPath
4、核心操作
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对象,显示是配置文件注入属性
【序列化】VS【依赖注入】
什么是【IOC】
说明一点:Spring目前只需要导入一个包既可以完成所有的包导入
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注入的配置文件
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
3、进行Spring对象获取
@Test
public void testSpringBean(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("userBeanTest", User.class);
System.out.println(user);
}
4)ApplicatonContext能力
核心功能:就是JavaBean的获取
5)XML_Schema了解
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
xmlns:xml-namespace:后面的namespace是前缀,进行对象定义的约束
举例说明:xmlns:xsi,xsi在【属性定义的时候,添加说明前缀的】
xsi:schemaLocation:schemaLocation的位置,这里是真实的URL链接,可以直接进行网页访问,用来进行真实格式约束的,内部定义了我们可以使用的标签
6)什么是ClassPath
当前项目不是web项目,只是使用Spring进行对象获取[就是Java的编译而已],所以就是代指target目录下的文件
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);
}