java 使用spring_java_spring

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

a8569578b3b63f34b49525cbf8cb28ef.png

2)什么是IOC容器?什么是序列化?什么是依赖注入?

id

name

function

description

1

对象序列化

implements Serializable

方便对象在网络传输中进行格式的转化

2

控制反转IOC

Inversion of Control,即“控制反转”

通过IOC进行属性读取 [以前是new对象,现在是读取配置]

3

依赖注入DI

Dependency Injection,即“依赖注入”

通过IOC进行属性注入,以前是new对象,显示是配置文件注入属性

【序列化】VS【依赖注入】

9e9d9175c65807033726d1ae651be92d.png

什么是【IOC】

3671d1ef3b8128b28739159571bf437e.png

说明一点:Spring目前只需要导入一个包既可以完成所有的包导入

36b3f25e2b4385e891ee1fa2a0bf6bc3.png

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);

}

21b46662e3416584661d8276f8d09781.png

4)ApplicatonContext能力

核心功能:就是JavaBean的获取

9ba8e4309a7e950798650ed702c8c0d7.png

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在【属性定义的时候,添加说明前缀的】

3b0ad80bc2dbf64c20f2e449624b06b0.png

xsi:schemaLocation:schemaLocation的位置,这里是真实的URL链接,可以直接进行网页访问,用来进行真实格式约束的,内部定义了我们可以使用的标签

db2afc8e0af814d7381058663f10275b.png

694304d718b2e6242ec81b4fcdad79c1.png

6)什么是ClassPath

当前项目不是web项目,只是使用Spring进行对象获取[就是Java的编译而已],所以就是代指target目录下的文件

583e780488eadb67a21c7ad8778f06a2.png

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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值