一般网站图标是用什么做的,网件路由器无线中继,网站建设开发方式包括,做网站前期费用前言
前面快速学习了Mybatis#xff0c;现在开始快速学习MyBatisPlus
学习教程#xff1a; 黑马mybatis教程全套视频教程#xff0c;2天Mybatis框架从入门到精通
黑马程序员最新MybatisPlus全套视频教程#xff0c;4小时快速精通mybatis-plus框架
简介
MyBatisPlus 是…前言
前面快速学习了Mybatis现在开始快速学习MyBatisPlus
学习教程 黑马mybatis教程全套视频教程2天Mybatis框架从入门到精通
黑马程序员最新MybatisPlus全套视频教程4小时快速精通mybatis-plus框架
简介
MyBatisPlus 是基于MyBatis框架基础上开发的增强型工具旨在简化开发、提高效率。
创建项目
创建项目
问了一下后端同事建议是创建一个springboot项目在springboot项目里使用mybatis-plus。参考了两篇文章给弄出来了下面是基本步骤如果遇到啥问题别找我不会。我是搞前端开发的学后端属于公司要求。
参考的文章
搭建SpringBoot项目三种方式(超详细版)
Spring Boot MyBatis MySQL框架搭建
1、修改为阿里源 https://start.aliyun.com/ 2、设置项目名称、其他配置 3、选择springboot版本和依赖 4、下载依赖并运行 下载完依赖后切换到SpringbootApplication.java文件里点击运行 运行成功并且能在浏览器里访问证明项目成功
项目配置
添加mybatis-plus和mysql依赖 在pom.xml里添加依赖
!-- mybatis plus--
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version
/dependency
!-- mysql--
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.28/version
/dependencymysql的依赖要与你的mysql保持一致。
添加完依赖后下载。
配置数据库信息 在src/main/resources/application.properties文件里添加数据库信息
# MySQL
spring.datasource.urljdbc:mysql://localhost:3306/test
spring.datasource.usernameroot
spring.datasource.password123abc!#
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver创建实体类 在src/main/java/org/example/springboot文件夹下创建entity文件夹用于存放实体类。在entity下创建实体类User.java
public class User {Integer id;String name;Integer age;String email;Integer sex;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getEmail() {return email;}public void setEmail(String email) {this.email email;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex sex;}Overridepublic String toString() {return User{ id id , name name \ , age age , email email \ , sex sex };}}创建mapper接口 在src/main/java/org/example/springboot文件夹下创建mapper文件夹用于存放实体类。在mapper下创建接口UseMapperr.java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.springboot.entity.User;import java.util.List;// 继承mybatis-plus的BaseMapper接口
Mapper
public interface UserMapper extends BaseMapperUser {// 查询所有用户ListUser selectAllUser();
}创建UserMapper.xml
在src/main/resources目录下创建一个名为mapper的文件夹并在其中创建一个名为UserMapper.xml的文件。这里要注意的是文件路径不要错了。
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--
namespace: 命名空间
resultType:返回值的类型一般是实体类
id:唯一标识
--
mapper namespaceorg.example.springboot.mapper.UserMapperresultMap iduserResultMap typeorg.example.springboot.entity.User!-- property 属性是指对应的 Java 类的属性column 属性是指对应的数据库表的字段名 --!-- 主键映射--id propertyid columnid//resultMap!-- 查询所有用户--select idselectAllUser resultMapuserResultMapselect *from user;/select/mapper创建Service和Controller 在src/main/java/org/example/springboot文件夹下创建service文件夹。在service下创建接口UserService.java
import org.example.springboot.entity.User;
import org.example.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;Service
public class UserService {Autowiredprivate UserMapper userMapper;public ListUser selectAllUser() {// 查询id为1、2的用户ListInteger idList new ArrayList();idList.add(1);idList.add(2);return userMapper.selectBatchIds(idList);}
}在src/main/java/org/example/springboot文件夹下创建controller文件夹。在controller下创建接口UserController.java
import org.example.springboot.entity.User;
import org.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;Controller
public class UserController {Autowiredprivate UserService userService;RequestMapping(/userlist)ResponseBodypublic ListUser selectAllUser() {return userService.selectAllUser();}
}测试 重启项目在浏览器里访问userlist看能否从数据库获取到数据