diff --git "a/03 \350\265\226\345\277\203\345\246\215/20231219 Maven.md" "b/03 \350\265\226\345\277\203\345\246\215/20231219 Maven.md" new file mode 100644 index 0000000000000000000000000000000000000000..68a5a3769527aef15388c4af48c02ee8ec0e16ce --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20231219 Maven.md" @@ -0,0 +1,35 @@ +## Maven + +Maven是专门用于管理和构建Java项目的工具,它的主要功能有: + +提供了一套标准化的项目结构 + +提供了一套标准化的构建流程(编译,测试,打包,发布……) + +提供了一套依赖管理机制 + +仓库分类: 本地仓库:自己计算机上的一个目录 + +中央仓库:由Maven团队维护的全球唯一的仓库 + +地址: [https://repo1.maven.org/maven2/](https://gitee.com/link?target=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2F) + +远程仓库(私服):一般由公司团队搭建的私有仓库,国内镜像也是远程仓库的一种 当项目中使用坐标引入对应依赖jar包后,首先会查找本地仓库中是否有对应的jar 包: 如果有,则在项目直接引用; + +如果没有,则去中央仓库中下载对应的jar包到本地仓库。 + +```java +import org.junit.Test; + +public class APP { + @Test + public void test01(){ + System.out.println(999); + } + @Test + public void test02(){ + System.out.println("吃饭了吗"); + } +} +``` + diff --git "a/03 \350\265\226\345\277\203\345\246\215/20231220 Mybatis.md" "b/03 \350\265\226\345\277\203\345\246\215/20231220 Mybatis.md" new file mode 100644 index 0000000000000000000000000000000000000000..7d9ed33353e6e6719175a1b5d6952835309fb12d --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20231220 Mybatis.md" @@ -0,0 +1,288 @@ +## Mybatis增删改查 + +### 1.pom.xml + +```xml + + + 4.0.0 + + org.example + untitled + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + + junit + junit + + 4.13.2 + + test + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + + org.mybatis + mybatis + 3.5.13 + + + + + +``` + +### 2.mysql建库建表 + +### 3.main包下java包建一个域名pojo包(com.yina.pojo)里建一个User类 + +与mysql的tb_user表里字段名对应 + +```java +package com.yina.pojo; + +public class User { + private Integer id; + private String username; + private Integer age; + private String gender; + + // javaBean + + public User(Integer id, String username, Integer age, String gender) { + this.id = id; + this.username = username; + this.age = age; + this.gender = gender; + } + + public User() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", age=" + age + + ", gender='" + gender + '\'' + + '}'; + } +} +``` + +### 4.mian包里的resources包里创建mybatis-config.xml文件 + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +### 5.mian包里的resources包里创建UserMapper.xml文件 + +```xml + + + + + + + + + + + insert into tb_user (id,username,age,gender) values (#{id},#{username},#{age},#{gender}); + + + + delete from tb_user where id = #{id}; + + + + update tb_user + set username = #{username}, + age = #{age}, + gender = #{gender} + where id = #{id}; + + +``` + +### 6.test包下java包下创建域名包(com.yina)下创建TestUser类 + +```java +package com.yina; + +import com.yina.pojo.User; +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.IOException; +import java.io.InputStream; +import java.util.List; + +// 因为每用一个方法都要重复执行所以把这一部分代码封装成一个方法 +public class TestUser { + private SqlSessionFactory getSqlSessionFactory() throws IOException { + String resource = "mybatis-config.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + return sqlSessionFactory; + } + + // 查询所有 + @Test + public void testSelectAll() throws IOException { + // 1 加载mybatis的核心配置文件,调用刚刚封装的方法 + SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); + // 2 创建sqlSession对象,用来执行SQL语句 + SqlSession sqlSession = sqlSessionFactory.openSession(); + // 3 执行SQL语句,test是UserMapper.xml文件的namespace,selectAll为id,执行不同的mysql语句 + List list = sqlSession.selectList("test.selectAll"); // namespace.id + System.out.println(list); + // 4 释放资源 + sqlSession.close(); + } + + // 查询一个 + @Test + public void testSelectById() throws IOException { + // 1 加载mybatis的核心配置文件 + SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); + // 2 创建sqlSession对象,用来执行SQL语句 + SqlSession sqlSession = sqlSessionFactory.openSession(); + // 3 执行SQL语句 + User list = sqlSession.selectOne("test.selectById",3); // namespace.id + System.out.println(list); + // 4 释放资源 + sqlSession.close(); + } + + // 增 + @Test + public void insert() throws IOException { + SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); + SqlSession sqlSession = sqlSessionFactory.openSession(); + User user = new User(); + user.setId(1); + user.setUsername("李四"); + user.setAge(18); + user.setGender("男"); + sqlSession.insert("test.insert",user); + // 默认事务是开启的,要手动提交事务 + sqlSession.commit(); // 手动提交事务 + System.out.println("添加成功"); + sqlSession.close(); + } + + // 删 + @Test + public void delete() throws IOException { + SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); + SqlSession sqlSession = sqlSessionFactory.openSession(true); // 自动提交事务 + sqlSession.delete("test.deleteById",1); + System.out.println("删除成功"); + sqlSession.close(); + } + + // 改 + @Test + public void update() throws IOException { + SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); + SqlSession sqlSession = sqlSessionFactory.openSession(true); // 自动提交事务 + User user = new User(9,"李四",18,"男"); + sqlSession.update("test.update",user); + System.out.println("修改成功"); + sqlSession.close(); + } +} +``` + diff --git "a/03 \350\265\226\345\277\203\345\246\215/20231221 Mybatis.md" "b/03 \350\265\226\345\277\203\345\246\215/20231221 Mybatis.md" new file mode 100644 index 0000000000000000000000000000000000000000..0d0c080b6614901346f66441ae3e45082cd0201d --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20231221 Mybatis.md" @@ -0,0 +1,315 @@ +## Mybatis (Mapper代理方式) + +**可做优化:** + +1. 设置别名 + +2. 参数写到专属文件,解除硬编码【写死在源代码中,而源代码有所改动,就要重新编译,测试,打包,整个流程,耦合度太高,好比把电池焊死在手机主板上了,不方便换电池,而且只能用专用品牌的电池,解耦,或解除硬编码编程方式,就好比可以随意换电池的玩具,只认型号,不认品牌,只要是5号电池就可以用。】 + +### 总结 + +使用Mapper代理方式,必须满足以下要求: + +1. 定义与SQL映射文件同名的Mapper接口, +2. 在 Mapper 接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致 +3. 设置SQL映射文件的namespace属性为Mapper接口全限定名 +4. 核心配置文件中的mapper路径要根据sql映射文件的实际路径改变(具体文件或包扫描) +5. 执行sql的方法改成getMapper()的方法 + +### 例题: + +**1.** **创建 brand 表,添加数据** + +~~~ mysql +-- 删除tb_brand表 +drop table if exists tb_brand; +-- 创建tb_brand表 +create table tb_brand( + id int primary key auto_increment, -- id 主键 + brand_name varchar(20), -- 品牌名称 + company_name varchar(20), -- 企业名称 + ordered int, -- 排序字段 + description varchar(100), -- 描述信息 + status int -- 状态:0:禁用 1:启用 +); +-- 添加数据 +insert into tb_brand (brand_name, company_name, ordered, description, status) +values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0), + ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1), + ('小米', '小米科技有限公司', 50, 'are you ok', 1); + +SELECT * FROM tb_brand; +~~~ + +**2.** **创建模块,根据数据表,编写对应的 java 类** + +在 com.mdd.pojo 包下创建 User类 + +~~~ java +package com.mdd.pojo; + +/** + * 品牌 + * + * alt + 鼠标左键:整列编辑 + * + * 在实体类中,基本数据类型建议使用其对应的包装类型 + */ + +public class Brand { + // id 主键 + private Integer id; + // 品牌名称 + private String brandName; + // 企业名称 + private String companyName; + // 排序字段 + private Integer ordered; + // 描述信息 + private String description; + // 状态:0:禁用 1:启用 + private Integer status; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getBrandName() { + return brandName; + } + + public void setBrandName(String brandName) { + this.brandName = brandName; + } + + public String getCompanyName() { + return companyName; + } + + public void setCompanyName(String companyName) { + this.companyName = companyName; + } + + public Integer getOrdered() { + return ordered; + } + + public void setOrdered(Integer ordered) { + this.ordered = ordered; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + @Override + public String toString() { + return "Brand{" + + "id=" + id + + ", brandName='" + brandName + '\'' + + ", companyName='" + companyName + '\'' + + ", ordered=" + ordered + + ", description='" + description + '\'' + + ", status=" + status + + '}'; + } +} +~~~ + +**3.** **导入坐标** + +在创建好的模块中的 pom.xml 配置文件中添加依赖的坐标 + +~~~ xml + + + 4.0.0 + + com.mdd + mybatis_02 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + org.mybatis + mybatis + 3.5.13 + + + + junit + junit + 4.13.2 + test + + + + +~~~ + +**4.** **编写 MyBatis 核心配置文件 - > 替换连接信息** + +在模块下的 resources 目录下创建mybatis的配置文件 mybatis-config.xml ,内容如下: + +~~~ xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +~~~ + +**5. 编写 SQL 映射文件 —> 统一管理 sql 语句,在resources下创建与代理商文件路径相同** + +将sql映射文件(改成与mapper接口同名)移动到该目录,并将namespace属性为Mapper接口完整路径(引用) + +~~~ xml + + + + + + + + + + + + + + + + +~~~ + +**6.定义与 SQL 映射文件同名的 Mapper 接口** + +~~~ java +package com.mdd.mapper; + +import com.mdd.pojo.Brand; + +import java.util.List; + +public interface BrandMapper { + // 方法名就是SQL映射文件中sql语句的id,并保持返回值类型一致 + // 查询所有 + List selectAll(); + // 按照id查询 + Brand selectById(Integer id); + // 查询id大于某个值的 + List selectAllById(Integer id); +} +~~~ + +**7. 编写测试类** + +~~~ java +package com.mdd; + +import com.mdd.mapper.BrandMapper; +import com.mdd.pojo.Brand; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; + +public class TestBrand { + @Test + public void selectAll() throws IOException { + //1. 加载mybatis的核心配置文件,获取 sqlSession 对象 + SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")).openSession(); + //2. 获取UserMapper接口的代理对象,执行SQL语句 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 查询所有 + List brands = mapper.selectAll(); + System.out.println(brands); + + // 指定id查询 + Brand brand = mapper.selectById(1); + System.out.println(brand); + + // 查询id大于某个值的 + List list = mapper.selectAllById(1); + System.out.println(list); + + //3. 释放资源 + sqlSession.close(); + } +} +~~~ \ No newline at end of file diff --git "a/03 \350\265\226\345\277\203\345\246\215/20231222 \345\242\236\345\210\240\346\224\271\346\237\245.md" "b/03 \350\265\226\345\277\203\345\246\215/20231222 \345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..6392af13c604b186e0cded010610d0c8ec71ad46 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20231222 \345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,508 @@ +# 笔记 + +1.导入maven环境 + 2.创建dao接口,里面定了你需要对数据库执行什么操作 + 3.编写mapper文件,sql映射文件,写上你的sql语句 + 4.创建mybatis主配置文件,读取配置文件中的内容,连接到数据库 + 5.使用mybatis对象执行sql语句:sqlSession对象 + 6.关闭连接:sqlSession.close(); + + +# 作业 + +数据库表 + +```java +CREATE TABLE `t_user` ( + `id` int NOT NULL AUTO_INCREMENT, + `username` varchar(255) DEFAULT NULL, + `password` varchar(255) DEFAULT NULL, + `email` varchar(255) DEFAULT NULL, + `phone` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + +INSERT INTO `t_user`(`id`, `username`, `password`, `email`, `phone`) VALUES (1, 'admin', 'admin', 'admin@qq.com', '1008611'); +INSERT INTO `t_user`(`id`, `username`, `password`, `email`, `phone`) VALUES (2, 'root', 'root', 'root@qq.com', '1008622'); +INSERT INTO `t_user`(`id`, `username`, `password`, `email`, `phone`) VALUES (19, 'admin2', 'root2', 'root@qq.com2', '1008644'); + + +``` + +创建实体类 + +```java +public class User { + private Integer id; + private String username; + private String password; + private String email; + private String phone; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public User() { + } + + public User(Integer id, String username, String password, String email, String phone) { + this.id = id; + this.username = username; + this.password = password; + this.email = email; + this.phone = phone; + } + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + ", email='" + email + '\'' + + ", phone='" + phone + '\'' + + '}'; + } + +} + + +``` + +创建maven项目 + +```java + + + 4.0.0 + + org.example + mybatis + 1.0-SNAPSHOT + + + UTF-8 + 1.9 + 1.9 + + + + + + junit + junit + 4.11 + test + + + + mysql + mysql-connector-java + 8.0.19 + + + + org.mybatis + mybatis + 3.5.4 + + + + org.junit.jupiter + junit-jupiter + RELEASE + compile + + + + + + + + + + + src/main/java + + **/*.properties + **/*.xml + + false + + + + + + +``` + +配置mybatis的主配置文件 + +```java + + + + + + + +--> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +编写mybatis工具类 + +```java +public class MyBatisUtils { + + private static SqlSessionFactory factory = null; + + static { + //读取mybatis.xml配置文件 + String config="mybatis.xml"; + try { + //将配置文件加入到流中 + InputStream in = Resources.getResourceAsStream(config); + //创建factory对象 + factory = new SqlSessionFactoryBuilder().build(in); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + public static SqlSession getSqlSession(){ + SqlSession sqlSession = null; + if (factory!=null){ + //如果 factory!=null 就创建 sqlSession对象 + sqlSession = factory.openSession(false);//非自动提交事务 + } + + return sqlSession; + } +} + +``` + +定义接口 + +```java +public interface UserManagerDao { + + /** + * 查询所有的数据 用于视图展示 + * @return 返回一个List集合 + */ + public List queryUser(); + + + /** + * 添加 + * @param user 一个user对象 + * @return 成功返回true 失败返回false + */ + public Boolean addUser(User user); + + /** + * 删除用户 + * @param id 需要删除的用户id + * @return 成功返回true 失败返回false + */ + public Boolean delUser(String id); + + /** + * 修改用户信息 + * @param user 需要一个user对象 + * @return 成功返回true 失败返回false + */ + public Boolean updateUser(User user); + + /** + * 按照用户名查找用户 + * @param name 需要查找的用户名 + * @return 返回一个List集合 + */ + public List likeUser(String name); + + + +} + +``` + +xml文件 + +```java + + + + + + + + + + insert into t_user values(#{id},#{username},#{username},#{email},#{phone}) + + + + delete from t_user where id=#{id} + + + + update t_user set username=#{username},password=#{password},email=#{email},phone=#{phone} where id=#{id} + + + + +``` + +编写daoImpl + +```java + +public class UserManagerDaoImpl implements UserManagerDao { + + /** + * 查询所有的数据 用于视图展示 + * + * @return 返回一个List集合 + */ + @Override + public List queryUser() { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.queryUser"; + //执行sql语句 + List userList = sqlSession.selectList(sqlId); + // sqlSession.commit(); //mybatis默认不会手动提交事务,在修改数据之后要 提交事务 + //关闭连接 + sqlSession.close(); + return userList; + } + + + /** + * 添加 + * + * @param user 一个user对象 + * @return 成功返回true 失败返回false + */ + @Override + public Boolean addUser(User user) { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.addUser"; + //执行sql语句 + int updateCount = sqlSession.update(sqlId,user); + sqlSession.commit(); //mybatis默认不会手动提交事务,在修改数据之后要 提交事务 + //关闭连接 + sqlSession.close(); + if(updateCount>0){ + return true; + }else{ + return false; + } + + } + + /** + * 删除用户 + * + * @param id 需要删除的用户id + * @return 成功返回true 失败返回false + */ + @Override + public Boolean delUser(String id) { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.delUser"; + //执行sql语句 + int delete = sqlSession.delete(sqlId, id); + sqlSession.commit(); //设置为自动提交事务 + //关闭连接 + sqlSession.close(); + if(delete>0){ + return true; + }else { + return false; + } + + } + + /** + * 修改用户信息 + * + * @param user 需要一个user对象 + * @return 成功返回true 失败返回false + */ + @Override + public Boolean updateUser(User user) { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.updateUser"; + //执行sql语句 + int updateCount = sqlSession.update(sqlId,user); + sqlSession.commit(); //设置为自动提交事务 + //关闭连接 + sqlSession.close(); + if(updateCount>0){ + return true; + }else { + return false; + } + + } + + /** + * 按照用户名查找用户 + * + * @param name 需要查找的用户名 + * @return 返回一个List集合 + */ + @Override + public List likeUser(String name) { + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.likeUser"; + List userList = sqlSession.selectList(sqlId, name); + sqlSession.commit(); + //关闭连接 + sqlSession.close(); + return userList; + } +} + +``` + +Test类 + +```java +class UserManagerDaoImplTest { + + UserManagerDaoImpl userManagerDao = new UserManagerDaoImpl(); + + @Test + void queryUser() { + List userList = userManagerDao.queryUser(); + for (User user : userList) { + System.out.println(user); + } + } + @Test + void ListUser() { + + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + UserManagerDao userDao = sqlSession.getMapper(UserManagerDao.class); + List userList = userDao.queryUser(); + for (User user : userList) { + System.out.println(user); + } + } + + @Test + void addUser() { + User user = new User(null,"admin","root","root@qq.com","1008644"); + Boolean flag = userManagerDao.addUser(user); + System.out.println(flag); + + } + + @Test + void delUser() { + Boolean flag = userManagerDao.delUser("23"); + System.out.println(flag); + } + + @Test + void updateUser() { + User user = new User(19,"admin2","root2","root@qq.com2","1008644"); + Boolean flag = userManagerDao.updateUser(user); + System.out.println(flag); + } + + @Test + void likeUser() { + List userList = userManagerDao.likeUser("admin"); + for (User user : userList) { + System.out.println(user); + } + } +} + +``` \ No newline at end of file diff --git "a/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md" "b/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md" new file mode 100644 index 0000000000000000000000000000000000000000..becc9ad839769b41dcef4e12b2cc925ea9f816e4 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md" @@ -0,0 +1,162 @@ +## Ioc和DI + +```html +IoC(Inversion of Control)控制反转 +使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转 +Spring技术对IoC思想进行了实现 +Spring提供了一个容器,称为IoC容器,用来充当IoC思想中的“外部” +IoC容器负责对象的创建、初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean +DI(Dependency Injection)依赖注入 +在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入 +``` + +bean生命周期: + +```java +初始化容器 +创建对象(内存分配) +执行构造方法 +执行属性注入(set操作) +执行bean初始化方法 +使用bean +执行业务操作 +关闭/销毁容器 +执行bean销毁方法 +``` + +bean销毁时机 + +```java +容器关闭前触发bean的销毁 +关闭容器方式: +手工关闭容器 +ConfigurableApplicationContext接口close()操作 +注册关闭钩子,在虚拟机退出前先关闭容器再退出虚拟机 + ConfigurableApplicationContext接口registerShutdownHook()操作 + +public class AppForLifeCycle { public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ctx.close(); }} + +``` + +pom.xml + +```xml + + + + 4.0.0 + + com.mdd + spring_01_quickstart + 1.0-SNAPSHOT + + + + junit + junit + 4.13.2 + test + + + org.springframework + spring-context + 5.2.25.RELEASE + + + + + + +``` + +```xml +src/main/resources/ApplicationContext.xml +ApplicationContext.xml + + + + + + + +``` + +```java +文件名:BookDaoImpl.java + +package com.mdd.dao.impl; + +import com.mdd.dao.BookDao; + +public class BookDaoImpl implements BookDao { + public void save() { + System.out.println("book dao save ..."); + } +} +``` + +```java +文件名: BookServiceImpl.java + +package com.mdd.service.impl; +import com.mdd.dao.BookDao; +import com.mdd.dao.impl.BookDaoImpl; +import com.mdd.service.BookService; + +public class BookServiceImpl implements BookService { + + private BookDao bookDao; + + public void setBookDao(BookDao bookDao) { + this.bookDao = bookDao; + } + + public void save() { + System.out.println("book service save ..."); + bookDao.save(); + } + +} +``` + +```java +文件名:BookDao.java +package com.mdd.dao; +public interface BookDao { + public void save(); +} +``` + +```java +文件名:App.java +package com.mdd; +import com.mdd.service.BookService; +import com.mdd.service.impl.BookServiceImpl; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(String[] args) { +// BookService bookService = new BookServiceImpl(); +// bookService.save(); + //1.通过配置文件获取IOC容器 + ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml"); + //2.通过IOC容器的getBean 方法,获取对象 + BookServiceImpl bookService = (BookServiceImpl) ctx.getBean("bookService"); + bookService.save(); + + } +} +``` + +```java +文件名:BookService.java +package com.mdd.service; + +public interface BookService { + public void save(); +} +``` \ No newline at end of file