Springboot-mybatis-plus Springboot整合mybatisplus详细篇+数据库+代码自动生成

直接上代码,目录在后面:

1 pom.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gf</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>


2:application.xml 这里也可以是yml文件 不多说

#spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.url=jdbc:mysql://192.168.110.208:3306/tb_web_ir?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.type-aliases-package=com.gf.mapper
mybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis-plus.configuration.map-underscore-to-camel-case=true


idea 安装MybatisX 插件  ,连接数据库  

 

 

 

 

 

 

 

 

 


controller
package com.gf.controller;


import com.gf.entity.BTbEpc;
import com.gf.entity.Employee;
import com.gf.service.BTbEpcService;
import com.gf.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* 前端控制器
* </p>
*
* @author jyc
* @since 2018-11-25
*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {


@Autowired
EmployeeService employeeService;

@Autowired
BTbEpcService bTbEpcService;

@RequestMapping("btb")
public List<BTbEpc> getBTbEpcs(){
return bTbEpcService.list(null);

}

//localhost
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Employee> getEmployees() {
return employeeService.list( null );
}
//localhost
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Employee getEmployeeById(@PathVariable("id") int id) {
return employeeService.getById( id );
}
//localhost
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String updateEmployee(@PathVariable("id") int id, @RequestParam(value = "lastName", required = true) String lastName,
@RequestParam(value = "email", required = true) String email , @RequestParam(value = "gender", required = true) int gender , @RequestParam(value = "dId", required = true) int dId) {
Employee employee = new Employee();
employee.setId( id );
employee.setLastName( "" );
employee.setEmail( "zhang@163.com" );
employee.setGender( 1 );
employee.setDId( 1 );

boolean b = employeeService.updateById( employee );

if (b) {
return "update success";
} else {
return "update fail";
}

}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable(value = "id")int id) {
boolean b = employeeService.removeById( id );

if(b) {
return "delete success";
}else {
return "delete fail";
}

}

@RequestMapping(value = "", method = RequestMethod.POST)
public String postEmployee(@RequestParam(value = "lastName", required = true) String lastName,
@RequestParam(value = "email", required = true) String email , @RequestParam(value = "gender", required = true) int gender , @RequestParam(value = "dId", required = true) int dId) {

Employee employee = new Employee();
employee.setLastName( "" );
employee.setEmail( "wang@163.com" );
employee.setGender( 2 );
employee.setDId( 2 );
boolean b = employeeService.save( employee );

if(b) {
return "sava success";
}else {
return "sava fail";
}

}


}


entity

package com.gf.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
* 电力公司信息表(省级、市级、区级)<epc: electric power company>
* @TableName b_tb_epc
*/
@TableName(value ="b_tb_epc")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class BTbEpc implements Serializable {
/**
* 电力公司ID
*/
@TableId(value = "epc_id")
private Long epcId;

/**
* 电力公司父级ID(最高级别:0
*/
@TableField(value = "epc_parent_id")
private Long epcParentId;

/**
* 电力公司编码
*/
@TableField(value = "epc_code")
private String epcCode;

/**
* 电力公司名称
*/
@TableField(value = "epc_name")
private String epcName;

/**
* 电力公司级别(1-2-3 --/)
*/
@TableField(value = "epc_level")
private Integer epcLevel;

/**
* 变电检测记录是否存在(1:存在)
*/
@TableField(value = "b_exist")
private Boolean bExist;

/**
* 输电检测记录是否存在(1:存在)
*/
@TableField(value = "s_exist")
private Boolean sExist;

/**
* 配电检测记录是否存在(1:存在)
*/
@TableField(value = "p_exist")
private Boolean pExist;

/**
* 所属地市ID
*/
@TableField(value = "region_id")
private Long regionId;

/**
* epc访问路径
*/
@TableField(value = "epc_route_path")
private String epcRoutePath;

/**
* epc访问路径ID
*/
@TableField(value = "epc_route_id")
private String epcRouteId;

/**
* reg访问路径
*/
@TableField(value = "reg_route_path")
private String regRoutePath;

/**
* reg访问路径ID
*/
@TableField(value = "reg_route_id")
private String regRouteId;

/**
* 排序
*/
@TableField(value = "sorted")
private Integer sorted;

/**
* 创建时间
*/
@TableField(value = "create_time")
private Date createTime;

/**
* 创建人
*/
@TableField(value = "creator")
private Long creator;

/**
* 更新时间
*/
@TableField(value = "update_time")
private Date updateTime;

/**
* 更新人
*/
@TableField(value = "updater")
private Long updater;

/**
* 软删除(1:已删除)
*/
@TableField(value = "deleted")
private Boolean deleted;

/**
* 乐观锁版本号
*/
@TableField(value = "version")
private Integer version;

/**
* 备注
*/
@TableField(value = "remark")
private String remark;

@TableField(exist = false)
private static final long serialVersionUID = 1L;



@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", epcId=").append(epcId);
sb.append(", epcParentId=").append(epcParentId);
sb.append(", epcCode=").append(epcCode);
sb.append(", epcName=").append(epcName);
sb.append(", epcLevel=").append(epcLevel);
sb.append(", bExist=").append(bExist);
sb.append(", sExist=").append(sExist);
sb.append(", pExist=").append(pExist);
sb.append(", regionId=").append(regionId);
sb.append(", epcRoutePath=").append(epcRoutePath);
sb.append(", epcRouteId=").append(epcRouteId);
sb.append(", regRoutePath=").append(regRoutePath);
sb.append(", regRouteId=").append(regRouteId);
sb.append(", sorted=").append(sorted);
sb.append(", createTime=").append(createTime);
sb.append(", creator=").append(creator);
sb.append(", updateTime=").append(updateTime);
sb.append(", updater=").append(updater);
sb.append(", deleted=").append(deleted);
sb.append(", version=").append(version);
sb.append(", remark=").append(remark);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
 


