SpringBoot 学习(一)源码还没撕,先试试怎么用

SpringBoot 学习(一)源码还没撕,先试试怎么用

关于怎么搭建项目,写个HelloWorld出来,就不多说了

  • SpringBoot 整合Mybatis
  1. pom.xml 导入依赖
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
  1. application.properties写配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC

在配置driver-class-name可能需要导入mysql-connector的jar包。File–>Project Structure–>Modules–>点+号导入对应的jar包。

url配置时,serverTimezone好像都是必须要写的

  1. 写表对应的实体类
package com.fallrain.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Data
@NoArgsConstructor
public class User {

    //用户姓名
    private String name;
    //年龄
    private int age;
    //城市
    private String city;
    //详细住址
    private String address;
    //邮箱
    private String email;
    //电话
    private String phone;
    //微信
    private String weixin;
    //qq
    private String qq;
    //微博
    private String weibo;
    //性别
    private String sex;
    //简介
    private String description;

    @Override
    public String toString() {
        return "User{" +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", city='" + city + '\'' +
                ", address='" + address + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", weixin='" + weixin + '\'' +
                ", qq='" + qq + '\'' +
                ", weibo='" + weibo + '\'' +
                ", sex='" + sex + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return  age == user.age &&
                Objects.equals(name, user.name) &&
                Objects.equals(city, user.city) &&
                Objects.equals(address, user.address) &&
                Objects.equals(email, user.email) &&
                Objects.equals(phone, user.phone) &&
                Objects.equals(weixin, user.weixin) &&
                Objects.equals(qq, user.qq) &&
                Objects.equals(weibo, user.weibo) &&
                Objects.equals(sex, user.sex) &&
                Objects.equals(description, user.description);
    }

    @Override
    public int hashCode() {
        return Objects.hash( name, age, city, address, email, phone, weixin, qq, weibo, sex, description);
    }


    public User(String name, int age, String city, String address, String email, String phone, String weixin, String qq, String weibo, String sex, String description) {
        this.name = name;
        this.age = age;
        this.city = city;
        this.address = address;
        this.email = email;
        this.phone = phone;
        this.weixin = weixin;
        this.qq = qq;
        this.weibo = weibo;
        this.sex = sex;
        this.description = description;
    }
}

  1. 创建mapper包,写对应的mapper类
@Repository
public interface UserMapper {
    @Select("select * from kkb_user")
    List<User> getAll();
    @Insert("insert into kkb_user(name,age,city,address,email,phone,weixin,qq,weibo,sex,description) " +
            "values(#{name},#{age},#{city},#{address},#{email},#{phone},#{weixin},#{qq},#{weibo},#{sex},#{description})")
    void insert(User user);
}

@Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理,如果不加,使用时会有找不到bean的警告提示。

这个接口上还需要使用@Mapper注解或者在启动类上添加@MapperScan(com.example.mapper)

在接口的方法上,添加sql的注解,也可以用xml的方式,xml的sql定制更灵活。

  1. 写个controller类试试水
@Autowired
private UserMapper userMapper;//不加@Mapper的注解就会有警报啦

@RequestMapping(value = "/user/getAll",method = RequestMethod.GET)
public List<User> getAll(){
    List<User> all = userMapper.getAll();
    return all;
}
[{"id":1,"name":"fallrain","age":1,"city":"here","address":"here","email":"12@12.com","phone":"11","weixin":"11","qq":"11","weibo":"11","sex":"nan","description":"我在这里等你"},{"id":26,"name":"karl","age":12,"city":"there","address":"there","email":"123@abc.com","phone":"11","weixin":"11","qq":"11","weibo":"11","sex":"nan","description":"1111"}]

OK。搞定了一个查询的功能,前端写好页面就可以将个人信息在前端展示了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值