springboot简单的增删改查

本文档详细介绍了如何使用SpringBoot、Lombok、Thymeleaf和MyBatis进行简单的增删改查操作。从创建项目、配置依赖、编写实体类、DAO接口、Service层到Controller层的实现,每个步骤都有清晰的说明,旨在记录作者的学习过程。此外,还提到了自定义配置类扩展Spring Boot的SpringMVC功能以及前端使用Bootstrap框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot+lombok+thymeleaf+mybais简单的写一个增删改查方法,前端是bootstrap框架
此文章只作为记录自己的学习,关于命名规则问题我随心,仅供参考

1.创建项目选中Web、Thymeleaf、MySql依赖,然后额外导入lombok、数据源Druid、以及通用mapper依赖,

<!--阿里druid数据源启动器-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.1</version>
    </dependency>
    <!--通用mapper常见启动器-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.1.5</version>
    </dependency>

2.在src/main/resources下创建application.yml文件,配置mybatis

```xmlspring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/durgwh?serverTimezone=UTC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    
    # 数据源其它配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
    filters: stat,wall
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true  
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  thymeleaf:
    cache: false
#MyBatis相关配置
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.bt.entity

#日志级别
logging:
  level:
    com.bt: debug

3.我们可以通过自定义配置类来扩展Spring boot的springmvc功能,我们只需要写一个配置类实现WebMvcConfigurer即可,我们创建com.bt.config包并且创建类MvcConfig,示例如下:目的直接去某个页面时不用去controller里去写请求

  @Configuration
public class MvcConfig implements WebMvcConfigurer {
   
  //扩展视图控制器映射器
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
   
        //浏览器发送/welcome请求 到达welcome页面
        registry.addViewController("/welcome").setViewName("login");
        registry.addViewController("/main").setViewName("main");
        registry.addViewController("/druglist").setViewName("druglist");
        registry.addViewController("/addinslist").setViewName("/addinslist");
    }

4.创建包com.bt.entity,并创建Instrument实体类,注意导包别导错。

package com.bt.entity;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Data
@Table(name="instrument")
public class Instrument {
   
    @Id
    @KeySql(useGeneratedKeys = true)
    private  Integer id;
    private String name;
    private String type;
    private Integer number;
    @DateTimeFormat(pattern =" yyyy-mm-dd")
    private Date datemanufacture;
    private String probatch;
    @DateTimeFormat(pattern =" yyyy-mm-dd")
    private Date warehousdate;
    private String position;
    private String pic;
}

5.编写com.bt.dao.mapper接口(注意mapper的注解,以及导包问题,一定要到tk下的mybatis的Mapper)

package com.bt.dao;

import com.bt.entity.Instrument;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface InstrumentMapper extends Mapper<Instrument>{
   
    List<Instrument> getListByCon(Instrument instrument);
}

6.编写模糊查sql的xml文件(注意:namespace命名空间是dao层的映射mapper地址,注意id必须和dao层的方法相同)

<mapper namespace="com.bt.dao.InstrumentMapper">
  <!--查询所有 -->
  <select id="getListByCon" resultType="instrument" parameterType="instrument">
    select * from instrument
    <trim prefix="where" prefixOverrides="and || or">
      <if test="name!=null and name!=''">and name like concat('%',#{name},'%')</if>
      <if test="position!=null and position!=''">and position=#{position}</if>
    </trim>
  </select>
</mapper>

7.Service接口

   //模糊查
    List<Instrument> getListByCon(Instrument instrument);
    //查看详情
    Instrument selectByIns(Integer id);
    //修改
    int updateByIns(Instrument instrument);
    //增加
    int addIns(Instrument instrument);
    //删除
    int delIns(Integer id);

8.Service的实现类

@Service
public class InstrumentServiceImpl implements InstrumentService {
   
@Autowired
private InstrumentMapper mapper;
    @Override
    public List<Instrument> getListByCon(Instrument instrument) {
   
        return mapper.getListByCon(instrument);
    }

    @Override
    public Instrument selectByIns(Integer id) {
   
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByIns(Instrument instrument) {
   
        return mapper.updateByPrimaryKeySelective(instrument);
    }

    @Override
    public int addIns(Instrument instrument) {
   
        return mapper.insertSelective(instrument);
    }

    @
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值