以简单的例子从头开始建spring boot web多模块项目(二)-mybatis简单集成

继续使用以简单的例子从头开始建spring boot web多模块项目(一)中的项目进行mybatis集成。

1、pom.xml文件中,增加相关的依赖包的引入,分别是mybatis-spring-boot-starter、lombok、mysql-connector-java

如下:

xml 复制代码
<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>

2、修改application.properties

xml 复制代码
server.port= 8081
spring.datasource.url = jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
mybatis.mapper-locations = classpath:mapper/*.xml

3、安装插件better-mybatis-generator之类的可以帮助生成mapper、entity、*mapper.xml等相关文件,也可以进行手工添加。

表结构如下:

创建脚本:

sql 复制代码
CREATE TABLE `warehouse` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fid` varchar(25)  NOT NULL,
  `fname` varchar(45) NOT NULL DEFAULT '',
  `fused` tinyint NOT NULL DEFAULT '0',
  `fclass` tinyint NOT NULL DEFAULT '0',
  `is_group` tinyint NOT NULL DEFAULT '0',
  `is_negative` tinyint unsigned NOT NULL DEFAULT '0',
  `in_process` varchar(1000) DEFAULT '' COMMENT '在产产品',
  `fitemid` int unsigned DEFAULT '0',
  `FSyncModifyTime` bigint DEFAULT '0' COMMENT '同步数据修改标记',
  `forbidden` tinyint DEFAULT '0' COMMENT '是否禁用',
  `FModifyTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1;

新增相应的包及xml的目录entity、mapper、service、service\impl

实体Warehouse 类

java 复制代码
package org.rainpet.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class Warehouse implements Serializable {
    private Integer id;

    private String fid;

    private String fname;

    private Byte fused;

    private Byte fclass;

    private Byte isGroup;

    private Byte isNegative;

    /**
     * 在产产品
     */
    private String inProcess;

    private Integer fitemid;

    /**
     * 同步数据修改标记
     */
    private Long fsyncmodifytime;

    /**
     * 是否禁用
     */
    private Byte forbidden;

    /**
     * 修改时间
     */
    private Date fmodifytime;

}

mapper文件WarehouseMapper

java 复制代码
package org.rainpet.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.rainpet.entity.Warehouse;

import java.util.List;

@Mapper
public interface WarehouseMapper {
    List<Warehouse> getList();
}

Mapper文件WarehouseMapper.xml

java 复制代码
<?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="org.rainpet.mapper.WarehouseMapper">
    <resultMap id="BaseResultMap" type="org.rainpet.entity.Warehouse">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="fid" jdbcType="VARCHAR" property="fid" />
        <result column="fname" jdbcType="VARCHAR" property="fname" />
        <result column="fused" jdbcType="TINYINT" property="fused" />
        <result column="fclass" jdbcType="TINYINT" property="fclass" />
        <result column="is_group" jdbcType="TINYINT" property="isGroup" />
        <result column="is_negative" jdbcType="TINYINT" property="isNegative" />
        <result column="in_process" jdbcType="VARCHAR" property="inProcess" />
        <result column="fitemid" jdbcType="INTEGER" property="fitemid" />
        <result column="FSyncModifyTime" jdbcType="BIGINT" property="fsyncmodifytime" />
        <result column="forbidden" jdbcType="TINYINT" property="forbidden" />
        <result column="FModifyTime" jdbcType="TIMESTAMP" property="fmodifytime" />
    </resultMap>
    <select id="getList" resultMap="BaseResultMap">
        select * from warehouse
    </select>
</mapper>

服务WarehouseService

java 复制代码
package org.rainpet.service;

import org.rainpet.entity.Warehouse;

import java.util.List;

public interface WarehouseService {
    List<Warehouse> getList();
}

服务实现类WarehouseServiceImpl

java 复制代码
package org.rainpet.service.impl;

import org.rainpet.entity.Warehouse;
import org.rainpet.mapper.WarehouseMapper;
import org.rainpet.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Autowired
    WarehouseMapper warehouseMapper;
    public List<Warehouse> getList() {
        return warehouseMapper.getList();
    }
}

控制器类DemoController

java 复制代码
package org.rainpet.controller;

import org.rainpet.entity.Warehouse;
import org.rainpet.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    WarehouseService warehouseService;
    @ResponseBody
    @GetMapping("")
    public String index(){
        return "hello!";
    }

    @ResponseBody
    @GetMapping("name")
    public String name(@RequestParam(name="name",required = false,defaultValue = "张三")String name){
        List<Warehouse> warehouseList= warehouseService.getList();
        return warehouseList.toString();
        // return "hello "+name;
    }
}

4、Main.java文件

java 复制代码
package org.rainpet;

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

@SpringBootApplication
@MapperScan(basePackages = "org.rainpet.mapper")
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(org.rainpet.Main.class,args);
        System.out.println("Hello world!");
    }
}

5、最终通过http://localhost:8081/demo/name?name=zhangsa来访问结果

6、控制器中,新增一个方法,name2,如下:

java 复制代码
@GetMapping("name2")
    @ResponseBody
    public List<Warehouse> name2(){
        List<Warehouse> warehouseList= warehouseService.getList();
        return warehouseList;
    }

springboot默认集成了Jackson,访问 http://localhost:8081/demo/name2,即可以直接拿到json。

7、在org.rainpet包下新建一个类:WebMvcConfig

内容如下:

java 复制代码
package org.rainpet;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;

@Configuration
public class WebMvcConfig {
    @Bean
    ObjectMapper objectMapper() {
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        return om;
    }
}

即可改写日期格式

如下:

相关推荐
猫头虎10 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
程序员侠客行12 小时前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
MZ_ZXD00112 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
invicinble12 小时前
springboot的核心实现机制原理
java·spring boot·后端
space621232713 小时前
在SpringBoot项目中集成MongoDB
spring boot·后端·mongodb
金牌归来发现妻女流落街头14 小时前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
皮卡丘不断更15 小时前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
lucky670715 小时前
Spring Boot集成Kafka:最佳实践与详细指南
spring boot·kafka·linq
Coder_Boy_15 小时前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
老毛肚16 小时前
手写mybatis
java·数据库·mybatis