diff --git "a/56 \350\265\265\346\225\217/20231231 \345\244\215\344\271\240.md" "b/56 \350\265\265\346\225\217/20231231 \345\244\215\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..965bd98fa1a12e4045e191ce2dd5b553858369df --- /dev/null +++ "b/56 \350\265\265\346\225\217/20231231 \345\244\215\344\271\240.md" @@ -0,0 +1,381 @@ +~~~ 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; + } +} + +~~~ + +~~~ 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"); + ssfb.setDataSource(dataSource); + return ssfb; + } + + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.dsg.mapper"); + return msc; + } +} + +~~~ + +~~~ java +package com.dsg.config; + +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.mapper.MapperScannerConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +import javax.sql.DataSource; + +@Configuration +@Import({JdbcConfig.class, MybatisConfig.class}) +@ComponentScan("com.dsg") +public class SpringConfig { + +} + +~~~ + +~~~ 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; + } +} + +~~~ + +~~~ java +package com.dsg.mapper; + +import com.dsg.domain.Brand; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +public interface BrandMapper { + @Select("select * from tb_brand") + List findAll(); + + List selectByIdBrand(Brand brand); + + +} + +~~~ + +~~~ 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 org.springframework.stereotype.Service; + +import java.util.List; +@Service +public class BrandServiceImpl implements BrandService { + @Autowired + private BrandMapper brandMapper; + @Override + public List selectAll() { + System.out.println("业务的命运齿轮开始运转。。。。"); + return brandMapper.findAll(); + } +} + +~~~ + +~~~ java +package com.dsg.service; + +import com.dsg.domain.Brand; +import org.springframework.stereotype.Service; + +import java.util.List; + +public interface BrandService { + List selectAll(); +} + +~~~ + +~~~ java +import com.dsg.config.SpringConfig; +import com.dsg.domain.Brand; +import com.dsg.mapper.BrandMapper; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import java.util.List; + +public class APP { + public static void main(String[] args) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); + BrandMapper bean = ctx.getBean(BrandMapper.class); + List all = bean.findAll(); + System.out.println(all); + + } +} + +~~~ + +~~~ java + + + + + + + + + + + +~~~ + +~~~ java +package com.dsg.test; + +import com.dsg.config.SpringConfig; +import com.dsg.domain.Brand; +import com.dsg.mapper.BrandMapper; +import com.dsg.service.BrandService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.List; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SpringConfig.class) +public class BrandTest { + @Autowired + private BrandMapper brandMapper; + @Autowired + private BrandService brandService; + + @Test + public void test01(){ + String brandName = "华为"; + String companyName = "华为"; + + brandName = "%" + brandName + "%"; + companyName = "%" + companyName + "%"; + + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + List brands = brandMapper.selectByIdBrand(brand); + System.out.println(brands); + } + + @Test + public void testService(){ + List brands = brandService.selectAll(); + System.out.println(brands); + + } +} + +~~~ + +~~~ java + + + 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 + + + + + +~~~ + diff --git "a/56 \350\265\265\346\225\217/20240102 SpringMVC.md" "b/56 \350\265\265\346\225\217/20240102 SpringMVC.md" new file mode 100644 index 0000000000000000000000000000000000000000..a007e98df80b5a08ce6285e0402799664c0c3356 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20240102 SpringMVC.md" @@ -0,0 +1,75 @@ +### config + + #### SpringConfing + +~~~ java +package com.dsg.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.dsg") +public class SpringConfig { + +} + +~~~ + +#### WebInitConfig + +~~~java +package com.dsg.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + protected Class[] getRootConfigClasses() { + return new Class[0]; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +~~~ + +### controller + + #### UserController + +~~~ java +package com.dsg.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class UserController { + @RequestMapping("/save") + @ResponseBody + public String userSave(){ + return "ok"; + } + + @RequestMapping("/book") + @ResponseBody + public String userBook(){ + return "book is ok"; + } + + @RequestMapping("souYe") +// @ResponseBody + public String SouYe(){ + return "index.jsp"; + } +} + +~~~ +