Springboot模板整合(三)配置mybatis框架以及MySQL数据库

引工具包

在pom.xml文件中引入如下包

<!--引入mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--引入mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!--引入JSON-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
<!--引入thymeleaf框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

配置相关内容

在application.properties中添加如下配置

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/template01?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
#配置mybatis打印SQL语句
logging.level.com.example.springboot_template01.dao=DEBUG

基于mybatis框架的SQL

在entity包中创建一个实体类,用来接收数据库的数据

package com.example.springboot_template01.entity;

/**
 * 用户类
 */
public class Consumer {
    private String consumer_name;
    private String consumer_id;
    private String consumer_other_name;
    private String consumer_sex;
    private String consumer_birth;
    private String consumer_phone;
    private String consumer_email;
    private String consumer_password;
    private String consumer_old_password;
    private String consumer_online;

    public String getConsumer_name() {
        return consumer_name;
    }

    public void setConsumer_name(String consumer_name) {
        this.consumer_name = consumer_name;
    }

    public String getConsumer_id() {
        return consumer_id;
    }

    public void setConsumer_id(String consumer_id) {
        this.consumer_id = consumer_id;
    }

    public String getConsumer_other_name() {
        return consumer_other_name;
    }

    public void setConsumer_other_name(String consumer_other_name) {
        this.consumer_other_name = consumer_other_name;
    }

    public String getConsumer_sex() {
        return consumer_sex;
    }

    public void setConsumer_sex(String consumer_sex) {
        this.consumer_sex = consumer_sex;
    }

    public String getConsumer_birth() {
        return consumer_birth;
    }

    public void setConsumer_birth(String consumer_birth) {
        this.consumer_birth = consumer_birth;
    }

    public String getConsumer_phone() {
        return consumer_phone;
    }

    public void setConsumer_phone(String consumer_phone) {
        this.consumer_phone = consumer_phone;
    }

    public String getConsumer_email() {
        return consumer_email;
    }

    public void setConsumer_email(String consumer_email) {
        this.consumer_email = consumer_email;
    }

    public String getConsumer_password() {
        return consumer_password;
    }

    public void setConsumer_password(String consumer_password) {
        this.consumer_password = consumer_password;
    }

    public String getConsumer_old_password() {
        return consumer_old_password;
    }

    public void setConsumer_old_password(String consumer_old_password) {
        this.consumer_old_password = consumer_old_password;
    }

    public String getConsumer_online() {
        return consumer_online;
    }

    public void setConsumer_online(String consumer_online) {
        this.consumer_online = consumer_online;
    }
}

创建一个dao包,并创建如下内容

package com.example.springboot_template01.dao;

import com.example.springboot_template01.entity.Consumer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
@Mapper
public interface ConsumerDao {
    @Select("select * from consumer")
    public List<Consumer> selectAll();
    @Select("select * from consumer where consumer_id = #{consumer_id}")
    public Consumer selectByName(Consumer consumer);
    @Select("select * from consumer where consumer_id = #{consumer_id} and consumer_password = #{consumer_password}")
    public Consumer selectByPassword(Consumer Consumer);

    @Select("select * from consumer where consumer_email =#{consumer_email}")
    public Consumer selectConsumerEmailExit(Consumer consumer);


}

创建controller类

package com.example.springboot_template01.web;

import com.example.springboot_template01.common.EmailUtil;
import com.example.springboot_template01.common.VerificationCode;
import com.example.springboot_template01.dao.ConsumerDao;
import com.example.springboot_template01.entity.Consumer;
import com.example.springboot_template01.service.ConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@Api(value = "ConsumerController",tags = "用户接口")
public class ConsumerController {
    private static Map<String,Long> map = new HashMap<String,Long>();

    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ConsumerDao consumerDao;
    @Autowired
    private EmailUtil emailUtil;

    @ApiOperation(value = "selectConsumerAll",notes = "无条件不分页查询所有的用户")
    @GetMapping("/selectConsumerAll")
    @ResponseBody
    public String selectConsumerAll(){
        return consumerService.selectAll();
    }

   
}

测试

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值