diff --git "a/50 \345\274\240\350\265\267\347\221\236/20240102 springmvc.md" "b/50 \345\274\240\350\265\267\347\221\236/20240102 springmvc.md" new file mode 100644 index 0000000000000000000000000000000000000000..1a2fbd9bfc74fd2a5835976cf443d6a04377b8c2 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20240102 springmvc.md" @@ -0,0 +1,165 @@ + + + + +```java +package com.mdd.controller; + +import com.mdd.domain.User; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.Arrays; +import java.util.List; + +@Controller +@RequestMapping("/class2") +@ResponseBody +public class UserController { + + @RequestMapping(value = "/student") +// @RequestParam 请求参数的注解,解决请求的参数与方法形参不一致的问题 + public String commonParam(@RequestParam("username") String name, Integer age){ + System.out.println("开始接收参数:==》"+name+age); + return "Your name is "+name+" and your age is "+age; + } + + @RequestMapping("/user") + public String userParam(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("address") String address){ + System.out.println("接收对象类型的参数:==》"+"User{username="+username+", password="+password+", address="+address+"}"); + return "User{username="+username+", password="+password+", address="+address+"}"; + } + + + @RequestMapping("/hobby") + public String arrayParam(@RequestParam List hobby){ + System.out.println("接收对象类型的参数:==》"+ hobby); + return hobby.toString(); + } + +} + +``` + +```java +package com.mdd.config; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; +import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; + +import javax.servlet.Filter; + +public class WebInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { +// Root 加载Spring的核心配置文件 + return new Class[0]; + } + + @Override + protected Class[] getServletConfigClasses() { +// Servlet 加载SpringMVC的核心配置文件 + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { +// 设置从/开始的路径,都归tomcat管理 + return new String[]{"/"}; + } + + @Override + protected Filter[] getServletFilters() { + CharacterEncodingFilter filter = new CharacterEncodingFilter(); + filter.setEncoding("utf-8"); + return new Filter[]{filter}; + } +} + +``` + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.mdd.controller") +public class SpringMvcConfig { +} + +``` + +```java + + + + Archetype Created Web Application + + +``` + +```java + + +

Hello World!