mapper
package com.gf.mapper;

import com.gf.entity.BTbEpc;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @Entity com.gf.entity.BTbEpc
*/
//@Mapper
public interface BTbEpcMapper extends BaseMapper<BTbEpc> {

}






service
package com.gf.service;

import com.gf.entity.BTbEpc;
import com.baomidou.mybatisplus.extension.service.IService;

/**
*
*/
public interface BTbEpcService extends IService<BTbEpc> {

}

impl
package com.gf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gf.entity.BTbEpc;
import com.gf.service.BTbEpcService;
import com.gf.mapper.BTbEpcMapper;
import org.springframework.stereotype.Service;

/**
*
*/
@Service
public class BTbEpcServiceImpl extends ServiceImpl<BTbEpcMapper, BTbEpc> implements BTbEpcService{

}






resource/mapper

<?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="com.gf.mapper.BTbEpcMapper">

<resultMap id="BaseResultMap" type="com.gf.entity.BTbEpc">
<id property="epcId" column="epc_id" jdbcType="BIGINT"/>
<result property="epcParentId" column="epc_parent_id" jdbcType="BIGINT"/>
<result property="epcCode" column="epc_code" jdbcType="VARCHAR"/>
<result property="epcName" column="epc_name" jdbcType="VARCHAR"/>
<result property="epcLevel" column="epc_level" jdbcType="INTEGER"/>
<result property="BExist" column="b_exist" jdbcType="BOOLEAN"/>
<result property="SExist" column="s_exist" jdbcType="BOOLEAN"/>
<result property="PExist" column="p_exist" jdbcType="BOOLEAN"/>
<result property="regionId" column="region_id" jdbcType="BIGINT"/>
<result property="epcRoutePath" column="epc_route_path" jdbcType="VARCHAR"/>
<result property="epcRouteId" column="epc_route_id" jdbcType="VARCHAR"/>
<result property="regRoutePath" column="reg_route_path" jdbcType="VARCHAR"/>
<result property="regRouteId" column="reg_route_id" jdbcType="VARCHAR"/>
<result property="sorted" column="sorted" jdbcType="INTEGER"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="creator" column="creator" jdbcType="BIGINT"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="updater" column="updater" jdbcType="BIGINT"/>
<result property="deleted" column="deleted" jdbcType="BOOLEAN"/>
<result property="version" column="version" jdbcType="INTEGER"/>
<result property="remark" column="remark" jdbcType="VARCHAR"/>
</resultMap>

<sql id="Base_Column_List">
epc_id,epc_parent_id,epc_code,
epc_name,epc_level,b_exist,
s_exist,p_exist,region_id,
epc_route_path,epc_route_id,reg_route_path,
reg_route_id,sorted,create_time,
creator,update_time,updater,
deleted,version,remark
</sql>
</mapper>





 

 

 http://localhost:8080/xxx/xxx

posted @ 2021-11-16 13:37  My_blog_s  阅读(91)  评论(0编辑  收藏  举报