简单的mybatis-plus后端项目快速搭建
工具:IDEA、Mysql、Navicat Premium
环境:maven3.6、JDK11、基于spring boot
比如maven:
提示:以下为基本代码模板实例,按需求参考
>官方简介与教程<
file->New->Project
建立Spring Initializr项目
选择如下两个✔
取名,项目位置
创建结束后的结构
直接提供整个内容好了,够用,需要再自行添加(如果报错,注意自己的JDK版本与其他依赖版本):
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.6version>
parent>
<properties>
<java.version>8java.version>
<skipTests>trueskipTests>
properties>
<groupId>com.examplegroupId>
<artifactId>javaAppartifactId>
<version>1.0version>
<description>project for Spring Bootdescription>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.apache.maven.surefiregroupId>
<artifactId>surefire-booterartifactId>
<version>2.22.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.7version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>8source>
<target>8target>
configuration>
plugin>
plugins>
build>
project>
编写application.properties:
具体代码
#端口号:
server.port=8848
#======================================================
# 配置数据库:
#======================================================
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?allowPublicKeyRetrieval=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false
# =====================================================
#
# ======================================================
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
在这个目录建4个包
都应该知道这几个包的意思
再分别在各个包中建立class/interface文件
再在service里添加serviceimp包,内容为UserServiceImp
bean->Uer文件:
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 lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* 定义POJO类
*/
@Data
@Component
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "user")//映射
//映射表名
@TableName(value = "user")//映射
public class User implements Serializable
{
//注意,此bean一切基于数据库设计而编写,根据自己需要来自行修改
@TableId(value = "userid",type = IdType.AUTO) //表明主键,设置自增,如果主键是String类型,就不用设置自增
private Integer userid; //用户id
private String phone; //用户状态
private String username; //用户名
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
注意依赖service中的UserService:
import com.example.demo.bean.User;
import java.util.List;
import java.util.Map;
public interface UserServices
{
//定义方法:根据ID获取指定信息
Map <String, Object> getUserInfoById(User user);
//定义方法:插入新数据
int insertUserInfo(User user);
//定义新方法:根据ID删除指定信息
int deleteUserInfoById(User user);
//定义新方法:根据ID修改信息
int updateUserById(User user);
//定义新方法:提取所有数据(使用条件查询,不设条件也可以完成此功能,所以另外两个service就不设此功能了)
List <User> getUserInfo();
//定义新方法:条件查询
List <User> getUserInfoByMap(User user);
}
注意依赖mapper中的UserMapper:
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.bean.User;
/**
* 定义数据访问层
*/
public interface UserMapper extends BaseMapper<User>
{
}
注意依赖service->serviceimp中的UserServiceImp:
import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.services.UserServices;
import org.apache.maven.surefire.shade.org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImp implements UserServices
{
//通过依赖注入:数据库访问层的组件
@Resource
private UserMapper userMapper;
//实现接口中的方法
//定义方法:根据ID获取指定信息
@Override
public Map<String, Object> getUserInfoById(User user)
{
Map<String, Object> map = new HashMap<>();
User object = userMapper.selectById(user);
map.put("user", object);
return map;
}
//定义方法:插入新数据
@Override
public int insertUserInfo(User user)
{
int i = userMapper.insert(user);
if (i == 1)
{
return i;
}
else
{
return 0;
}
}
//定义新方法:根据ID删除指定信息
@Override
public int deleteUserInfoById(User user)
{
int i = userMapper.deleteById(user);
return i;
}
//定义新方法:根据ID修改信息
public int updateUserById(User user)
{
int i = userMapper.updateById(user);
return i;
}
//定义新方法:提取所有数据
public List<User> getUserInfo()
{
List<User> userList = userMapper.selectList(null);
return userList;
}
//定义新方法:条件查询
public List<User> getUserInfoByMap(User user)
{
Map<String, Object> map = new HashMap<>();
if (user.getPhone() != null)
map.put("phone", user.getPhone());
if (user.getUsername() != null)
map.put("username", user.getUsername());
if (user.getUserid() != null)
map.put("userid", user.getUserid());
List<User> userList = userMapper.selectByMap(map);
return userList;
}
}
注意依赖controller中的UserController:
import com.example.demo.bean.User;
import com.example.demo.services.UserServices;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.List;
/**
* 定义控制器
*/
//解决跨域
@CrossOrigin
//表明:当前类充当控制器(控制类)、该注解的功能:默认方式JSON
@RestController
public class UserController
{
//依赖注入:业务层的组件
// @Autowired
@Resource
private UserServices userServices;
@RequestMapping("/getUserInfoById")
public Map<String, Object> getUserInfoById(User user)
{
return userServices.getUserInfoById(user);
}
@RequestMapping("/insertUserInfo")
public int insertUserInfo(User user)
{
return userServices.insertUserInfo(user);
}
@RequestMapping("/deleteUserInfoById")
public int deleteUserInfoById(User user)
{
return userServices.deleteUserInfoById(user);
}
@RequestMapping("/updateUserById")
public int updateUserById(User user)
{
return userServices.updateUserById(user);
}
@RequestMapping("/getUserall")
public List<User> getUserInfo(User user)
{
return userServices.getUserInfo();
}
@RequestMapping("/getUserByMap")
public List<User> getUserInfoByMap(User user)
{
return userServices.getUserInfoByMap(user);
}
}
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
通过注解扫描数据访问层的接口
*/
@MapperScan(basePackages = {"com.example.demo.mapper"})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
RUN
测试:
测测这个:
结果:
再测测这个:
结果:
测:
结果:
其他方法也应该可用
无