文章目录简单的mybatis-plus后端项目快速搭建
- 环境与工具
- 一、mybatis-plus是什么?
- 二、具体内容
- 1.建立项目
- 2.导入依赖
- 3.连接数据库
- 4.构建项目
- 4.1各个文件内容代码(按自己数据库设计需求来编写,此处为本人数据库实列)
- 4.2编写用于测试的简陋controller
- 4.3编写DemoApplication准备测试
- 5.测试刚刚编写的简陋controller
- 总结
环境与工具
工具:IDEA、Mysql、Navicat Premium
环境:maven3.6、JDK11、基于spring boot
比如maven:
提示:以下为基本代码模板实例,按需求参考
一、mybatis-plus是什么?>官方简介与教程<
二、具体内容 1.建立项目file->New->Project
建立Spring Initializr项目
选择如下两个✔
取名,项目位置
创建结束后的结构
编写pom.xml文件:
直接提供整个内容好了,够用,需要再自行添加(如果报错,注意自己的JDK版本与其他依赖版本):
3.连接数据库4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.6 8 true com.example javaApp 1.0 project for Spring Boot org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-data-redis mysql mysql-connector-java runtime com.baomidou mybatis-plus-boot-starter 3.4.3 org.springframework.boot spring-boot-starter-thymeleaf org.apache.maven.surefire surefire-booter 2.22.2 org.springframework spring-context 5.3.7 org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-resources-plugin org.apache.maven.plugins maven-compiler-plugin 8 8
编写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;
@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 getUserInfoById(User user);
//定义方法:插入新数据
int insertUserInfo(User user);
//定义新方法:根据ID删除指定信息
int deleteUserInfoById(User user);
//定义新方法:根据ID修改信息
int updateUserById(User user);
//定义新方法:提取所有数据(使用条件查询,不设条件也可以完成此功能,所以另外两个service就不设此功能了)
List getUserInfo();
//定义新方法:条件查询
List 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{ }
注意依赖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 getUserInfoById(User user)
{
Map 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 getUserInfo()
{
List userList = userMapper.selectList(null);
return userList;
}
//定义新方法:条件查询
public List getUserInfoByMap(User user)
{
Map 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 userList = userMapper.selectByMap(map);
return userList;
}
}
4.2编写用于测试的简陋controller
注意依赖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 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 getUserInfo(User user)
{
return userServices.getUserInfo();
}
@RequestMapping("/getUserByMap")
public List getUserInfoByMap(User user)
{
return userServices.getUserInfoByMap(user);
}
}
4.3编写DemoApplication准备测试
改这个文件:
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);
}
}
5.测试刚刚编写的简陋controller
RUN
测试:
测测这个:
结果:
再测测这个:
结果:
测:
结果:
其他方法也应该可用
总结
无



