diff --git "a/50 \345\274\240\350\265\267\347\221\236/20231227 \346\263\250\350\247\243.md" "b/50 \345\274\240\350\265\267\347\221\236/20231227 \346\263\250\350\247\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..150cd0ce5f03217e32d9d142dd1c35584482aaf3 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20231227 \346\263\250\350\247\243.md" @@ -0,0 +1,57 @@ +#### 创建容器 + +```java +方式一:类路径加载配置文件 +ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); +方式二:文件路径加载配置文件 +ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\applicationContext.xml"); +加载多个配置文件 +ApplicationContext ctx = new ClassPathXmlApplicationContext("bean1.xml", "bean2.xml"); +``` + +### 获取bean + +```java +方式一:使用bean名称获取 +BookDao bookDao = (BookDao) ctx.getBean("bookDao"); +方式二:使用bean名称获取并指定类型 +BookDao bookDao = ctx.getBean("bookDao", BookDao.class); +方式三:使用bean类型获取 +BookDao bookDao = ctx.getBean(BookDao.class); +``` + +#### 注解开发定义bean + +```java +Spring提供@Component注解的三个衍生注解 +@Controller:用于表现层bean定义 +@Service:用于业务层bean定义 +@Repository:用于数据层bean定义 + +@Repository("bookDao") public class BookDaoImpl implements BookDao { } +@Service public class BookServiceImpl implements BookService { } + +``` + +#### Spring整合MyBatis + +```java +// 1. 创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); +// 2. 加载SqlMapConfig.xml配置文件 InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); +// 3. 创建SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); +// 4. 获取SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); +// 5. 执行SqlSession对象执行查询,获取结果User AccountDao accountDao = sqlSession.getMapper(AccountDao.class); Account ac = accountDao.findById(2); System.out.println(ac); +// 6. 释放资源 sqlSession.close(); + +``` + +#### 整合JUnit + +```java +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SpringConfig.class) public class BookServiceTest +{ @Autowired private BookService bookService; + @Test public void testSave(){ bookService.save(); } } + +``` + diff --git "a/50 \345\274\240\350\265\267\347\221\236/20231229 mvc.md" "b/50 \345\274\240\350\265\267\347\221\236/20231229 mvc.md" new file mode 100644 index 0000000000000000000000000000000000000000..7f7f87febc3c43e91c5a8f9a832a8429bcce7174 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20231229 mvc.md" @@ -0,0 +1,310 @@ + + + + +```java +POM: + + 4.0.0 + com.mxx + mvc + 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 + / + + + + + + +index: + + +

Hello World!

+ + + web.xml: + + + + Archetype Created Web Application + +``` + +## 复习: + +```java + +### JdbcConfig +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; + } +} +``` + +```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; + } +} +``` + +```java + +``` + +```java + SpringConfig +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 +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 +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 测试类 +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 + + + + + +``` +