From 610aeef71377a239c15fc0357cee515cff58f0a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E8=88=9C?= <1991510644@qq.com> Date: Wed, 3 Jan 2024 13:26:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "51 \347\250\213\350\210\234/20231222.md" | 508 ++++++++++++++++++++++ "51 \347\250\213\350\210\234/20231226.md" | 162 +++++++ "51 \347\250\213\350\210\234/20231227.md" | 53 +++ "51 \347\250\213\350\210\234/20231228.md" | 265 +++++++++++ "51 \347\250\213\350\210\234/20231229.md" | 265 +++++++++++ "51 \347\250\213\350\210\234/20240102.md" | 182 ++++++++ 6 files changed, 1435 insertions(+) create mode 100644 "51 \347\250\213\350\210\234/20231222.md" create mode 100644 "51 \347\250\213\350\210\234/20231226.md" create mode 100644 "51 \347\250\213\350\210\234/20231227.md" create mode 100644 "51 \347\250\213\350\210\234/20231228.md" create mode 100644 "51 \347\250\213\350\210\234/20231229.md" create mode 100644 "51 \347\250\213\350\210\234/20240102.md" diff --git "a/51 \347\250\213\350\210\234/20231222.md" "b/51 \347\250\213\350\210\234/20231222.md" new file mode 100644 index 0000000..6392af1 --- /dev/null +++ "b/51 \347\250\213\350\210\234/20231222.md" @@ -0,0 +1,508 @@ +# 笔记 + +1.导入maven环境 + 2.创建dao接口,里面定了你需要对数据库执行什么操作 + 3.编写mapper文件,sql映射文件,写上你的sql语句 + 4.创建mybatis主配置文件,读取配置文件中的内容,连接到数据库 + 5.使用mybatis对象执行sql语句:sqlSession对象 + 6.关闭连接:sqlSession.close(); + + +# 作业 + +数据库表 + +```java +CREATE TABLE `t_user` ( + `id` int NOT NULL AUTO_INCREMENT, + `username` varchar(255) DEFAULT NULL, + `password` varchar(255) DEFAULT NULL, + `email` varchar(255) DEFAULT NULL, + `phone` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + +INSERT INTO `t_user`(`id`, `username`, `password`, `email`, `phone`) VALUES (1, 'admin', 'admin', 'admin@qq.com', '1008611'); +INSERT INTO `t_user`(`id`, `username`, `password`, `email`, `phone`) VALUES (2, 'root', 'root', 'root@qq.com', '1008622'); +INSERT INTO `t_user`(`id`, `username`, `password`, `email`, `phone`) VALUES (19, 'admin2', 'root2', 'root@qq.com2', '1008644'); + + +``` + +创建实体类 + +```java +public class User { + private Integer id; + private String username; + private String password; + private String email; + private String phone; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public User() { + } + + public User(Integer id, String username, String password, String email, String phone) { + this.id = id; + this.username = username; + this.password = password; + this.email = email; + this.phone = phone; + } + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + ", email='" + email + '\'' + + ", phone='" + phone + '\'' + + '}'; + } + +} + + +``` + +创建maven项目 + +```java + + + 4.0.0 + + org.example + mybatis + 1.0-SNAPSHOT + + + UTF-8 + 1.9 + 1.9 + + + + + + junit + junit + 4.11 + test + + + + mysql + mysql-connector-java + 8.0.19 + + + + org.mybatis + mybatis + 3.5.4 + + + + org.junit.jupiter + junit-jupiter + RELEASE + compile + + + + + + + + + + + src/main/java + + **/*.properties + **/*.xml + + false + + + + + + +``` + +配置mybatis的主配置文件 + +```java + + + + + + + +--> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +编写mybatis工具类 + +```java +public class MyBatisUtils { + + private static SqlSessionFactory factory = null; + + static { + //读取mybatis.xml配置文件 + String config="mybatis.xml"; + try { + //将配置文件加入到流中 + InputStream in = Resources.getResourceAsStream(config); + //创建factory对象 + factory = new SqlSessionFactoryBuilder().build(in); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + public static SqlSession getSqlSession(){ + SqlSession sqlSession = null; + if (factory!=null){ + //如果 factory!=null 就创建 sqlSession对象 + sqlSession = factory.openSession(false);//非自动提交事务 + } + + return sqlSession; + } +} + +``` + +定义接口 + +```java +public interface UserManagerDao { + + /** + * 查询所有的数据 用于视图展示 + * @return 返回一个List集合 + */ + public List queryUser(); + + + /** + * 添加 + * @param user 一个user对象 + * @return 成功返回true 失败返回false + */ + public Boolean addUser(User user); + + /** + * 删除用户 + * @param id 需要删除的用户id + * @return 成功返回true 失败返回false + */ + public Boolean delUser(String id); + + /** + * 修改用户信息 + * @param user 需要一个user对象 + * @return 成功返回true 失败返回false + */ + public Boolean updateUser(User user); + + /** + * 按照用户名查找用户 + * @param name 需要查找的用户名 + * @return 返回一个List集合 + */ + public List likeUser(String name); + + + +} + +``` + +xml文件 + +```java + + + + + + + + + + insert into t_user values(#{id},#{username},#{username},#{email},#{phone}) + + + + delete from t_user where id=#{id} + + + + update t_user set username=#{username},password=#{password},email=#{email},phone=#{phone} where id=#{id} + + + + +``` + +编写daoImpl + +```java + +public class UserManagerDaoImpl implements UserManagerDao { + + /** + * 查询所有的数据 用于视图展示 + * + * @return 返回一个List集合 + */ + @Override + public List queryUser() { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.queryUser"; + //执行sql语句 + List userList = sqlSession.selectList(sqlId); + // sqlSession.commit(); //mybatis默认不会手动提交事务,在修改数据之后要 提交事务 + //关闭连接 + sqlSession.close(); + return userList; + } + + + /** + * 添加 + * + * @param user 一个user对象 + * @return 成功返回true 失败返回false + */ + @Override + public Boolean addUser(User user) { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.addUser"; + //执行sql语句 + int updateCount = sqlSession.update(sqlId,user); + sqlSession.commit(); //mybatis默认不会手动提交事务,在修改数据之后要 提交事务 + //关闭连接 + sqlSession.close(); + if(updateCount>0){ + return true; + }else{ + return false; + } + + } + + /** + * 删除用户 + * + * @param id 需要删除的用户id + * @return 成功返回true 失败返回false + */ + @Override + public Boolean delUser(String id) { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.delUser"; + //执行sql语句 + int delete = sqlSession.delete(sqlId, id); + sqlSession.commit(); //设置为自动提交事务 + //关闭连接 + sqlSession.close(); + if(delete>0){ + return true; + }else { + return false; + } + + } + + /** + * 修改用户信息 + * + * @param user 需要一个user对象 + * @return 成功返回true 失败返回false + */ + @Override + public Boolean updateUser(User user) { + //获取sqlSession对象 + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.updateUser"; + //执行sql语句 + int updateCount = sqlSession.update(sqlId,user); + sqlSession.commit(); //设置为自动提交事务 + //关闭连接 + sqlSession.close(); + if(updateCount>0){ + return true; + }else { + return false; + } + + } + + /** + * 按照用户名查找用户 + * + * @param name 需要查找的用户名 + * @return 返回一个List集合 + */ + @Override + public List likeUser(String name) { + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + String sqlId = "com.mybatis.dao.UserManagerDao.likeUser"; + List userList = sqlSession.selectList(sqlId, name); + sqlSession.commit(); + //关闭连接 + sqlSession.close(); + return userList; + } +} + +``` + +Test类 + +```java +class UserManagerDaoImplTest { + + UserManagerDaoImpl userManagerDao = new UserManagerDaoImpl(); + + @Test + void queryUser() { + List userList = userManagerDao.queryUser(); + for (User user : userList) { + System.out.println(user); + } + } + @Test + void ListUser() { + + SqlSession sqlSession = MyBatisUtils.getSqlSession(); + UserManagerDao userDao = sqlSession.getMapper(UserManagerDao.class); + List userList = userDao.queryUser(); + for (User user : userList) { + System.out.println(user); + } + } + + @Test + void addUser() { + User user = new User(null,"admin","root","root@qq.com","1008644"); + Boolean flag = userManagerDao.addUser(user); + System.out.println(flag); + + } + + @Test + void delUser() { + Boolean flag = userManagerDao.delUser("23"); + System.out.println(flag); + } + + @Test + void updateUser() { + User user = new User(19,"admin2","root2","root@qq.com2","1008644"); + Boolean flag = userManagerDao.updateUser(user); + System.out.println(flag); + } + + @Test + void likeUser() { + List userList = userManagerDao.likeUser("admin"); + for (User user : userList) { + System.out.println(user); + } + } +} + +``` \ No newline at end of file diff --git "a/51 \347\250\213\350\210\234/20231226.md" "b/51 \347\250\213\350\210\234/20231226.md" new file mode 100644 index 0000000..becc9ad --- /dev/null +++ "b/51 \347\250\213\350\210\234/20231226.md" @@ -0,0 +1,162 @@ +## Ioc和DI + +```html +IoC(Inversion of Control)控制反转 +使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转 +Spring技术对IoC思想进行了实现 +Spring提供了一个容器,称为IoC容器,用来充当IoC思想中的“外部” +IoC容器负责对象的创建、初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean +DI(Dependency Injection)依赖注入 +在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入 +``` + +bean生命周期: + +```java +初始化容器 +创建对象(内存分配) +执行构造方法 +执行属性注入(set操作) +执行bean初始化方法 +使用bean +执行业务操作 +关闭/销毁容器 +执行bean销毁方法 +``` + +bean销毁时机 + +```java +容器关闭前触发bean的销毁 +关闭容器方式: +手工关闭容器 +ConfigurableApplicationContext接口close()操作 +注册关闭钩子,在虚拟机退出前先关闭容器再退出虚拟机 + ConfigurableApplicationContext接口registerShutdownHook()操作 + +public class AppForLifeCycle { public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ctx.close(); }} + +``` + +pom.xml + +```xml + + + + 4.0.0 + + com.mdd + spring_01_quickstart + 1.0-SNAPSHOT + + + + junit + junit + 4.13.2 + test + + + org.springframework + spring-context + 5.2.25.RELEASE + + + + + + +``` + +```xml +src/main/resources/ApplicationContext.xml +ApplicationContext.xml + + + + + + + +``` + +```java +文件名:BookDaoImpl.java + +package com.mdd.dao.impl; + +import com.mdd.dao.BookDao; + +public class BookDaoImpl implements BookDao { + public void save() { + System.out.println("book dao save ..."); + } +} +``` + +```java +文件名: BookServiceImpl.java + +package com.mdd.service.impl; +import com.mdd.dao.BookDao; +import com.mdd.dao.impl.BookDaoImpl; +import com.mdd.service.BookService; + +public class BookServiceImpl implements BookService { + + private BookDao bookDao; + + public void setBookDao(BookDao bookDao) { + this.bookDao = bookDao; + } + + public void save() { + System.out.println("book service save ..."); + bookDao.save(); + } + +} +``` + +```java +文件名:BookDao.java +package com.mdd.dao; +public interface BookDao { + public void save(); +} +``` + +```java +文件名:App.java +package com.mdd; +import com.mdd.service.BookService; +import com.mdd.service.impl.BookServiceImpl; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(String[] args) { +// BookService bookService = new BookServiceImpl(); +// bookService.save(); + //1.通过配置文件获取IOC容器 + ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml"); + //2.通过IOC容器的getBean 方法,获取对象 + BookServiceImpl bookService = (BookServiceImpl) ctx.getBean("bookService"); + bookService.save(); + + } +} +``` + +```java +文件名:BookService.java +package com.mdd.service; + +public interface BookService { + public void save(); +} +``` \ No newline at end of file diff --git "a/51 \347\250\213\350\210\234/20231227.md" "b/51 \347\250\213\350\210\234/20231227.md" new file mode 100644 index 0000000..b6caf78 --- /dev/null +++ "b/51 \347\250\213\350\210\234/20231227.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方法 + +注意:自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法 \ No newline at end of file diff --git "a/51 \347\250\213\350\210\234/20231228.md" "b/51 \347\250\213\350\210\234/20231228.md" new file mode 100644 index 0000000..5d788f7 --- /dev/null +++ "b/51 \347\250\213\350\210\234/20231228.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/51 \347\250\213\350\210\234/20231229.md" "b/51 \347\250\213\350\210\234/20231229.md" new file mode 100644 index 0000000..5d788f7 --- /dev/null +++ "b/51 \347\250\213\350\210\234/20231229.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/51 \347\250\213\350\210\234/20240102.md" "b/51 \347\250\213\350\210\234/20240102.md" new file mode 100644 index 0000000..22aefe6 --- /dev/null +++ "b/51 \347\250\213\350\210\234/20240102.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 + } + + +} +``` \ No newline at end of file -- Gitee