网站怎么做响应式布局,设计师网址导航sdc,文大侠seo,2023中关村手机排行榜继续使用以简单的例子从头开始建spring boot web多模块项目#xff08;一#xff09;中的项目进行mybatis集成。 1、pom.xml文件中#xff0c;增加相关的依赖包的引入#xff0c;分别是mybatis-spring-boot-starter、lombok、mysql-connector-java 如下#xff1a;
d…继续使用以简单的例子从头开始建spring boot web多模块项目一中的项目进行mybatis集成。 1、pom.xml文件中增加相关的依赖包的引入分别是mybatis-spring-boot-starter、lombok、mysql-connector-java 如下
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.3.1/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.33/version/dependency2、修改application.properties
server.port 8081
spring.datasource.url jdbc:mysql://localhost:3306/test?serverTimezoneGMT%2B8characterEncodingutf-8useSSLfalse
spring.datasource.username root
spring.datasource.password
spring.datasource.driver-class-name com.mysql.cj.jdbc.Driver
mybatis.mapper-locations classpath:mapper/*.xml3、安装插件better-mybatis-generator之类的可以帮助生成mapper、entity、*mapper.xml等相关文件也可以进行手工添加。 表结构如下 创建脚本
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)
) ENGINEInnoDB AUTO_INCREMENT1;
新增相应的包及xml的目录entity、mapper、service、service\impl 实体Warehouse 类
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
package org.rainpet.mapper;import org.apache.ibatis.annotations.Mapper;
import org.rainpet.entity.Warehouse;import java.util.List;Mapper
public interface WarehouseMapper {ListWarehouse getList();
}Mapper文件WarehouseMapper.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.rainpet.mapper.WarehouseMapperresultMap idBaseResultMap typeorg.rainpet.entity.Warehouseid columnid jdbcTypeINTEGER propertyid /result columnfid jdbcTypeVARCHAR propertyfid /result columnfname jdbcTypeVARCHAR propertyfname /result columnfused jdbcTypeTINYINT propertyfused /result columnfclass jdbcTypeTINYINT propertyfclass /result columnis_group jdbcTypeTINYINT propertyisGroup /result columnis_negative jdbcTypeTINYINT propertyisNegative /result columnin_process jdbcTypeVARCHAR propertyinProcess /result columnfitemid jdbcTypeINTEGER propertyfitemid /result columnFSyncModifyTime jdbcTypeBIGINT propertyfsyncmodifytime /result columnforbidden jdbcTypeTINYINT propertyforbidden /result columnFModifyTime jdbcTypeTIMESTAMP propertyfmodifytime //resultMapselect idgetList resultMapBaseResultMapselect * from warehouse/select
/mapper服务WarehouseService
package org.rainpet.service;import org.rainpet.entity.Warehouse;import java.util.List;public interface WarehouseService {ListWarehouse getList();
}
服务实现类WarehouseServiceImpl
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 {AutowiredWarehouseMapper warehouseMapper;public ListWarehouse getList() {return warehouseMapper.getList();}
}
控制器类DemoController
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 {AutowiredWarehouseService warehouseService;ResponseBodyGetMapping()public String index(){return hello!;}ResponseBodyGetMapping(name)public String name(RequestParam(namename,required false,defaultValue 张三)String name){ListWarehouse warehouseList warehouseService.getList();return warehouseList.toString();// return hello name;}
}
4、Main.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?namezhangsa来访问结果 6、控制器中新增一个方法name2如下
GetMapping(name2)ResponseBodypublic ListWarehouse name2(){ListWarehouse warehouseList warehouseService.getList();return warehouseList;}springboot默认集成了gjson,访问 http://localhost:8081/demo/name2,即可以直接拿到json。 7、在org.rainpet包下新建一个类WebMvcConfig 内容如下
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 {BeanObjectMapper objectMapper() {ObjectMapper om new ObjectMapper();om.setDateFormat(new SimpleDateFormat(yyyy-MM-dd));return om;}
}
即可改写日期格式
如下