Springboot+mybatis+mysql8.0

一、环境信息

jdk:1.8
mysql:8.0.23
springboot:2.1.0
mybatis:3.5.2

二、maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <!--此处的配置是识别到mapper.xml文件,也可以在application.properties中配置-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

三、mybatis配置信息

1、数据库配置信息

spring.datasource.url = jdbc:mysql://192.168.0.12:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=riant
spring.datasource.password=root
#数据库名称
spring.datasource.name=test
#默认情况下mybatis是不开启SQL日志输出,需要手动配置,com.riant.dao为mapper文件夹路径
logging.level.com.riant.mybatis.dao=debug
#指定mapper的配置文件的路径是mapper文件夹下的所有 xml文件。
mybatis.mapper-locations=classpath:mapper/*.xml 

注意:mysql8.0版本配置与mysql5.0之间的区别:

  • 驱动区别,com.mysql.cj.jdbc.Driver
  • 连接信息,要有时区信息,要有是否使用ssl信息

2、mybatis的mapper.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--上面2行的是约束依赖,固定照抄就好-->
<!--下面的才是要自己编写的地方-->
<!--写mapper的配置文件第一步就是要写<mapper></mapper>标签-->
<!--<mapper></mapper>标签里包含着各个CURD操作的SQL语句-->
<mapper namespace="com.riant.mybatis.dao.WebSiteMapper">
    <!--定义一个名为BaseResultMap的返回类型-->
    <resultMap id="BaseResultMap" type="com.riant.mybatis.bean.WebSite">
        <id column="id" property="id" jdbcType="INTEGER"></id>
        <result column="name" property="name" jdbcType="CHAR"></result>
        <result column="url" property="url" jdbcType="VARCHAR"></result>
        <result column="alexa" property="alexa" jdbcType="INTEGER"></result>
        <result column="country" property="country" jdbcType="CHAR"></result>
    </resultMap>

    <!--查找语句-->
    <!--resultMap表示函数返回的类型-->
    <select id="selectWebSiteById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
       select * from websites where id = #{id,jdbcType=INTEGER}
    </select>
    <!--resultMap表示函数返回的类型-->
    <select id="selectAllWebSite" resultMap="BaseResultMap">
       select * from websites
    </select>
    <insert id="insertWebSite">
        insert into websites
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">
                name,
            </if>
            <if test="url != null">
                url,
            </if>
            <if test="alexa != null">
                alexa,
            </if>
            <if test="country != null">
                country,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">
                #{name,jdbcType=CHAR},
            </if>
            <if test="url != null">
                #{url,jdbcType=VARCHAR},
            </if>
            <if test="alexa != null">
                #{alexa,jdbcType=INTEGER},
            </if>
            <if test="country != null">
                #{country,jdbcType=CHAR},
            </if>
        </trim>
    </insert>
    <delete id="deleteById">
        delete from websites where id=#{id,jdbcType=INTEGER}
    </delete>
    <update id="updateWebSiteById" parameterType="com.riant.mybatis.bean.WebSite">
        update websites
        <set>
            <if test="name != null">
                name =#{name},
            </if>
            <if test="url != null">
                url =#{url},
            </if>
            <if test="alexa != null">
                alexa =#{alexa},
            </if>
            <if test="country != null">
                country =#{country},
            </if>
        </set>
        where id=#{id,jdbcType=INTEGER}
    </update>
</mapper>

3、建表语句

CREATE TABLE `websites` (
	`id` INT NOT NULL AUTO_INCREMENT,
	`name` CHAR(20) NOT NULL DEFAULT '' COMMENT '站点名称',
	`url` VARCHAR(255) NOT NULL DEFAULT '',
	`alexa` INT NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
	`country` CHAR(10) NOT NULL DEFAULT '' COMMENT '国家',
	PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=10
;

四、分层接口

1、dao层接口

@Repository
@Mapper
public interface WebSiteMapper {
    WebSite selectWebSiteById(int id);
    List<WebSite> selectAllWebSite();
    Integer insertWebSite(WebSite webSite);
    Integer deleteById(int id);
    Integer updateWebSiteById(WebSite webSite);
}

2、service层接口

package com.riant.mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.riant.mybatis.bean.WebSite;
import com.riant.mybatis.dao.WebSiteMapper;
import com.riant.mybatis.service.WebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname WebSiteServiceImpl
 * @Description TODO
 * @Date 2021/6/3 22:31
 * @Created by 张斌
 */
@Service("webSiteService")
public class WebSiteServiceImpl implements WebSiteService {

    @Autowired
    private WebSiteMapper webSiteMapper;

    @Override
    public WebSite getWebSite(int id) {
        return webSiteMapper.selectWebSiteById(id);
    }

    @Override
    public List<WebSite> getAllWebSite() {
        return webSiteMapper.selectAllWebSite();
    }

    /*
     * 这个方法中springboot分页插件pagehelper
     * 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
     * pageNum 开始页数
     * pageSize 每页显示的数据条数
     * */
    @Override
    public List<WebSite> getAllWebSite(int pageNum, int pageSize) {
        //将参数传给这个方法就可以实现物理分页了,非常简单。
        PageHelper.startPage(pageNum, pageSize);
        return webSiteMapper.selectAllWebSite();
    }

