From 49f331ab122db755bfd0cce9fb42371215ab7b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=95=8F?= <3234934487@qq.com> Date: Wed, 10 Jan 2024 12:15:42 +0800 Subject: [PATCH] zy --- ...vc\345\256\214\346\225\264\347\211\210.md" | 379 ++++++++++++++++++ 1 file changed, 379 insertions(+) create mode 100644 "56 \350\265\265\346\225\217/20240104 SpringMvc\345\256\214\346\225\264\347\211\210.md" diff --git "a/56 \350\265\265\346\225\217/20240104 SpringMvc\345\256\214\346\225\264\347\211\210.md" "b/56 \350\265\265\346\225\217/20240104 SpringMvc\345\256\214\346\225\264\347\211\210.md" new file mode 100644 index 0000000..1b21a47 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20240104 SpringMvc\345\256\214\346\225\264\347\211\210.md" @@ -0,0 +1,379 @@ +#### config + + ##### JdbcConfig + +~~~ java +package com.dsg.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?useSSL=false"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} + +~~~ + +MybatisConfig + +~~~ java +package com.dsg.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.dsg.domain"); + // 设置数据源dataSource + ssfb.setDataSource(dataSource); + return ssfb; + } + + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + // 设置代理的包 + msc.setBasePackage("com.dsg.mapper"); + return msc; + } +} + +~~~ + +SpringConfig + +~~~ java +package com.dsg.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@ComponentScan({"com.dsg.domain","com.dsg.mapper"}) +@Import({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig { +} + +~~~ + +SpringMvcConfig + +~~~ java +package com.dsg.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.dsg.controller") +@EnableWebMvc +public class SpringMvcConfig { + +} + +~~~ + +SpringWebInitConfig + +~~~ java +package com.dsg.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class SpringWebInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +~~~ + +#### controller + +BrandController + +~~~ java +package com.dsg.controller; + +import com.dsg.domain.Brand; +import com.dsg.mapper.BrandMapper; +import com.dsg.service.BrandService; +import com.dsg.service.impl.BrandServiceImpl; +import org.apache.ibatis.annotations.Insert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/brands") +public class BrandController { + + @Autowired + private BrandService brandService; + + @GetMapping + public List getBrands(){ + List brands = brandService.selectAll(); + return brands; + } + + @GetMapping("/{id}") + public Brand getBrandById(@PathVariable Integer id){ + System.out.println("接收到一个id==》"+id); + System.out.println("从数据库查询到了一个id="+id+"的品牌"); + return new Brand(id,"华为","华为",100,"华为",1); + } + + @DeleteMapping("/{id}") + public String deleteById(@PathVariable Integer id){ + System.out.println("接收到一个id==》"+id); + System.out.println("从数据库删除了一个id="+id+"的品牌"); + return "is ok"; + } + + @PutMapping + public String editBrand(@RequestBody Brand brand){ + System.out.println("修改一个Brand==》"+brand); + brandService.editBrand(brand); + return "edit is ok"; + } + + @PostMapping + public String addBrand(@RequestBody Brand brand){ + System.out.println("接收到一个Brand==》"+brand); + brandService.addBrand(brand); + return "add is ok"; + } + +} + +~~~ + +#### domain + +Brand + +~~~ java +package com.dsg.domain; + +public class Brand { + private Integer id; + private String brandName; + private String companyName; + private Integer ordered; + private String description; + private Integer status; + + @Override + public String toString() { + return "Brand{" + + "id=" + id + + ", brandName='" + brandName + '\'' + + ", companyName='" + companyName + '\'' + + ", ordered=" + ordered + + ", description='" + description + '\'' + + ", 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; + } + + 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; + } +} + +~~~ + +#### mapper + +brandMapper + +~~~ java +package com.dsg.mapper; + +import com.dsg.domain.Brand; +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 BrandMapper { + @Select("select * from tb_brand") + List selectAll(); + + @Select("select id, brand_name brandName , company_name companyName, ordered, description, status from tb_brand where id = #{id}}") + Brand selectById(int id); + + @Delete("delete from tb_brand where id = #{id}") + void deleteById(int id); + + @Insert("insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status})") + void addBrand(Brand brand); + + @Update("update tb_brand set brand_name =#{brandName},company_name = #{companyName},ordered=#{ordered},description=#{description},status =#{status} where id = #{id}") + void editBrand(Brand brand); +} + +~~~ + +#### service + +brandService + +~~~ java +package com.dsg.service; + +import com.dsg.domain.Brand; + +import java.util.List; + +public interface BrandService { + List selectAll(); + + Brand selectById(int id); + + void deleteById(int id); + + void addBrand(Brand brand); + + void editBrand(Brand brand); +} + +~~~ + +##### impl + +brandServiceImpl + +~~~ java +package com.dsg.service.impl; + +import com.dsg.domain.Brand; +import com.dsg.mapper.BrandMapper; +import com.dsg.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +public class BrandServiceImpl implements BrandService { + @Autowired + private BrandMapper brandMapper; + + @Override + public List selectAll(){ + System.out.println("从业务层开始:查询所有"); + return brandMapper.selectAll(); + } + + @Override + public Brand selectById(int id) { + System.out.println("从业务层开始,通过id查询"); + 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); + } +} + +~~~ + -- Gitee