微信小程序开发:利用Spring Boot, Mybatis, MySQL 实现数据传输以及数据增删改查

微信小程序开发:利用Spring Boot, Mybatis, MySQL 实现数据传输以及数据增删改查

项目结构截图:

在这里插入图片描述

controller:

package com.glxy.canjian.controller;
import com.glxy.canjian.pojo.Business_index;
import com.glxy.canjian.service.BusinessIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BusinessIndexController {

    @Autowired
    private BusinessIndexService businessIndexService;

    @RequestMapping("/select")
    public Business_index findById(@RequestParam(value = "business_id") int business_id){
        return businessIndexService.findById(business_id);
    }

    @RequestMapping("/getList")
    public List<Business_index> findAll(){
        return businessIndexService.findAll();
    }

    @RequestMapping("/add")
    public void addBusiness_index(Business_index business_index){
        businessIndexService.addBusiness_index(business_index);
    }

    @RequestMapping("/delete")
    public int deleteBusiness_index(int business_id){
        return businessIndexService.deleteBusiness_index(business_id);
    }

    @RequestMapping("/update")
    public void updateBusiness_index(Business_index business_index){
        businessIndexService.updateBusiness_index(business_index);
    }
}

mapper(java):

package com.glxy.canjian.mapper;

import com.glxy.canjian.pojo.Business_index;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface Business_indexMapper {
    List<Business_index> findAll();
    Business_index findById(int business_id);
    void addBusiness_index(Business_index business_index);
    int deleteBusiness_index(int business_id);
    void updateBusiness_index(Business_index business_index);
}

pojo:

package com.glxy.canjian.pojo;


public class Business_index  {
    private int rest_id;
    private  String rest_img;
    private int business_id;
    private String business_name;
    private String business_img;
    private Long business_score_total;
    private String business_introduction_short;

    public Business_index() {
    }

    public int getRest_id() {
        return rest_id;
    }

    public void setRest_id(Long id) {
        this.rest_id = rest_id;
    }

    public String getRest_img() {
        return rest_img;
    }

    public void setRest_img(String rest_img) {
        this.rest_img = rest_img;
    }

    public int getBusiness_id() {
        return business_id;
    }

    public void setBusiness_id(int business_id) {
        this.business_id = business_id;
    }

    public String getBusiness_name() {
        return business_name;
    }

    public void setBusiness_name(String business_name) {
        this.business_name = business_name;
    }

    public String getBusiness_img() {
        return business_img;
    }

    public void setBusiness_img(String business_img) {
        this.business_img = business_img;
    }

    public Long getBusiness_score_total() {
        return business_score_total;
    }

    public void setBusiness_score_total(Long business_score_total) {
        this.business_score_total = business_score_total;
    }

    public String getBusiness_introduction_short() {
        return business_introduction_short;
    }

    public void setBusiness_introduction_short(String business_introduction_short) {
        this.business_introduction_short = business_introduction_short;
    }
}

service:

package com.glxy.canjian.service;

import com.glxy.canjian.pojo.Business_index;

import java.util.List;


public interface BusinessIndexService {
    List<Business_index> findAll();
    Business_index findById(int business_id);
    void addBusiness_index(Business_index business_index);
    int deleteBusiness_index(int id);
    void updateBusiness_index(Business_index business_index);
}

implement:

package com.glxy.canjian.service.impl;

import com.glxy.canjian.mapper.Business_indexMapper;
import com.glxy.canjian.pojo.Business_index;
import com.glxy.canjian.service.BusinessIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class BusinessIndexServiceImpl implements BusinessIndexService {

    @Autowired
    private Business_indexMapper business_indexMapper;

    @Override
    public Business_index findById(@RequestParam(value = "business_id") int business_id) {
        return business_indexMapper.findById(business_id);
    }

    @Override
    public List<Business_index> findAll() {
        return business_indexMapper.findAll();
    }

    @Override
    public void addBusiness_index(Business_index business_index) {
        business_indexMapper.addBusiness_index(business_index);
    }

    @Override
    public int deleteBusiness_index(int id) {
        return business_indexMapper.deleteBusiness_index(id);
    }

    @Override
    public void updateBusiness_index(Business_index business_index){
        business_indexMapper.updateBusiness_index(business_index);
    }
}

mybatis.config:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.-//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <typeAliases>
        <package name="com.glxy.canjian.pojo"/>
    </typeAliases>
</configuration>

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">

<mapper namespace="com.glxy.canjian.mapper.Business_indexMapper">
    <resultMap id="BaseResultMap" type="com.glxy.canjian.pojo.Business_index">
        <result column="rest_id" property="rest_id"/>
        <result column="rest_img" property="rest_img"/>
        <result column="business_id" property="business_id"/>
        <result column="business_name" property="business_name"/>
        <result column="business_img" property="business_img"/>
        <result column="business_score_total" property="business_score_total"/>
        <result column="business_introduction_short" property="business_introduction_short"/>
    </resultMap>

    <select id="findById" resultMap="BaseResultMap">
        select * from business_index where business_id =#{business_id}
    </select>

    <select id="findAll" resultMap="BaseResultMap">
        select * from business_index
    </select>

    <insert id="addBusiness_index" parameterType="com.glxy.canjian.pojo.Business_index" keyProperty="business_id">
        INSERT INTO business_index (rest_id, rest_img, business_id, business_name, business_img, business_score_total, business_introduction_short)
        VALUES (
        #{rest_id},
        #{rest_img,jdbcType=VARCHAR},
        #{business_id},
        #{business_name,jdbcType=VARCHAR},
        #{business_img,jdbcType=VARCHAR},
        #{business_score_total},
        #{business_introduction_short,jdbcType=VARCHAR})
    </insert>

    <delete id="deleteBusiness_index" parameterType="int">
        delete from business_index where business_id = #{business_id};
    </delete>

    <update id="updateBusiness_index" parameterType="com.glxy.canjian.pojo.Business_index">
        update business_index set
        rest_id=#{rest_id},
        rest_img=#{rest_img,jdbcType=VARCHAR},
        business_name=#{business_name,jdbcType=VARCHAR},
        business_img=#{business_img,jdbcType=VARCHAR},
        business_score_total=#{business_score_total},
        business_introduction_short=#{business_introduction_short,jdbcType=VARCHAR}
        where business_id=#{business_id}
    </update>
    

</mapper>
注意点:

本人在调试过程中频繁报错如下:在这里插入图片描述
经过长时间的尝试后发现是SQL语句的语法错误 T_T希望带伙应以为戒。若表内元素是int类型,后面是不用加jdbcType=INT的,虽然不知道原因,但去掉就可以正常运行了。

pom.xml配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.glxy</groupId>
    <artifactId>canjian</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>canjian</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>compile</scope>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

数据库截图:

在这里插入图片描述
在这里插入图片描述

运行截图:在这里插入图片描述

参考文章链接:https://www.jianshu.com/p/55f4fbfe6759
萌新摸索代码中,若有不足之处欢迎各位大佬指出!!
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值