javaweb:maven-->javaweb-->Tomcat
---->mybatis--->spring--->springmvc--->spring boot
一:创建Maven的web项目
首先我们设置好maven的基本环境。
以下是我们最终需要的目录结构:
创建基于maven的web有两种方法:
第一种是直接两种一起创建。第二种是创建空的maven的项目,设置好后再添加web。(我们推荐这种方法)
1. 第一种
在博客:Idea中创建maven项目(超详细)_Yan Yang的博客-CSDN博客_idea创建maven项目讲解的比较具体。
2. 第二种
先需要创建一个maven项目
(1)2022最新的版本的idea创建方法(比较简单)
再增加resource目录即可。我们新建test的测试类测试一下,以此maven构建完成。
(2)之前版本的idea创建方法
只是前面略微不同,后边finish以后方法都一样。
-----------------------------------------------------------------------------------------------------------
然后再导入web项目
这样一个完整的maven-web就出来了。
二:tomcat使用
Tomcat在官网下载,解压后,配置好环境变量就可以使用。
在IDEA中配置
这里可以设置初始的url:例如http://localhost:8080/abc/index.html是默认在自己创建的myweb目录下的。由此,tomcat配置完成。
还有很多小技巧,例如热部署,之后再补充。
三:mybatis
1. ORM介绍
-
ORM(Object Relational Mapping): 对象关系映射
-
指的是持久化数据和实体对象的映射模式,为了解决面向对象与关系型数据库存在的互不匹配的现象的技术。
2. 为什么要用这种ORM?
针对原始的JDBC,存在以下几点问题:
例如:
-
原始 JDBC 的操作问题分析
1.频繁创建和销毁数据库的连接会造成系统资源浪费从而影响系统性能。
-
sql 语句在代码中硬编码,如果要修改 sql 语句,就需要修改 java 代码,造成代码不易维护。
-
查询操作时,需要手动将结果集中的数据封装到实体对象中。
-
增删改查操作需要参数时,需要手动将实体对象的数据设置到 sql 语句的占位符。
-
-
原始 JDBC 的操作问题解决方案
1.使用数据库连接池初始化连接资源。
-
将 sql 语句抽取到配置文件中。
-
使用反射、内省等底层技术,将实体与表进行属性与字段的自动映射
-
3. Mybatis的快速入门
MyBatis开发步骤:
①添加MyBatis的jar包
②创建Student数据表
③编写Studentr实体类
④编写映射文件StudentMapper.xml *****
⑤编写核心文件MyBatisConfig.xml *****
⑥编写测试类
(1)环境的基本搭建和入门程序
完整的目录结构:《这里的mapperImpl相当于dao层(dao层就是直接对数据库的持久化操作)》,具体的流程写在小结中了。
1) 导入MyBatis的jar包
2)创建student数据表
3)编写Student类和其他相关的MVC实现类
Student:
package com.itheima.bean;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
StudentMapperImpl :
package com.itheima.mapper.impl;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
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;
import java.util.List;
/*
持久层实现类
*/
public class StudentMapperImpl implements StudentMapper {
/*
查询全部
*/
@Override
public List<Student> selectAll() {
List<Student> list = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.执行映射配置文件中的sql语句,并接收结果
list = sqlSession.selectList("StudentMapper.selectAll");
} catch (Exception e) {
e.printStackTrace();
} finally {
//5.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//6.返回结果
return list;
}
/*
根据id查询
*/
@Override
public Student selectById(Integer id) {
Student stu = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.执行映射配置文件中的sql语句,并接收结果
stu = sqlSession.selectOne("StudentMapper.selectById",id);
} catch (Exception e) {
e.printStackTrace();
} finally {
//5.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//6.返回结果
return stu;
}
/*
新增功能
*/
@Override
public Integer insert(Student stu) {
Integer result = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.执行映射配置文件中的sql语句,并接收结果
result = sqlSession.insert("StudentMapper.insert",stu);
} catch (Exception e) {
e.printStackTrace();
} finally {
//5.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//6.返回结果
return result;
}
/*
修改功能
*/
@Override
public Integer update(Student stu) {
Integer result = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.执行映射配置文件中的sql语句,并接收结果
result = sqlSession.update("StudentMapper.update",stu);
} catch (Exception e) {
e.printStackTrace();
} finally {
//5.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//6.返回结果
return result;
}
/*
删除功能
*/
@Override
public Integer delete(Integer id) {
Integer result = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.执行映射配置文件中的sql语句,并接收结果
result = sqlSession.delete("StudentMapper.delete",id);
} catch (Exception e) {
e.printStackTrace();
} finally {
//5.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//6.返回结果
return result;
}
}
StudentMapper :
package com.itheima.mapper;
import com.itheima.bean.Student;
import java.util.List;
/*
持久层接口
*/
public interface StudentMapper {
//查询全部
public abstract List<Student> selectAll();
//根据id查询
public abstract Student selectById(Integer id);
//新增数据
public abstract Integer insert(Student stu);
//修改数据
public abstract Integer update(Student stu);
//删除数据
public abstract Integer delete(Integer id);
}
StudentServiceImpl :
package com.itheima.service.impl;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.mapper.impl.StudentMapperImpl;
import com.itheima.service.StudentService;
import java.util.List;
/*
业务层实现类
*/
public class StudentServiceImpl implements StudentService {
//创建持久层对象
private StudentMapper mapper = new StudentMapperImpl();
@Override
public List<Student> selectAll() {
return mapper.selectAll();
}
@Override
public Student selectById(Integer id) {
return mapper.selectById(id);
}
@Override
public Integer insert(Student stu) {
return mapper.insert(stu);
}
@Override
public Integer update(Student stu) {
return mapper.update(stu);
}
@Override
public Integer delete(Integer id) {
return mapper.delete(id);
}
}
StudentService :
package com.itheima.service;
import com.itheima.bean.Student;
import java.util.List;
/*
业务层接口
*/
public interface StudentService {
//查询全部
public abstract List<Student> selectAll();
//根据id查询
public abstract Student selectById(Integer id);
//新增数据
public abstract Integer insert(Student stu);
//修改数据
public abstract Integer update(Student stu);
//删除数据
public abstract Integer delete(Integer id);
}
4)编写StudentMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
mapper:核心根标签
namespace属性:名称空间
-->
<mapper namespace="StudentMapper">
<!--
select:查询功能的标签
id属性:唯一标识
resultType属性:指定结果映射对象类型
parameterType属性:指定参数映射对象类型
-->
<select id="selectAll" resultType="student">
SELECT * FROM student
</select>
<select id="selectById" resultType="student" parameterType="int">
SELECT * FROM student WHERE id = #{id}
</select>
<insert id="insert" parameterType="student">
INSERT INTO student VALUES (#{id},#{name},#{age})
</insert>
<update id="update" parameterType="student">
UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
5)编写Mybatis核心文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration 核心根标签-->
<configuration>
<!--引入数据库连接的配置文件-->
<properties resource="jdbc.properties"/>
<!--配置LOG4J-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--起别名-->
<typeAliases>
<typeAlias type="com.itheima.bean.Student" alias="student"/>
<!--<package name="com.itheima.bean"/>-->
</typeAliases>
<!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个-->
<environments default="mysql">
<!--environment配置数据库环境 id属性唯一标识-->
<environment id="mysql">
<!-- transactionManager事务管理。 type属性,采用JDBC默认的事务-->
<transactionManager type="JDBC"></transactionManager>
<!-- dataSource数据源信息 type属性 连接池-->
<dataSource type="POOLED">
<!-- property获取数据库连接的配置信息 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- mappers引入映射配置文件 -->
<mappers>
<!-- mapper 引入指定的映射配置文件 resource属性指定映射配置文件的名称 -->
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
Controller: 可以开始测试了
package com.itheima.controller;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.List;
/*
控制层测试类
*/
public class StudentController {
//创建业务层对象
private StudentService service = new StudentServiceImpl();
//查询全部功能测试
@Test
public void selectAll() {
List<Student> students = service.selectAll();
for (Student stu : students) {
System.out.println(stu);
}
}
//根据id查询功能测试
@Test
public void selectById() {
Student stu = service.selectById(3);
System.out.println(stu);
}
//新增功能测试
@Test
public void insert() {
Student stu = new Student(4,"赵六",26);
Integer result = service.insert(stu);
System.out.println(result);
}
//修改功能测试
@Test
public void update() {
Student stu = new Student(4,"赵六",16);
Integer result = service.update(stu);
System.out.println(result);
}
//删除功能测试
@Test
public void delete() {
Integer result = service.delete(4);
System.out.println(result);
}
}
(2)简单的mybatis总结
导入libs三个jar包--->(controller访问-->service--->Mapper类(相当于dao层,dao层就是数据库的持久化操作)-->MybatisConfig.xml--->jdbc.properties(数据库账号密码保存), StudentMapper.xml(sql语句))
(1):创建Student实体对象类,创建mapper映射StudentMapper接口类(设计对数据库的增删改查等操作的接口)。
(2):创建StudentMapperImpl实现接口(这里相当于dao层)(加载配置文件中的sql语句,实现增删改查<调用mapper配置中的namespace.id找到相关的sql语句>)。(在后面会讲另一种方法)
起个别名:com.itheima.bean.Student类的地址=student,方便在namespace调用。
<!--起别名-->
<typeAliases>
<typeAlias type="com.itheima.bean.Student" alias="student"/>
<!--<package name="com.itheima.bean"/>-->
</typeAliases>
(3):创建StudentService接口(设计业务类的方法,这里也写数据库的增删改查接口,和StudentMapper保持一致),创建StudentServiceImpl实现接口类(业务层实现类,StudentMapper mapper = new StudentMapperImpl(),可以之直接return mapper的相关返回方法值)。
(4):创建controller类(StudentService service = new StudentServiceImpl(); 直接调用service中的方法就可以返回对应参数。)
注: public List<Student> selectAll() {}查询所有学生的时候会查询到多个实体对象,所以这边使用的是List<>:
// 关于List<>的用法
students = sqlSession.selectList("StudentMapper.selectAll");
List<Student> students = service.selectAll();
for (Student stu : students) {
System.out.println(stu);
}
(3)mybatis的dao层实现接口代理
就是我想删除StudentMapperImpl的实现类,mapper里只保留了StudentMapper接口类。
目录结构如下:
既然删掉了StudentMapperImpl类,那数据库持久化的操作放到哪里?
我们放到了Service的实现类里。调用如下方法就可以了;
StudentServiceImp类:
package com.itheima.service.impl;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
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;
import java.util.List;
/*
业务层实现类
*/
public class StudentServiceImpl implements StudentService {
@Override
public List<Student> selectAll() {
List<Student> list = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
list = mapper.selectAll();
} catch (Exception e) {
} finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return list;
}
@Override
public Student selectById(Integer id) {
Student stu = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
stu = mapper.selectById(id);
} catch (Exception e) {
} finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return stu;
}
@Override
public Integer insert(Student stu) {
Integer result = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
result = mapper.insert(stu);
} catch (Exception e) {
} finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return result;
}
@Override
public Integer update(Student stu) {
Integer result = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
result = mapper.update(stu);
} catch (Exception e) {
} finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return result;
}
@Override
public Integer delete(Integer id) {
Integer result = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
result = mapper.delete(id);
} catch (Exception e) {
} finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return result;
}
}
问题在于这个 sqlSession.getMapper(StudentMapper.class)方法怎么可以访问到配置文件中的sql语句。我们需要将配置文件进行四项订正;
Mapper 接口开发需要遵循以下规范:
**1) Mapper.xml文件中的namespace与mapper接口的全限定名相同**
**2) Mapper接口方法名和Mapper.xml中定义的每个statement的id相同**
**3) Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同**
**4) Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同**
对应如下的几点配置:(必须一一对应起来。namespace必须用全称)
<!--起别名-->
<typeAliases>
<typeAlias type="com.itheima.bean.Student" alias="student"/>
<!--<package name="com.itheima.bean"/>-->
</typeAliases>
总结:好处在哪里?
好处就是我们在写代码的时候不用去找配置文件中的namespace,id去调用sql。直接去找mapper抽象类的函数去调用就可以了。
(4)mysql进阶之动态sql(小结sql)
回顾:我们之前的sql都是静态sql
<select id="selectById" resultType="student" parameterType="int">
SELECT * FROM student WHERE id = #{id}
</select>
// 我们调用
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
stu = mapper.selectById(ID);
// 这里的参数ID传进去,就会自动传到#{id}
// -------------------------------------------------------------------
<update id="update" parameterType="student">
UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}
</update>
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
result = mapper.update(stu);
// 我们传入stu,就会自动将stu的三个参数对应进去。
// ---------------------------------------------------------------------
<select id="selectAll" resultType="student">
SELECT * FROM student
</select>
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
result = mapper.update(stu);
// 现在我们提出了另外一种需求。就是根据输入的id或者username查询student信息。在id如果不为 // 空时可以根据id查询,如果username 不同空时还要加入用户名作为条件。现在就涉及到判断了;
动态sql之if:
我们根据实体类的不同取值,使用不同的 SQL语句来进行查询。比如在 id如果不为空时可以根据id查询,如果username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。
<select id="findByCondition" parameterType="student" resultType="student">
select * from student
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
</where>
</select>
总结语法:
<where>:条件标签。如果有动态条件,则使用该标签代替 where 关键字。
<if>:条件判断标签。
<if test=“条件判断”>
查询条件拼接
</if>
动态sql之foreach:
循环执行sql的拼接操作,例如:SELECT * FROM student WHERE id IN (1,2,5)。
<select id="findByIds" parameterType="list" resultType="student">
select * from student
<where>
<foreach collection="array" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
测试:
//获得MyBatis框架生成的UserMapper接口的实现类
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int[] ids = new int[]{2,5};
List<Student> sList = mapper.findByIds(ids);
System.out.println(sList);
总结语法:
<foreach>:循环遍历标签。适用于多个参数或者的关系。
<foreach collection=“”open=“”close=“”item=“”separator=“”>
获取参数
</foreach>
属性
collection:参数容器类型, (list-集合, array-数组)。
open:开始的 SQL 语句。
close:结束的 SQL 语句。
item:参数变量名。
separator:分隔符。
SQL重复片段抽取:
<!--抽取sql片段简化编写-->
<sql id="selectStudent" select * from student</sql>
<select id="findById" parameterType="int" resultType="student">
<include refid="selectStudent"></include> where id=#{id}
</select>
<select id="findByIds" parameterType="list" resultType="student">
<include refid="selectStudent"></include>
<where>
<foreach collection="array" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
总结语法:
我们可以将一些重复性的 SQL 语句进行抽取,以达到复用的效果。
- <sql>:抽取 SQL 语句标签。
- <include>:引入 SQL 片段标签。
<sql id=“片段唯一标识”>抽取的 SQL 语句</sql> <include refid=“片段唯一标识”/>
小结:
<select>:查询
<insert>:插入
<update>:修改
<delete>:删除
<where>:where条件
<if>:if判断
<foreach>:循环
<sql>:sql片段抽取
(5)分页插件的使用(在企业级开发常用到)
我们在mysql中,可以用limit显示当前的数据库条数。
select * from student limit 0, 3 第一页3条数据
select * from student limit 3, 3 第二页3条数据
select * from student limit 6, 3
在企业级开发中,可以借用PageHelper插件进行的分页操作。
开发步骤:
①导入与PageHelper的jar包
②在mybatis核心配置文件中配置PageHelper插件
<!-- 注意:分页助手的插件 配置在通用mapper之前 放到其他位置可能还会报错-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
③测试分页数据获取
import com.github.pagehelper.PageHelper;
public class StudentController {
//创建业务层对象
private StudentService service = new StudentServiceImpl();
//查询全部功能测试
@Test
public void selectAll() {
PageHelper.startPage(1,2); // 第一页显示两条数据的意思
// PageHelper.startPage(2,2); // // 第一页显示两条数据的意思
List<Student> students = service.selectAll();
for (Student stu : students) {
System.out.println(stu);
}
}
}
//**获得分页相关的其他参数**:
PageInfo<Student> pageInfo = new PageInfo<Student>(select);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("每页显示长度:"+pageInfo.getPageSize());
System.out.println("是否第一页:"+pageInfo.isIsFirstPage());
System.out.println("是否最后一页:"+pageInfo.isIsLastPage());
(6)多表操作
多表模型分类:
一对一:在任意一方建立外键,关联对方的主键。
一对多:在多的一方建立外键,关联一的一方的主键。
多对多:借助中间表,中间表至少两个字段,分别关联两张表的主键。
一对一:
我们建立两个表,人和身份证,一个人只有一个身份证。
CREATE DATABASE db2;
USE db2;
CREATE TABLE person(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
age INT
);
INSERT INTO person VALUES (NULL,'张三',23);
INSERT INTO person VALUES (NULL,'李四',24);
INSERT INTO person VALUES (NULL,'王五',25);
CREATE TABLE card(
id INT PRIMARY KEY AUTO_INCREMENT,
number VARCHAR(30),
pid INT,
CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id)
);
INSERT INTO card VALUES (NULL,'12345',1);
INSERT INTO card VALUES (NULL,'23456',2);
INSERT INTO card VALUES (NULL,'34567',3);
一对多:我们创建班级表classes和student表,一个班里有很多学生,是一个一对多的关系
CREATE TABLE classes(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO classes VALUES (NULL,'黑马一班');
INSERT INTO classes VALUES (NULL,'黑马二班');
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30),
age INT,
cid INT,
CONSTRAINT cs_fk FOREIGN KEY (cid) REFERENCES classes(id)
);
INSERT INTO student VALUES (NULL,'张三',23,1);
INSERT INTO student VALUES (NULL,'李四',24,1);
INSERT INTO student VALUES (NULL,'王五',25,2);
INSERT INTO student VALUES (NULL,'赵六',26,2);
多对多:我们创建课程表courses,stu_cr(保存每个student的courses的id<sid, cid>),外加事先存在的student表。stu_cr是中间表,实现多对多的关系。
CREATE TABLE course(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO course VALUES (NULL,'语文');
INSERT INTO course VALUES (NULL,'数学');
CREATE TABLE stu_cr(
id INT PRIMARY KEY AUTO_INCREMENT,
sid INT,
cid INT,
CONSTRAINT sc_fk1 FOREIGN KEY (sid) REFERENCES student(id),
CONSTRAINT sc_fk2 FOREIGN KEY (cid) REFERENCES course(id)
);
INSERT INTO stu_cr VALUES (NULL,1,1);
INSERT INTO stu_cr VALUES (NULL,1,2);
INSERT INTO stu_cr VALUES (NULL,2,1);
INSERT INTO stu_cr VALUES (NULL,2,2);
目录结构:其中resources里面我们复制了一份xml文件,这是为了MybatisConfig中能访问到:
<mappers>
<mapper resource="com/itheima/one_to_one/OneToOneMapper.xml"/>
<mapper resource="com/itheima/one_to_many/OneToManyMapper.xml"/>
<mapper resource="com/itheima/many_to_many/ManyToManyMapper.xml"/>
</mappers>
一对一:
bean包:Card中,我们加入了Person p表示所属人的对象。Student类不变。
package com.itheima.bean;
public class Card {
private Integer id; //主键id
private String number; //身份证号
private Person p; //所属人的对象
public Card() {
}
public Card(Integer id, String number, Person p) {
this.id = id;
this.number = number;
this.p = p;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Person getP() {
return p;
}
public void setP(Person p) {
this.p = p;
}
@Override
public String toString() {
return "Card{" +
"id=" + id +
", number='" + number + '\'' +
", p=" + p +
'}';
}
}
OneToOneMapper类:
package com.itheima.table02;
import com.itheima.bean.Classes;
import java.util.List;
public interface OneToManyMapper {
//查询全部
public abstract List<Classes> selectAll();
}
MybatisConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration 核心根标签-->
<configuration>
<!--引入数据库连接的配置文件-->
<properties resource="jdbc.properties"/>
<!--配置LOG4J-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--起别名-->
<typeAliases>
<package name="com.itheima.bean"/>
</typeAliases>
<!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个-->
<environments default="mysql">
<!--environment配置数据库环境 id属性唯一标识-->
<environment id="mysql">
<!-- transactionManager事务管理。 type属性,采用JDBC默认的事务-->
<transactionManager type="JDBC"></transactionManager>
<!-- dataSource数据源信息 type属性 连接池-->
<dataSource type="POOLED">
<!-- property获取数据库连接的配置信息 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- mappers引入映射配置文件 -->
<mappers>
<mapper resource="com/itheima/one_to_one/OneToOneMapper.xml"/>
<mapper resource="com/itheima/one_to_many/OneToManyMapper.xml"/>
<mapper resource="com/itheima/many_to_many/ManyToManyMapper.xml"/>
</mappers>
</configuration>
OneToOneMapper.xml\
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.table01.OneToOneMapper">
<!--配置字段和实体对象属性的映射关系-->
<resultMap id="oneToOne" type="card">
<id column="cid" property="id" />
<result column="number" property="number" />
<!--
association:配置被包含对象的映射关系
property:被包含对象的变量名
javaType:被包含对象的数据类型
-->
<association property="p" javaType="person">
<id column="pid" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
</association>
</resultMap>
<select id="selectAll" resultMap="oneToOne">
SELECT c.id cid,number,pid,NAME,age FROM card c,person p WHERE c.pid=p.id
</select>
</mapper>
测试类:test01:
package com.itheima.table01;
import com.itheima.bean.Card;
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 org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.获取OneToOneMapper接口的实现类对象
OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);
//5.调用实现类的方法,接收结果
List<Card> list = mapper.selectAll();
//6.处理结果
for (Card c : list) {
System.out.println(c);
}
//7.释放资源
sqlSession.close();
is.close();
}
}
一对多:增加classes类,里面增加了List<Student> student对象;
package com.itheima.bean;
import java.util.List;
public class Classes {
private Integer id; //主键id
private String name; //班级名称
private List<Student> students; //班级中所有学生对象
public Classes() {
}
public Classes(Integer id, String name, List<Student> students) {
this.id = id;
this.name = name;
this.students = students;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Classes{" +
"id=" + id +
", name='" + name + '\'' +
", students=" + students +
'}';
}
}
OneToManyMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.table02.OneToManyMapper">
<resultMap id="oneToMany" type="classes">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
<!--
collection:配置被包含的集合对象映射关系
property:被包含对象的变量名
ofType:被包含对象的实际数据类型
-->
<collection property="students" ofType="student">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="sage" property="age"/>
</collection>
</resultMap>
<select id="selectAll" resultMap="oneToMany">
SELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.id=s.cid
</select>
</mapper>
测试类:
package com.itheima.table02;
import com.itheima.bean.Classes;
import com.itheima.bean.Student;
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 org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.获取OneToManyMapper接口的实现类对象
OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);
//5.调用实现类的方法,接收结果
List<Classes> classes = mapper.selectAll();
//6.处理结果
for (Classes cls : classes) {
System.out.println(cls.getId() + "," + cls.getName());
List<Student> students = cls.getStudents();
for (Student student : students) {
System.out.println("\t" + student);
}
}
//7.释放资源
sqlSession.close();
is.close();
}
}
多对多:
增加Course类,修改Student类《 private List<Course> courses》
package com.itheima.bean;
public class Course {
private Integer id; //主键id
private String name; //课程名称
public Course() {
}
public Course(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Course{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package com.itheima.bean;
import java.util.List;
public class Student {
private Integer id; //主键id
private String name; //学生姓名
private Integer age; //学生年龄
private List<Course> courses; // 学生所选择的课程集合
public Student() {
}
public Student(Integer id, String name, Integer age, List<Course> courses) {
this.id = id;
this.name = name;
this.age = age;
this.courses = courses;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
ManyToManyMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.table03.ManyToManyMapper">
<resultMap id="manyToMany" type="student">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="sage" property="age"/>
<collection property="courses" ofType="course">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
</collection>
</resultMap>
<select id="selectAll" resultMap="manyToMany">
SELECT sc.sid,s.name sname,s.age sage,sc.cid,c.name cname FROM student s,course c,stu_cr sc WHERE sc.sid=s.id AND sc.cid=c.id
</select>
</mapper>
测试类:
package com.itheima.table03;
import com.itheima.bean.Course;
import com.itheima.bean.Student;
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 org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.获取ManyToManyMapper接口的实现类对象
ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class);
//5.调用实现类的方法,接收结果
List<Student> students = mapper.selectAll();
//6.处理结果
for (Student student : students) {
System.out.println(student.getId() + "," + student.getName() + "," + student.getAge());
List<Course> courses = student.getCourses();
for (Course cours : courses) {
System.out.println("\t" + cours);
}
}
//7.释放资源
sqlSession.close();
is.close();
}
}
总结配置:
一对一配置:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<association>:配置被包含对象的映射关系标签。
property 属性:被包含对象的变量名
javaType 属性:被包含对象的数据类型
一对多配置:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
多对多配置:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
总结:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<association>:配置被包含对象的映射关系标签。
property 属性:被包含对象的变量名
javaType 属性:被包含对象的数据类型
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
三. javaweb的核心内容
1. http协议
HTTP的全称是:Hyper Text Transfer Protocol,意为 超文本传输协议。它指的是服务器和客户端之间交互必须遵循的一问一答的规则。形容这个规则:问答机制、握手机制。
协议就是规则,就是报文传输:
请求部分:
请求行: 永远位于请求的第一行 请求消息头: 从第二行开始,到第一个空行结束 请求的正文: 从第一个空行后开始,到正文的结束。
响应部分:
响应行: 永远位于响应的第一行 响应消息头: 从第二行开始,到第一个空行结束 响应的正文: 从第一个空行后开始,到正文的结束。
2. Servlet
Servlet就是http协议的接口,可以接收从前端传过来的数据。(类似于网络编程中socket).
Servlet翻译成中文是服务端脚本。
怎么接收?如何配置?
客户端使用http协议发起请求给Tomcat服务器,Tomcat服务器解析http协议的地址部分,找到服务器上的项目,然后再根据web.xml找到对应的实现的功能类(路由),然后功能类中有继承servlet类,servlet会初始化并实现service方法,最终响应给客户端浏览器。
web.xml配置:
StudentServlet实现类:
3. spring
(1)基础概念
Spring是分层的JAVASE/EE应用full-stack轻量级开源框架。(分层就是可以把其中一块拿出来单独使用)
底层核心容器是spring必不可少的,AOP是一种技术规范,Aspects是实现AOP的方法。
应用层的技术才是重点,主要做了数据接入和web的集成,例如把不同数据库的JDBC操作做了集成,不用那么麻烦的写了。Spring还提供了Test测试方法。
(2)IOC的概念(反转控制)
优质程序代码的制作原则:
我们之前的代码,包括mybatis都会手动写mysql的连接过程,如果换了数据库,用户名密码改了?
那么代码要重写写? (高耦合了,代码书写中所使用的技术的结合紧密度太高了)
JDBC单独的模块实现高内聚(单个模块内部各组成部分间的联系紧密),外边不要一直那么多的调用。
我们的代码规则就是低耦合,高内聚。
我们之前的代码是service直接调用dao层(应用程序直接调用资源),如果dao层方法改了,service中的代码也要改,这就是一种紧耦合的形式。
我们现在增加一个工厂模式:(只要更改工厂类中实现类),现在应用程序和资源不耦合了,但是工厂和资源耦合了。
我们再增加一个配置文件:(只要更改配置文件的参数),现在是配置文件和资源的紧耦合。
配置文件有啥好处?
不需要代码重新打包发布,只要把配置文件更新一下就可以了!很方便。
所谓的工厂+配置,就是spring的雏形。
怎么理解IOC?控制反转
原来我们的service是自己主动获取dao资源,现在不需要主动new来获取了,我们使用了工厂+配置,现在配置都放到spring容器里了,我们的service等着资源传过来就可以了。。
入门案例:
案例环境说明
-
模拟三层架构中表现层调用业务层功能
-
表现层:UserApp模拟UserServlet(使用main方法模拟)
-
业务层:UserService
-
IoC入门案例制作步骤
1.导入spring坐标(5.1.9.release)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
2.编写业务层与表现层(模拟)接口与实现类
public interface UserService {
//业务方法
void save();
}
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("user service running...");
}
}
3.建立spring配置文件
4.配置所需资源(Service)为spring控制的资源(这里我们控制Service,通常是控制dao,都可以)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.创建spring控制的资源-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
</beans>
5.表现层(App)通过spring获取资源(Service实例)
public class UserApp {
public static void main(String[] args) {
//2.加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//3.获取资源
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
注:bean基本属性说明
<bean id="beanId" name="beanName1,beanName2" class="ClassName"></bean>
id:bean的名称,通过id值获取bean
class:bean的类型
name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名
<bean scope="singleton"></bean>
取值:
- singleton:设定创建出的对象保存在spring容器中,是一个单例的对象。(意思就是new出来的对象都是同一个内存的)
- prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象(意思就是new出来的对象都是不同内存的)
-- 其中singleton在new ClassPathXmlApplicationContext对象的时候会自动调用构造方法,而prototype不会调用。
- request、session、application、 websocket :设定创建出的对象放置在web容器对应的位置
<bean init-method="init" destroy-method="destroy></bean>
- 取值:bean对应的类中对应的具体方法名
- 注意事项:
- 当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次
- 当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次
- 当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次
- 当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行
以下为了解:
<bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
- 取值:工厂bean中用于获取对象的静态方法名
- 注意事项:
- class属性必须配置成静态工厂的类名
<bean factory-bean="factoryBeanId" factory-method="factoryMethodName"></bean>
- 取值:工厂bean中用于获取对象的实例方法名
- 注意事项:
- 使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
- factory-bean是实例工厂的beanId
(3)DI(依赖注入)
-
DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入