结合SpringBoot+Mybatis+MySQL+FreeMarker实现增删改查

目录

1.新建一个SpringBoot项目

2.数据库设计

3.项目整体目录结构

4.在pom.xml中添加Mybatis、MySQL、FreeMarker等的starter

5.在application.properties中添加数据库、Mybatis、FreeMarker等的相关配置

6.在SpringBoot启动类MallApplication配置扫描mapper接口类

7.实现增删改查

8.页面展示

9.使用Git将项目上传到GitHub

10.使用git-commit-id-plugin插件

11.自定义starter读取git.properties

12.完整代码


1.新建一个SpringBoot项目

(1)File--->New--->Project

(2)选择项目工具和JDK版本

(3)填写项目信息

(4)选择Web依赖包

(5)填写项目名称和项目保存地址

(6)搭建好的目录结构

2.数据库设计

3.项目整体目录结构

4.在pom.xml中添加Mybatis、MySQL、FreeMarker等的starter

        <!-- spring boot mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>

        <!-- mysql连接驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>

        <!-- spring boot freemarker依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

5.在application.properties中添加数据库、Mybatis、FreeMarker等的相关配置

## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/testproject?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Mybatis配置
## 实体类包路径
mybatis.typeAliasesPackage=org.mall.domain
## mapper.xml路径
mybatis.mapperLocations=classpath:mapper/*.xml

## Freemarker 配置
## 文件配置路径
spring.freemarker.template-loader-path=classpath:/web/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
## 获取contextPath
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

6.在SpringBoot启动类MallApplication配置扫描mapper接口类

package org.mall;

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

//Spring Boot应用的标识
@SpringBootApplication
//mapper接口类扫描包配置,用这个注解可以注册mybatis mapper接口类
@MapperScan("org.mall.dao")
public class MallApplication {
    public static void main(String[] args) {
        // 程序启动入口,启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
        SpringApplication.run(MallApplication.class,args);
    }
}

7.实现增删改查

(1)Commodity 实体类

package org.mall.domain;

/*
* 商品实体类
* */
public class Commodity {
    //主键id
    private Integer id;
    //商品名称
    private String name;
    //商品规格
    private String standard;
    //商品温度
    private String temperature;
    //商品价格
    private int price;
    //商品描述
    private String description;

    //省略getter、setter方法
    ......
}

(2)CommodityDao接口类

package org.mall.dao;

import org.mall.domain.Commodity;

import java.util.List;

/*
* 商品DAO
* */
public interface CommodityDao {
    //获取全部商品
    List<Commodity> findAll();

    //新增商品
    void insertCommodity(Commodity commodity);

    //修改商品
    void updateCommodity(Commodity commodity);

    //根据id获取商品
    Commodity findById(Integer id);

    //根据id删除商品
    void deleteById(Integer id);
}

(3)CommodityService接口

package org.mall.service;

import org.mall.domain.Commodity;

import java.util.List;

/*
* 商品Service接口
* */
public interface CommodityService {
    //获取全部商品信息
    List<Commodity> findAll();

    //新增商品信息
    void insertCommodity(Commodity commodity);

    //修改商品信息
    void updateCommodity(Commodity commodity);

    //根据id查询获取商品信息
    Commodity findById(Integer id);

    //根据id删除商品信息
    void deleteById(Integer id);
}

(4)CommodityService接口实现类

package org.mall.service.impl;

import org.mall.dao.CommodityDao;
import org.mall.domain.Commodity;
import org.mall.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
* 商品Service实现类
* */
@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private CommodityDao commodityDao;

    //获取全部商品
    public List<Commodity> findAll(){
        return commodityDao.findAll();
    }

    //新增商品
    public void insertCommodity(Commodity commodity){
        commodityDao.insertCommodity(commodity);
    }

    //修改商品
    public void updateCommodity(Commodity commodity){
        commodityDao.updateCommodity(commodity);
    }

    //根据id获取商品
    public Commodity findById(Integer id){
        return commodityDao.findById(id);
    }

    //根据id删除商品
    public void deleteById(Integer id){
        commodityDao.deleteById(id);
    }
}

(5)Commodity控制层

注:在控制层使用了Swagger实现接口说明,具体使用看上篇博

  • 9
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值