+ + + +``` + +```java + + 4.0.0 + com.mdd + springmvc_01 + war + 1.0-SNAPSHOT + + + + + org.springframework + spring-webmvc + 5.2.25.RELEASE + + + javax.servlet + javax.servlet-api + 3.1.0 + + provided + + + + + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + 80 + / + + + + + + + + +``` + diff --git "a/50 \345\274\240\350\265\267\347\221\236/20240104 Spring04.md" "b/50 \345\274\240\350\265\267\347\221\236/20240104 Spring04.md" new file mode 100644 index 0000000000000000000000000000000000000000..d3c43e7c27e0a473f6c06802b1b2f78e0b43a37f --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20240104 Spring04.md" @@ -0,0 +1,548 @@ + + + + +```java + +pom.xml + + + 4.0.0 + com.mdd + springmvc_04 + war + 1.0-SNAPSHOT + + + + + org.springframework + spring-webmvc + 5.2.25.RELEASE + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + com.fasterxml.jackson.core + jackson-databind + 2.11.3 + + + + + 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 + + + + + + + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + 80 + / + + utf-8 + + + + + + +src/main/java/com/mdd/config/SpringMvcConfig.java + +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.mdd.controller") +@EnableWebMvc +public class SpringMvcConfig { +} +src/main/java/com/mdd/config/WebConfig.java + +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + @Override + protected Class[] getRootConfigClasses() { +// SpringConfig.class + return new Class[]{SpringConfig.class}; // SpringConfig.class + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} +src/main/java/com/mdd/domain/Brand.java + +package com.mdd.domain; + +public class Brand { + private Integer id; + private String brandName; + private String companyName; + private Integer ordered; + private String description; + private Integer status; + + + // 提供getter/setter方法,有参,无参构造器,toString,也就是标准的JavaBean ,快捷方式ALT+insert + + public Brand() { + } + + public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, + Integer status) { + this.id = id; + this.brandName = brandName; + this.companyName = companyName; + this.ordered = ordered; + this.description = description; + this.status = 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 + + '}'; + } +} +src/main/java/com/mdd/domain/R.java + +package com.mdd.domain; + +public class R { + private Integer code; + private String msg; + private Object date; + + public R() { + } + + public static R success(Integer code,Object data){ + return new R(code,data); + } + + public static R success(Integer code,String msg){ + return new R(code,msg); + } + + + + public R(Integer code, Object date) { + this.code = code; + this.date = date; + } + + public R(Integer code, String msg, Object date) { + this.code = code; + this.msg = msg; + this.date = date; + + } + + public R(Integer code, String msg) { + this.code = code; + this.msg = msg; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Object getDate() { + return date; + } + + public void setDate(Object date) { + this.date = date; + } + + @Override + public String toString() { + return "R{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", date=" + date + + '}'; + } +} +src/main/java/com/mdd/controller/BrandController.java + +package com.mdd.controller; + +import com.mdd.domain.Brand; +import com.mdd.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +//@Controller +//@ResponseBody +@RestController +@RequestMapping( "/brands") +public class BrandController { + // 表现层要调用业务层 + @Autowired + private BrandService brandService; + // 添加 + @PostMapping + public String addBrand(@RequestBody Brand brand){ + System.out.println("接收到一个Brand==》"+brand); + brandService.addBrand(brand); + return "add ok!"; + } + + + // 修改 + @PutMapping + public String editBrand(@RequestBody Brand brand){ + brandService.editBrand(brand); + return "add ok!"; + } + + // 查所有 + + @GetMapping + public List getBrands(){ + List brands = brandService.selectAll(); + return brands; + } + + // 查单个 + @GetMapping("/{id}") + public Brand getBrand(@PathVariable Integer id){ + System.out.println("接收到一个id==》"+id); + System.out.println("从数据库从查询到了。id="+id+"的品牌"); + + return new Brand(id,"华为10086","大华为",100,"科技公司",1); + } + + // 删除 + @DeleteMapping("/{id}") + public String delBrand(@PathVariable Integer id){ + System.out.println("接收到一个id==》"+id); + System.out.println("从数据库从删除了。id="+id+"的品牌"); + return "delete ok!"; + } +} +src/main/java/com/mdd/config/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.service", "com.mdd.mapper"}) +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +src/main/java/com/mdd/config/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:///mybatis"); + ds.setUsername("root"); + ds.setPassword("root"); //123456 + return ds; + } +} +src/main/java/com/mdd/config/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 sqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); + // 设置实体类的包 + ssfb.setTypeAliasesPackage("com.mdd.domain"); + // 设置数据源dataSource + ssfb.setDataSource(dataSource); + return ssfb; + } + + // 设置Mapper + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + // 设置代理的包 + msc.setBasePackage("com.mdd.mapper"); + return msc; + } +} +src/main/java/com/mdd/mapper/BrandMapper.java + +package com.mdd.service.impl; + +import com.mdd.domain.Brand; +import com.mdd.mapper.BrandMapper; +import com.mdd.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BrandServiceImpl implements BrandService { + // 业务层要调用数据层的对象,所以先自动装配BrandMapper + @Autowired + private BrandMapper brandMapper; + + + @Override + public List selectAll() { + System.out.println("业务层开始:查询所有"); + return brandMapper.selectAll(); + } + + @Override + public Brand selectById(int id) { + System.out.println("业务层开始:查某个"); + + return brandMapper.selectById(id); + } + + @Override + public void deleteById(int id) { + brandMapper.deleteById(id); + } + + @Override + public void addBrand(Brand brand) { + brandMapper.addBrand(brand); + } + + @Override + public void editBrand(Brand brand) { + brandMapper.editBrand(brand); + } +} +src/main/java/com/mdd/service/impl/BrandServiceImpl.java + +package com.mdd.service.impl; + +import com.mdd.domain.Brand; +import com.mdd.mapper.BrandMapper; +import com.mdd.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BrandServiceImpl implements BrandService { + // 业务层要调用数据层的对象,所以先自动装配BrandMapper + @Autowired + private BrandMapper brandMapper; + + + @Override + public List selectAll() { + System.out.println("业务层开始:查询所有"); + return brandMapper.selectAll(); + } + + @Override + public Brand selectById(int id) { + System.out.println("业务层开始:查某个"); + + return brandMapper.selectById(id); + } + + @Override + public void deleteById(int id) { + brandMapper.deleteById(id); + } + + @Override + public void addBrand(Brand brand) { + brandMapper.addBrand(brand); + } + + @Override + public void editBrand(Brand brand) { + brandMapper.editBrand(brand); + } +} +src/main/java/com/mdd/service/BrandService.java(接口) + +package com.mdd.service; + +import com.mdd.domain.Brand; + +import java.util.List; + +public interface BrandService { + // 查所有 + + List selectAll(); + + // id查某个 + + Brand selectById(int id); + + // id 删除某个 + void deleteById(int id); + + // 添加 + + void addBrand(Brand brand); + + + // 修改 + + void editBrand(Brand brand); +} +``` +