diff --git "a/49 \346\235\216\350\210\222\346\261\266/202312.27\345\210\233\345\273\272\345\256\271\345\231\250.md" "b/49 \346\235\216\350\210\222\346\261\266/202312.27\345\210\233\345\273\272\345\256\271\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..dd504c4b365536cb1393e0c473f7c9b2a56340a3 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/202312.27\345\210\233\345\273\272\345\256\271\345\231\250.md" @@ -0,0 +1,53 @@ +创建容器 + +加载多个配置文件 + +```java +ApplicationContext ctx = new ClassPathXmlApplicationContext("bean1.xml", "bean2.xml"); +``` + + + +Spring提供@Component注解的三个衍生注解 + +@Controller:用于表现层bean定义 + +@Service:用于业务层bean定义 + +@Repository:用于数据层bean定义 + +```java +@Repository("bookDao")public class BookDaoImpl implements BookDao {} +@Servicepublic class BookServiceImpl implements BookService {} + +``` + +Spring3.0开启了纯注解开发模式,使用Java类替代配置文件,开启了Spring快速开发赛道Java类代替Spring核心配置文件, + +@Configuration注解用于设定当前类为配置类@ComponentScan注解用于设定扫描路径,此注解只能添加一次,多个数据请用数组格式 + +```java +@ComponentScan({com.mdd.service","com.mdd.dao"}) + +``` + +```java +//加载配置文件初始化容器 +ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); +//加载配置类初始化容器 +ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); + +``` + +依赖注入 + +使用@Autowired注解开启自动装配模式(按类型) + +```java +@Servicepublic 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(); }} + +``` + +注意:自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法 + +注意:自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法 diff --git "a/49 \346\235\216\350\210\222\346\261\266/202312.29\346\225\264\345\220\210Spring.md" "b/49 \346\235\216\350\210\222\346\261\266/202312.29\346\225\264\345\220\210Spring.md" new file mode 100644 index 0000000000000000000000000000000000000000..7bf451e7741674f61acdc89d4a6670f3d82e4b87 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/202312.29\346\225\264\345\220\210Spring.md" @@ -0,0 +1,265 @@ +# 复习 + +### 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 + + + + + +``` \ No newline at end of file diff --git "a/49 \346\235\216\350\210\222\346\261\266/20240102\347\254\224\350\256\260.md" "b/49 \346\235\216\350\210\222\346\261\266/20240102\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..3a7b259308373309a02615a03193ba64e010bafd --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/20240102\347\254\224\350\256\260.md" @@ -0,0 +1,182 @@ +## 笔记 + +名称:@ComponentScan + +类型:类注解 + +属性 + +excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)与具体项(classes) + +includeFilters:加载指定的bean,需要指定类别(type)与具体项(classes) + +名称:@RequestParam + +类型:形参注解 + +位置:SpringMVC控制器方法形参定义前面 + +作用:绑定请求参数与处理器方法形参间的关系 + +参数: + +required:是否为必传参数 + +defaultValue:参数默认值 + +名称:@EnableWebMvc + +类型:配置类注解 + +位置:SpringMVC配置类定义上方 + +作用:开启SpringMVC多项辅助功能 + +## 作业 + +步骤; + +1.pom.xml,将没有的东西复原,如java,test。。。 + +```xml + + 4.0.0 + com.xlu + ssmvc + war + 1.0-SNAPSHOT + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + org.springframework + spring-webmvc + 5.2.25.RELEASE + + + + + + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + 88 + / + + + + + + + +``` + +2.创建com.xlu.config.SpringMvcConfig + +```java +package com.xlu.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.xlu") +public class SpringMvcConfig { +} +``` + +3.com.xlu.config.WeblnitConfig + +```java +package com.xlu.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WeblnitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + + + protected Class[] getRootConfigClasses() { + return new Class[0];//加载spring的核心配置文件 + } + + protected Class[] getServletConfigClasses() { + //加载springmvc的核心配置文件 + return new Class[]{SpringMvcConfig.class}; + } + + protected String[] getServletMappings() { + //设置从/开始的路径,都归tomcat管理 + return new String[]{"/"}; + } +} +``` + +4.com.xlu.controller.UserController + +```java +package com.xlu.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.xml.ws.RequestWrapper; +import java.security.PublicKey; + +@Controller +public class UserController { + + + @RequestMapping("/save") + @ResponseBody + public String userSave(){ + + return "ok";//http://localhost:88/save网址,查看是否出现OK + + } + + @RequestMapping("/book") + @ResponseBody + public String book(){ + return "book is ok!";//http://localhost:88/book网址,查看是否有东西 + } + + + @RequestMapping("/hello") + @ResponseBody + public String hello(){ + + System.out.println(666); + return "666";//http://localhost:88/hello网址,查看是否有东西 + + } + +// @RequestMapping("/shouYe") +// public String shouYe(){ +// +// return "index.jsp";//http://localhost:88/shouYe网址,跳转网页出现 helloword +// } + + @RequestMapping("/shouYe") + @ResponseBody + public String shouYe(){ + + return "index.jsp";//http://localhost:88/shouYe网址,出现index.jsp + } + + +} +```