SpringBoot快速入门复习概览
步骤
Jdk maven 配置到系统环境变量修改idea 的Maven配置打开spring boot项目,选择web依赖 mysql依赖...
写一个controller控件 @Restcontroller(只请求数据 返回的对象数据会转为json格式) @Controller(请求的是页面和数据)
-
方法 @GetMapping("/hello")
dev-tools 组件 热部署监听classpath下面文件变化
-
在pom.xml中添加dependency
控制器如何接受前端请求-->路由映射
-
@RequestMapping
-
value (url模板/正则表达式) ("/getJson/*.json")"**" 匹配任意路径 "?"匹配单个字符 * 匹配任意字符
-
method RequestMethod.GET --> GetMapping等价
-
-
参数传递
-
前端通过get直接在url上,后端只要在方法上名称不要搞错可以接收到
-
示例
-
:http://localhost:8080/hello?nickname=zhangsan&phone=123
-
方法写@RequestMapping(value="/hello",method=RequestMethod.GET)
-
public String hello(String nickname,String phone){
-
return "你好"+nickname+phone;}
-
-
还有@Pathvariable 路径参数注解
-
如果参数名称和前端名称不一致,
-
可以使用(@RequestParam("nickname") String name)
-
同理意味着这个参数是必须的,否则会报错405
-
405 post get方法错误,404路径输错
-
-
-
post可以用工具调试
-
参数太多,可以封装到类里面。entity,但是里面的属性要和前端传回来的一致。
-
在测试时如果传递的是json的数据,要在参数前面加一个@RequestBody注解
-
-
文件上传
-
略
拦截器
-
先写一个实现类
-
写在Interceptor包下面
-
实现HandlerInterceptor方法
-
重写prehandler方法(有三个方法,分别是在controller之前之后,页面渲染之后执行)
-
public boolean preHandler(HttpServletRequest request,HttpServletResponse response,Object Handler){
-
...
-
return false;}
-
-
-
注册这个实现类
-
写在config包下面
-
@Configuration注解 实现WebMvcConfigurer接口
-
重写addInterceptors方法
-
public void addIntercetors(InterceptorRegistry registry){
-
registry.addInterceptor(new LoginInterceptor()).addPathPattrens("user/**");}
-
-
构建Restful服务
略
swagger在编码中加个注解。我建议直接用/idea自带
MyBatis-plus
-
添加依赖
-
MyBatisPlus mysql驱动 数据库连接池druid 依赖...
-
-
全局配置
-
spring.datasource.type .driver-class-name .url .username .password
-
url如果是远程的,就是localhost要改成ip,jdbc:mysql://localhost:3306/mydb?useSSL=false
-
mybatis-plus.configuration.log-impl
-
-
添加@MapperScan注解 放在启动类中
-
@MapperScan("com.xxx.mapper")
-
使用
-
controller 写方法
-
mapper接口 加上@Mapper注解 (不过这个注解和前面的MapperScan要一个就好了)
-
entity实体类
-
可以用@Data注解省掉setter,getter等方法
-
-
具体使用
-
在mapper接口里面写抽象方法,简单查询用
-
例如@Select("select * from user")就可以
-
public List<User> find(); // 抽象方法
-
“#{username} ”占位符
-
参数是个对象,上面sql语句也可以写属性名,注意驼峰命名区别,主键自增id可以不传
-
-
在controller方法中,直接注入就行@Autowired。因为userMapper在springboot被自动实例化了,动态代理实现这个接口。(这个是mybatis的使用,对比一下)
-
使用
-
UserMapper接口继承BaseMapper<User>
-
自动根据这个类,找到user表,做增删改查。。。啊?那锁呢?那复杂操作呢?
-
-
controller那边方法就不是userMapper.find()了,而是那个接口里面实现的方法selectList(null) 括号里面是条件。
-
还有一些很有用注解
-
如果表名和类名不一致,可以在类名上面加注解@TableName("t_user") 这个就太有用了吧!!
-
主键自增的可以加一个@TableId(type=IdType.AUTO) 这样代码里面也能够拿到这个id值了,虽然数据库中自增,但如果没有,代码为0。
-
字段如果不对应,可以用@TableField对应
-
多表查询,条件查询,分页查询
-
多表查询 (感觉不如xml,用得是mybatis)
-
@Result 给每个字段进行赋值 所有字段都要映射 即使名称一致
-
@One
-
@Many 表示有多个 一对多的关系
-
-
如果没有的字段但是需要查询,可以
-
@TableField(exit=false)
-
private List<Order> orders;
-
可以让其做映射
-
-
条件查询
-
QueryWrapper 先new出来,m-plus(mybatis-plus我的简写)官方文档里面有介绍
-
QueryWrapper<User> queryWrapper = new QueryWrapper();
-
queryWrapper.eq("username","zhangsan");
-
return userMapper.selectList(queryWrapper);
-
-
-
分页查询
-
limit
-
先做一个配置,定义一个配置类。
-
@Configurationpublic class MyBatisPlusConfig{@Beanpublic MyBatisInterceptor paginationInterceptor(){MyBatisPlusInterceptor interceptor = new MyBatisPlusInterceptor();PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); // 数据库类型interceptor.addInnerInterceptor(paginationInterceptor);return interceptor;}}
-
配置完之后就可以调用selectPage方法
-
@GetMapping("/user/findByPage") public IPage findByPage(){Page<User> page = new Page<>(0,2); // 设置起始值及每页条数IPage iPage = userMapper.selectMapper.selectPage(page,null); //第二个参数是查询条件return iPage; }
-
-
VUE
快速入门
-
导入vue.js的srcipt脚本文件
-
<script src="https://unpkg.com/vue@next"><script>
-
或者 "https://unpkg.com/vue@3"
-
-
在页面中声明一个将要被vue所控制的DOM区域,既MVVM中的View
-
<div id="app">{{message}} </div>
-
-
创建vm实例对象(vue实例对象)
-
const hello={ // 指定数据源,即MVVM中的Modeldata:function(){return {message:'Hello Vue!'}} } const app=Vue.createApp(hello) app.mount('#app') // 指定当前vue实例要控制页面的哪个区域
-
<script> // 指定数据源,即MVVM中的ModelVue.createApp({data(){return {message:'Hello Vue!'}} }).mount('#app') </script>
-