diff --git "a/09 \346\233\271\346\255\243\346\263\242/20231227 Spring.md" "b/09 \346\233\271\346\255\243\346\263\242/20231227 Spring.md" new file mode 100644 index 0000000000000000000000000000000000000000..abe6985e294915ba5f214ba461f4e7fa14671c8c --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20231227 Spring.md" @@ -0,0 +1,167 @@ +### 创建容器的两种方式 + +- ClassPathXmlApplicationContext + +- FileSystemXmlApplicationContext + +### Spring提供 @Component 注解的三个衍生注解(定义bean) + +- @Controller:用于表现层bean定义 + +- @Service:用于业务层bean定义 + +- @Repository:用于数据层bean定义 + +### 纯注解开发 + +- @Configuration + +- @ComponentScan + +- AnnotationConfigApplicationContext + + + +### 自动装配 + +#### 使用@Autowired注解开启自动装配模式(按类型) + +~~~ java +@Service +public class BookServiceImpl implements BookService { + @Autowired + private BookDao bookDao; + public void setBookDao(BookDao bookDao) { + this.bookDao = bookDao; + } + public void save() { + System.out.println("book service save ..."); + bookDao.save(); + } +} +~~~ + +注意: + +1. 自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法 + +2. 自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法 + +#### 使用@Qualifier注解开启指定名称装配bean + +~~~ java +@Service +public class BookServiceImpl implements BookService { + @Autowired + @Qualifier("bookDao") + private BookDao bookDao; +} +~~~ + +注意:@Qualifier注解无法单独使用,必须配合@Autowired注解使用 + +#### 使用@Value实现简单类型注入 + +~~~ java +@Repository("bookDao") +public class BookDaoImpl implements BookDao { + @Value("100") + private String connectionNum; +} +~~~ + +#### 使用@PropertySource注解加载properties文件 + +~~~ java +@Configuration +@ComponentScan("com.mdd") +@PropertySource("classpath:jdbc.properties") +public class SpringConfig { +} +~~~ + +注意:路径仅支持单一文件配置,多文件请使用数组格式配置,不允许使用通配符 + + + +### 第三方bean管理 + +#### 使用@Bean配置第三方bean + +~~~ java +@Configuration +public class SpringConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + ds.setDriverClassName("com.mysql.jdbc.Driver"); + ds.setUrl("jdbc:mysql://localhost:3306/spring_db"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} +~~~ + +#### 使用独立的配置类管理第三方bean + +~~~ java +public class JdbcConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + ds.setDriverClassName("com.mysql.jdbc.Driver"); + ds.setUrl("jdbc:mysql://localhost:3306/spring_db"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} +~~~ + +#### 将独立的配置类加入核心配置 + +**方式一:导入式** + +~~~ java +public class JdbcConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + //相关配置 + return ds; + } +} +~~~ + +使用@Import注解手动加入配置类到核心配置,此注解只能添加一次,多个数据请用数组格式 + +~~~ java +@Configuration +@Import(JdbcConfig.class) +public class SpringConfig { +} +~~~ + +**方式二:扫描式** + +~~~ java +@Configuration +public class JdbcConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + //相关配置 + return ds; + } +} +~~~ + +使用@ComponentScan注解扫描配置类所在的包,加载对应的配置类信息 + +~~~ java +@Configuration +@ComponentScan({"com.mdd.config","com.mdd.service","com.mdd.dao"}) +public class SpringConfig { +} +~~~ \ No newline at end of file diff --git "a/09 \346\233\271\346\255\243\346\263\242/20231228 Spring.md" "b/09 \346\233\271\346\255\243\346\263\242/20231228 Spring.md" new file mode 100644 index 0000000000000000000000000000000000000000..c9b0192d35b4082329ad82713aed813de3de7c33 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20231228 Spring.md" @@ -0,0 +1,318 @@ +1. 打开IDEA,新建一个项目(模块),开启MySQL + +2. 编写Pom.mxl文件,导入相关依赖的坐标 + +~~~ xml + + + 4.0.0 + + com.mdd + spring_02 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.springframework + spring-context + 5.1.10.RELEASE + + + + org.springframework + spring-jdbc + 5.1.10.RELEASE + + + org.springframework + spring-test + 5.1.10.RELEASE + + + + org.mybatis + mybatis + 3.5.13 + + + + org.mybatis + mybatis-spring + 2.0.5 + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + junit + junit + 4.13.2 + test + + + + com.alibaba + druid + 1.1.23 + + + +~~~ + +3. 创建数据库的相关表,列,并导入数据 +4. 在src下的 main下的 java里新建包com.mdd.domain,并在 domain下创建实体类 Brand + +~~~ java +public class Brand { + // id 主键 + private Integer id; + // 品牌名称 + private String brandName; + // 企业名称 + private String companyName; + // 排序字段 + private Integer ordered; + // 描述信息 + private String description; + // 状态:0:禁用 1:启用 + private Integer status; + // 无参、全参、get,set方法、tostring方法 +~~~ + +5. 编写代理接口 com.mdd.mapper.BrandMapper.java ,用来从数据库从获取数据 + +~~~ java +public interface BrandMapper { + // 给不符字段添加别名(解决数据为null的问题) + @Select("select id, brand_name brandName, company_name companyName, ordered, description, status from tb_brand") + // 查询所有数据 + List findAll(); + // 根据相应条件查询,动态SQL + List selectByCondition(Brand brand); +} +~~~ + +6. 在 com.mdd.config 目录下,编写Spring核心配置类文件SpringConfig.java,写上@Configuration注解,将这个类变成配置类 + +~~~ java +@Configuration +// 引入 JdbcConfig,MybatisConfig 配置文件 +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +~~~ + +7. 在 com.mdd.config 目录下,编写 JdbcConfig.java也就是mysql驱动配置类文件 + +~~~ java +public class JdbcConfig { + // jdbc负责连接mysql,配置驱动,url,用户名,密码 + // spring如何加载第三方的Bean,通过写一个方法,返回这个bea 的类弄。并上这个方法上写一个注解@Bean + @Bean + public DataSource dataSource(){ + // 此时 DataSource 为实现类,需通过 druid 代理 dataSource + DruidDataSource ds = new DruidDataSource(); + // 配置连接四要素:驱动、url、密码、用户名 + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql://localhost:3306/mybatis?useSSl=false"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} +~~~ + +8. 在 com.mdd.config 目录下,编写MybatisConfig.java配置类文件 + +~~~ java +public class MybatisConfig { + // 这里做两件事,1.获取SqlSessionFactoryBean 2.获取Mapper代理的包 + //1.获取SqlSessionFactoryBean + @Bean + public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); + // ssfb 也要做两件事:1.设置实体类的别名 2.设置数据源 + ssfb.setTypeAliasesPackage("com.mdd.domain"); // 实体类所在包的位置 + ssfb.setDataSource(dataSource); + return ssfb; + } + @Bean + //2.获取Mapper代理的包 + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); // 代理类包的位置 + return msc; + } +} +~~~ + +9. 测试代理的功能 + +~~~ java +public class App { + public static void main(String[] args) { + // 1.通过 spring核心配置文件 获取 IOC容器 + ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); + // 2.获取代理接口的对象 + BrandMapper brandMapper = ctx.getBean(BrandMapper.class); + List all = brandMapper.findAll(); + System.out.println(all); + } +} +~~~ + +10. 遇上复杂的sql,及要使用动态SQL时就要配置对应的XML配置文件在resources下根据代理接口com/mdd/mapper的路径,创建一个**同包同名**的xml文件 com/mdd/mapper/BrandMapper.xml + +~~~ xml + + + + + + + + + + +~~~ + +11. 在Test中测试代理的方法 + +~~~ java +@RunWith(SpringJUnit4ClassRunner.class) // 加载spring为Junit专门提供的运行类 +@ContextConfiguration(classes = SpringConfig.class) // 加载Spring核心配置文件,IOC容器 +public class BrandTest { + //通过自动装配的形式,直接将 bean 注入进来 + @Autowired + private BrandMapper brandMapper; + @Test + public void test01(){ +// List all = brandMapper.findAll(); +// System.out.println(all); + + // 1. 模拟浏览器向服务器发送了查询的数据 + String brandName = "华为"; + String companyName = "华为"; + // 2. 因为是模糊查询,参数要加 % + brandName = "%"+brandName+"%"; + companyName = "%"+companyName+"%"; + // 3. 将数据分装成一个对象 + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + // 4. 调用代理对象接口中的方法 + List brands = brandMapper.selectByCondition(brand); + System.out.println(brands); + } +} +~~~ + +12. 模拟业务层,先有一个接口,再写一个实现类 + +com.mdd.service.BrandService.java 接口 + +~~~ java +public interface BrandService { + List findAll(); +} +~~~ + +com.mdd.service.impl.BrandServiceImpl.java 实现类 + +~~~ java +@Service +public class BrandServiceImpl implements BrandService { + // 业务层要实现数据的调用,就要借助数据层的对象 + // 将数据层的对象,自动装配进来 + @Autowired + private BrandMapper brandMapper; + @Override + public List findAll() { + System.out.println("业务层的命运齿轮开始运转。。。。"); + // 返回数据层获得的结果 + return brandMapper.findAll(); + } +} +~~~ + +13. 测试类,模拟表现层调用业务层 + +~~~ java +@RunWith(SpringJUnit4ClassRunner.class) // 加载spring为Junit专门提供的运行类 +@ContextConfiguration(classes = SpringConfig.class) // 加载Spring核心配置文件,IOC容器 +public class BrandTest { + //通过自动装配的形式,直接将 bean 注入进来 + @Autowired + private BrandMapper brandMapper; + @Autowired + private BrandService brandService; // 要什么就自动装配什么,装配的是接口,面向接口编程 + @Test + public void test01(){ +// List all = brandMapper.findAll(); +// System.out.println(all); + // 1. 模拟浏览器向服务器发送了查询的数据 + String brandName = "华为"; + String companyName = "华为"; + // 2. 因为是模糊查询,参数要加 % + brandName = "%"+brandName+"%"; + companyName = "%"+companyName+"%"; + // 3. 将数据分装成一个对象 + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + // 4. 调用代理对象接口中的方法 + List brands = brandMapper.selectByCondition(brand); + System.out.println(brands); + } + + // 模拟表现层调用业务层 + @Test + public void test02(){ + List all = brandService.findAll(); + System.out.println(all); + } +} +~~~ + +14. SptingConfig 类 扫包 + +~~~ java +@Configuration +@Import({JdbcConfig.class, MybatisConfig.class}) +@ComponentScan("com.mdd") +public class SpringConfig { +} +~~~ + +注意:不要漏掉以下操作: + +1. 将实现类用注解@Service,标记为一个Bean + +2. 修改SpringConfig.java,将业务层的包,用 @CompenentScan("com.mdd") ,扫描进来 \ No newline at end of file diff --git "a/09 \346\233\271\346\255\243\346\263\242/20231229 Spring\345\244\215\344\271\240.md" "b/09 \346\233\271\346\255\243\346\263\242/20231229 Spring\345\244\215\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..b77fc70778484b6ded31bac47c8b02f966b6dc18 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20231229 Spring\345\244\215\344\271\240.md" @@ -0,0 +1,266 @@ +# 复习 + +### JdbcConfig + +```java +package com.mdd.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class JdbcConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///masql"); + ds.setUsername("root"); + ds.setPassword("123456"); + return ds; + } +} + +``` + +### MybatisConfig + +```java +package com.mdd.config; + +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.mapper.MapperScannerConfigurer; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class MybatisConfig { + @Bean + public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfy = new SqlSessionFactoryBean(); + ssfy.setTypeAliasesPackage("com.mdd.domain"); + ssfy.setDataSource(dataSource); + return ssfy; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer mc = new MapperScannerConfigurer(); + mc.setBasePackage("com.mdd.mapper"); + return mc; + } +} + +``` + +### SpringConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@ComponentScan("com.mdd") +@Import({JdbcConfig.class,MybatisConfig.class}) +public class SpringConfig { + +} + +``` + +### User + +```java +package com.mdd.domain; + +public class User { + private Integer id; + private String name; + private String sex; + private Integer age; + + public User() { + } + + public User(Integer id, String name, String sex, Integer age) { + this.id = id; + this.name = name; + this.sex = sex; + 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 String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + '}'; + } +} + +``` + +### UserMapper + +```java +package com.mdd.mapper; + +import com.mdd.domain.User; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import java.util.List; + +public interface UserMapper { + @Select("select * from tb_ures") + List findAll(); + + @Select("select * from tb_ures where id=#{id}") + User findByid(int id); + + @Delete("delete from tb_ures where id=#{id}") + void deleteByid(int id); + + @Update("Updata tb_ures set name=#{name},sex=#{sex},age=#{age} where id=#{id}") + void updetaByid(User user); + + @Insert("insert into tb_ures(name,sex,age) values(#{name},#{sex},#{age})") + void insertByid(User user); +} + +``` + +### App 测试类 + +```java +import com.mdd.config.SpringConfig; +import com.mdd.domain.User; +import com.mdd.mapper.UserMapper; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import java.util.List; + +public class App { + public static void main(String[] args) { + ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class); + UserMapper bean = ctx.getBean(UserMapper.class); + List all = bean.findAll(); + System.out.println(all); + } +} + +``` + +### 架包 + +```xml + + + 4.0.0 + + com.mdd + spring_10_review + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.springframework + spring-context + 5.2.25.RELEASE + + + + org.springframework + spring-jdbc + 5.2.25.RELEASE + + + org.springframework + spring-test + 5.2.25.RELEASE + + + + org.mybatis + mybatis + 3.5.13 + + + + org.mybatis + mybatis-spring + 2.0.5 + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + junit + junit + 4.13.2 + test + + + + com.alibaba + druid + 1.1.23 + + + + + +``` +