SpringBoot项目实战(001)mybatis通过xml实现mapper

说明

尝试做一个springboot的框架demo,不足之处,请留言指出,不胜感谢。
篇幅比较长,可以通过目录来找到需要的部分代码。

相关参考

以下是我的开发环境,仅做参考

开发

先做一个简单的增删改查,使用mybatis框架。之前写过注解版的《Mybatis 简单应用》,这里用的是xml配置版。

数据库

CREATE TABLE `db_laowu`.`Order` (
  `OrderId` INT NOT NULL AUTO_INCREMENT,
  `Code` VARCHAR(45) NOT NULL,
  `Name` VARCHAR(45) NOT NULL,
  `CreateTime` VARCHAR(45) NOT NULL,
  `LeftAmount` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`OrderId`));

CREATE TABLE `db_laowu`.`OrderDetail` (
  `OrderDetailId` INT NOT NULL AUTO_INCREMENT,
  `OrderId` INT NOT NULL,
  `ProductId` INT NOT NULL,
  `ProductCount` INT NOT NULL,
  PRIMARY KEY (`OrderDetailId`));  

创建项目

我是用vscode创建的,通过命令行:

  1. “spring Initializr: generate a maven project” 使用springboot工具创建maven项目
  2. “java” 选择语言
  3. “com.it_laowu.springboot-study” groupId
  4. “springboot-study-demo” artifactId
  5. “2.0.6” version,这个可以在pom里面改

依赖初步选择了以下,有缺的后面再加:

  • spring web
  • lombok
  • mysql
  • mybatis

项目分层

使用vscode的话,现在可以先分一下层,建一下文件夹,大致如下:
在这里插入图片描述

配置

application.properties改为application.yml

spring:
    application:
        name: springboot-study-demo
    profiles:
        active: dev
server:
    port: 1111
    servlet:
        context-path: /springboot-study-demo

新增application-dev.yml,包含一些环境特有的信息,这里存放下数据库链接

itlaowu:
    datasource:
        type: mysql
        //由于是容器内的mysql,这里用端口33060
        jdbc-url: jdbc:mysql://localhost:33060/db_laowu?useUnicode=true&characterEncoding=UTF-8
        username: it_laowu
        password: "!Aa123456"
        driver-class-name: com.mysql.jdbc.Driver

core

core/base下,我们存放一些基类。

BaseCondition 基础查询条件,包含排序和分页,分页放到下一章
package com.it_laowu.springbootstudy.springbootstudydemo.core.base;

import lombok.Data;

/**
 * 查询条件基类 不能直接指定sortSql,而是要校验安全性,再拼接sortType sortFields
 */
@Data
public abstract class BaseCondition {
   
    private String sortType;
    private String sortFields;
    private String sortSql;
    private int pageSize;
    private int currPageIndex;

    public String getSortSql() {
   
        if (sortFields != null && !sortFields.equals("") && !sortFields.equals("null")) {
   
            if ("asc,desc".indexOf(sortType.toLowerCase()) == -1) {
   
                return "";
            }
            final Class<?> childClass = getChildClass();
            for (String field : sortFields.split(",")) {
   
                try {
   
                    // 这里可以根据字段名,找到数据库对应的列名。本例简单认为两者相等。
                    childClass.getDeclaredField(field);
                } catch (NoSuchFieldException | SecurityException e) {
   
                    return "";
                }
            }
            return String.format(" ORDER BY %s %s ", sortFields, sortType);
        } else {
   
            return "";
        }
    }

    abstract public Class<?> getChildClass();
}
IBaseDao Dao层接口
package com.it_laowu.springbootstudy.springbootstudydemo.core.base;

import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface IBaseDao<Bean,Condition> {
   

    //全部查询,可以加入分页
    public List<Bean> findAll(@Param("conditionQC") Condition conditionQC);
    //该方法不对外,容易出错
    Bean findOne(@Param("keyId") int keyId);
    public int insert(Bean entity);
    public int update(Bean entity);
    public int delete(@Param("keyId") int keyId);
}
IBaseService Service接口
package com.it_laowu.springbootstudy.springbootstudydemo.core.base;

import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface IBaseService<Bean,Condition> {
   

    //内部方法
    IBaseDao<Bean,Condition> getBaseDao
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值