    @Override
    public Integer updateWebSite(WebSite webSite) {
        return webSiteMapper.updateWebSiteById(webSite);
    }

    @Override
    public Integer insertWebSite(WebSite webSite) {
        return webSiteMapper.insertWebSite(webSite);
    }

    @Override
    public Integer deleteWebSite(int id) {
        return webSiteMapper.deleteById(id);
    }
}

3、contoller层接口

package com.riant.mybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.riant.mybatis.bean.WebSite;
import com.riant.mybatis.service.WebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname WebSiteController
 * @Description TODO
 * @Date 2021/6/3 22:22
 * @Created by 张斌
 */
@RestController
@RequestMapping("/website")
public class WebSiteController {

    @Autowired
    private WebSiteService webSiteService;

    @RequestMapping(path = "/v1/{id}", method = RequestMethod.GET)
    public WebSite getWebSite(@PathVariable int id) {
        return webSiteService.getWebSite(id);
    }

    @RequestMapping(path = "/v1/website", method = RequestMethod.GET)
    public List<WebSite> getAllWebSite() {
        List<WebSite> list = webSiteService.getAllWebSite();
        return webSiteService.getAllWebSite();
    }

    @RequestMapping(path = "/v1/{pageNum}/{size}", method = RequestMethod.GET)
    public List<WebSite> getAllWebSite(@PathVariable int pageNum, @PathVariable int size) {
        List<WebSite> list = webSiteService.getAllWebSite(pageNum, size);
        return list;
    }

    @RequestMapping(path = "/v1/update", method = RequestMethod.POST)
    public JSONObject updateWebSite(@RequestBody WebSite webSite) {
        Integer result = webSiteService.updateWebSite(webSite);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("code", "000000");
            jsonObject.put("message", "SUCCESS");
        } else {
            jsonObject.put("code", "999999");
            jsonObject.put("message", "FAIL");
        }
        List<WebSite> list = webSiteService.getAllWebSite();
        return jsonObject;
    }

    @RequestMapping(path = "/v1/insert", method = RequestMethod.POST)
    public JSONObject insertWebSite(@RequestBody WebSite webSite) {
        Integer result = webSiteService.insertWebSite(webSite);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("code", "000000");
            jsonObject.put("message", "SUCCESS");
        } else {
            jsonObject.put("code", "999999");
            jsonObject.put("message", "FAIL");
        }
        return jsonObject;
    }
    @RequestMapping(path = "/v1/{id}", method = RequestMethod.DELETE)
    public JSONObject deleteWebSite(@PathVariable int id) {
        Integer result = webSiteService.deleteWebSite(id);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("code", "000000");
            jsonObject.put("message", "SUCCESS");
        } else {
            jsonObject.put("code", "999999");
            jsonObject.put("message", "FAIL");
        }
        return jsonObject;
    }
}

4、启动类

@SpringBootApplication
//表示要扫描的Mapper的目录路径是在com.riant.mybatis.dao下的。
@MapperScan("com.riant.mybatis.dao")
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }
}

五、测试

1、查询单条记录

在这里插入图片描述

2、查询多条记录

在这里插入图片描述

3、分页查询

在这里插入图片描述

4、插入记录

在这里插入图片描述

5、更新记录

在这里插入图片描述

6、删除记录

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2,ElementUI,SpringBootMybatisMySQL8.0是一组优秀的前后端技术组合。Vue2是一款流行的前端框架,具有响应式的数据绑定和组件化开发等特点,适合开发动态交互性强的单页面应用;ElementUI是一款基于Vue2的UI库,提供了一系列美观实用的组件,可快速搭建现代化的Web应用;SpringBoot是一款基于Spring框架的轻量级应用开发框架,使用简单,能够快速集成其他框架;Mybatis是一个优秀的ORM框架,能够极大地提高Java开发与SQL交互的效率,避免手写SQL语句的麻烦;MySQL8.0则是一个高性能、稳定性强的关系型数据库,使用广泛。 结合这些技术进行登陆注册系统的开发,可以使用Vue2和ElementUI实现前端页面的效果,使用SpringBoot作为后端框架,利用MybatisMySQL8.0数据库进行访问。具体开发过程可以分为以下几步: 1. 后端开发:使用SpringBoot框架搭建RESTful风格API接口,使用Mybatis框架访问MySQL8.0数据库,并实现用户登陆、注册以及对用户信息的增删改查等功能。 2. 前端开发:使用Vue2和ElementUI完成前端页面的搭建,包括登陆、注册、用户信息管理等页面,并使用Axios等技术与后端进行数据传输。可以使用Vuex实现数据的状态管理和共享。 3. 接口实现:在前后端开发完成后,需要将后端的API接口与前端进行对接,实现数据的交互。可以使用Postman等工具测试和调试接口。 4. 系统上线:在完成开发后,需要对系统进行测试和调试,确保系统能够稳定运行并满足用户需求后,再进行部署上线。 综上,登陆注册Vue2 ElementUI SpringBoot Mybatis MySQL8.0的开发过程相对复杂,需要前后端开发人员精细的协作和技术储备,但使用这些优秀的技术组合可以有效提高开发效率和用户体验,是一种切实可行的开发方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值