一个完整的springboot+mybatis的项目demo

表结构如下:

CREATE TABLE `sys_dict` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `dict_key` varchar(64) NOT NULL,
  `dict_value` varchar(512) NOT NULL,
  `label` varchar(32) NOT NULL DEFAULT 'common' COMMENT '数据标签,默认为common',
  `username` varchar(64) NOT NULL,
  `create_time` datetime NOT NULL,
  `comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `key_idx` (`dict_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

是基于表结构sys_dict的Spring Boot和MyBatis注解方式的增删改查功能实现。

1. POM文件

确保pom.xml包含以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

2. 配置文件

配置数据库连接信息,在application.properties文件中:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.type-aliases-package=com.example.demo.model
mybatis.mapper-locations=classpath:mappers/*.xml

3. 创建实体类

创建实体类SysDict

package com.example.demo.model;

import java.util.Date;

import lombok.Data;

@Data
public class SysDict {
    private Long id;
    private String dictKey;
    private String dictValue;
    private String label;
    private String username;
    private Date createTime;
    private String comment;
}

4. 创建Mapper接口

创建Mapper接口SysDictMapper

package com.example.demo.mapper;

import com.example.demo.model.SysDict;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysDictMapper {

    @Select("SELECT * FROM sys_dict WHERE id = #{id}")
    SysDict getSysDictById(Long id);

    @Select("SELECT * FROM sys_dict")
    List<SysDict> getAllSysDicts();

    @Insert("INSERT INTO sys_dict(dict_key, dict_value, label, username, create_time, comment) " +
            "VALUES(#{dictKey}, #{dictValue}, #{label}, #{username}, #{createTime}, #{comment})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertSysDict(SysDict sysDict);

    @Update("UPDATE sys_dict SET dict_key=#{dictKey}, dict_value=#{dictValue}, label=#{label}, " +
            "username=#{username}, create_time=#{createTime}, comment=#{comment} WHERE id=#{id}")
    void updateSysDict(SysDict sysDict);

    @Delete("DELETE FROM sys_dict WHERE id = #{id}")
    void deleteSysDict(Long id);
}

5. 创建Service类

创建Service类SysDictService

package com.example.demo.service;

import com.example.demo.mapper.SysDictMapper;
import com.example.demo.model.SysDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    public SysDict getSysDictById(Long id) {
        return sysDictMapper.getSysDictById(id);
    }

    public List<SysDict> getAllSysDicts() {
        return sysDictMapper.getAllSysDicts();
    }

    public void insertSysDict(SysDict sysDict) {
        sysDictMapper.insertSysDict(sysDict);
    }

    public void updateSysDict(SysDict sysDict) {
        sysDictMapper.updateSysDict(sysDict);
    }

    public void deleteSysDict(Long id) {
        sysDictMapper.deleteSysDict(id);
    }
}

6. 创建Controller类

创建Controller类SysDictController

package com.example.demo.controller;

import com.example.demo.model.SysDict;
import com.example.demo.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysdict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @GetMapping("/getById")
    public SysDict getSysDictById(@RequestParam Long id) {
        return sysDictService.getSysDictById(id);
    }

    @GetMapping("/getAll")
    public List<SysDict> getAllSysDicts() {
        return sysDictService.getAllSysDicts();
    }

    @PostMapping("/create")
    public void createSysDict(@RequestParam String dictKey,
                              @RequestParam String dictValue,
                              @RequestParam String label,
                              @RequestParam String username,
                              @RequestParam String comment) {
        SysDict sysDict = new SysDict();
        sysDict.setDictKey(dictKey);
        sysDict.setDictValue(dictValue);
        sysDict.setLabel(label);
        sysDict.setUsername(username);
        sysDict.setCreateTime(new Date());
        sysDict.setComment(comment);
        sysDictService.insertSysDict(sysDict);
    }

    @PostMapping("/update")
    public void updateSysDict(@RequestParam Long id,
                              @RequestParam String dictKey,
                              @RequestParam String dictValue,
                              @RequestParam String label,
                              @RequestParam String username,
                              @RequestParam String comment) {
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        sysDict.setDictKey(dictKey);
        sysDict.setDictValue(dictValue);
        sysDict.setLabel(label);
        sysDict.setUsername(username);
        sysDict.setCreateTime(new Date());
        sysDict.setComment(comment);
        sysDictService.updateSysDict(sysDict);
    }

    @GetMapping("/delete")
    public void deleteSysDict(@RequestParam Long id) {
        sysDictService.deleteSysDict(id);
    }
}

7. Application启动类

确保Spring Boot应用启动类如下:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

这套代码实现了对sys_dict表的基本增删改查功能,调整了HTTP请求方式和参数传递方式,并且使用了Spring Boot和MyBatis注解方式。请根据你的具体项目结构和需求进行适当的调整和测试。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring BootMyBatis结合使用的项目可以通过以下步骤来创建: 1. 在Spring Boot中添加MyBatis依赖。可以在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置MyBatis。可以在application.properties或application.yml文件中添加以下配置: ``` mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.domain ``` 3. 创建实体类和映射文件。可以创建一个包来保存实体类和映射文件,例如: ``` com.example.demo.domain ├─ User.java └─ UserMapper.xml ``` 4. 创建Mapper接口。Mapper接口是用来调用映射文件中定义的SQL语句的,例如: ``` package com.example.demo.mapper; import com.example.demo.domain.User; public interface UserMapper { User selectUserById(int id); } ``` 5. 创建Service层。Service层是用来调用Mapper接口中定义的方法的,例如: ``` package com.example.demo.service; import com.example.demo.domain.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(int id) { return userMapper.selectUserById(id); } } ``` 6. 创建Controller层。Controller层是用来处理HTTP请求的,例如: ``` package com.example.demo.controller; import com.example.demo.domain.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users/{id}") public User getUserById(@PathVariable("id") int id) { return userService.getUserById(id); } } ``` 以上就是一个基本的Spring BootMyBatis结合使用的项目的搭建过程,可以根据自己的实际需求进行进一步的开发和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值