栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一周重点内容总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一周重点内容总结

1.Get与Post 1.1两种方法的区别

Get在跳转时会将数据拼接在url中显示出来,可以携带少量数据,可以通过输入网址发送请求

Post在跳转网页时不会将数据拼接到url中(相较Get更加安全),可以携带大量数据,可以通过表单发送请求

1.2两种方法的使用

写法

先是Get方法

想要使用Get方法,就要在相应的方法前加上@GetMapping标签

@GetMapping("/index")
public String index(){
    return "index";
}

接下来我们便可以在浏览器的地址栏输入

http://localhost:8080/index

来访问到返回值中的对应页面了

同理,使用Post方法就需要@PostMapping标签

@PostMapping("/login")
public String login(String name,String password){
    return "login";
}

如何传参

Get方法

@GetMapping("/index/{name}/{age}")
// @PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
public String index(@PathVariable String name,@PathVariable int age) {
    System.out.println(name);
    System.out.println(age);
    return "index";
}

然后在网站上输入

http://localhost:8080/index/zhangs

控制台的输出结果

来试试简单的写法

方法可以这样写

@GetMapping("/index2")
// 注意这里将上面的int改成Integer,否则在你不输入int类型时就会报错
public String index2(String name,Integer age) {
    System.out.println(name);
    System.out.println(age);
    return "index";
}

这时的地址栏便要这样写

http://localhost:8080/index2?name=zhangsan&age=30

输出结果(控制台)

当然,它也是可以来传对象的

我先随便写个用户类

package com.example.controller;

import lombok.Data;


// 使用lombok插件省去写Get/Set方法
@Data
public class User {
    private String name;
    private String age;
    private String sex;
}
@GetMapping("/index3")
public String index3(User user) {
    System.out.println(user.getName());
    System.out.println(user.getAge());
    System.out.println(user.getSex());
    return "index";
}

在网页上输入

http://localhost:8080/index3?name=zhangsan&age=30&sex=男

控制台输出

 Post方法

使用Post方法时,不需要从地址栏输入参数,而是要使用html中的form表单

先看html哪里是怎么写的




    
    测试



姓名:
年龄:
性别:

在网页中的效果

在表单中填入信息

 

点击提交按钮(login)

参数会传到Post方法中

@PostMapping("/login")
    public String login(User user,Model model){
        System.out.println(user.getName());
        System.out.println(user.getAge());
        System.out.println(user.getSex());

        model.addAttribute("name",user.getName());

        return "login";
    }

控制台输出结果

 @PostMapping括号中的地址就是html中的action的地址

2.MyBatis Plus 2.1简单介绍一下MyBatis Plus是什么

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

简单来说就是一个依托于spring的数据库管理工具

2.2如何使用MyBatis Plus

这里我将参考官方文档做个演示

SQL部分

先新建一个数据库

这里一定要注意字符集的设置

然后根据官方文档给的sql命令建表

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAr(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAr(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

插入数据

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

初始化项目

新建Spring项目,导入如下依赖

 然后导入MyBatis Plus的依赖


        com.baomidou
        mybatis-plus-boot-starter
        Latest Version
    

注意将Latest Version改成你使用的版本号

然后写配置文件

#驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=123456
#链接的数据库
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8

编写实体类

package com.example.domain;

import lombok.Data;
import lombok.ToString;


@Data
@ToString
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

编写Mapper类

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.example.domain.User;




public interface UserMapper extends baseMapper {
}

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringBootMybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
    }

}

开始使用

使用Spring Boot自动创建的测试类

package com.example;

import com.example.domain.User;
import com.example.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringBootMybatisPlusApplicationTests {
    // 这个标签是注入,加了这个标签就是将下面的类交给Spring托管了
    // 如果不加这个标签,下面这个类就不能实例化
    @Autowired
    private UserMapper userMapper;

    @Test // 查询测试
    public void test1() {
        List userList = userMapper.selectList(null);
        for (User user : userList) {
            System.out.println(user);
        }
    }
}

控制台的输出

当然,增删改的操作MyBatis Plus同样可以做

这里我想设置主键ID的自增

首先找到实体类User

在id属性上方添加标签

// 设置id的自增
    @TableId(type = IdType.AUTO)

注意不要导错包

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

然后来到SQL的设置,给id设置上自增

点开高级-->自动增量

输入1,点保存(它会自动改变成目前元素数+1)

数据库设置完毕

接下来看一下怎么插入数据

可以参考官方文档中CRUD接口部分

现在试着插入一条数据

@Test
    public void test2() {
        User user = new User();
        user.setName("火锅");
        user.setAge(30);
        int i = userMapper.insert(user);

        System.out.println(i);
        System.out.println(user);

    }

控制台

可以看到id的自增也生效了

数据也确实插入了

删除数据

@Test
    public void test3() {
        // 注意,这里的id是指表的主键
        // 也就是根据主键删除
        int i = userMapper.deleteById(2);
        System.out.println(i);
    }

 更新数据

@Test
    public void test4() {
        User user = new User();
        user.setName("奶油糖");
        user.setAge(30);
        user.setId(1L);
        int i = userMapper.updateById(user);

        System.out.println(i);
        System.out.println(user);

    }

> 应用

可以联系之前所学

做一个用id查名字和年龄的小网页

控制类

package com.example.controller;

import com.example.domain.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class IndexController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @PostMapping ("/homePage")
    public String homePage(Model model, Long id) {
        User user = userMapper.selectById(id);
        model.addAttribute("user",user);
        return "homePage";
    }
}

index页




    
    Title


输入用户id:

homePage页




    
    Title


输入用户id:

效果展示

结果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424103.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号