1.什么是Mybatis? MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
2. 添加依赖 修改pom.xml
文件,添加以下内容
<dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 2.1.2</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency >
踩坑记录:
我使用的springboot版本是2.3.3.RELEASE
,安装mybatis依赖过程中报错,报错内容如下:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project java-learn: There are test failures. Please refer to /Users/liuqh/ProjectItem/JavaItem/java-learn/target/surefire-reports for the individual test results. Please refer to dump files (if any exist) [date ].dump, [date ]-jvmRun[N].dump and [date ].dumpstream.
解决方法: 在pom.xml
文件中添加maven-surefire-plugin
,完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.3.3.RELEASE</version > <relativePath /> </parent > <groupId > com.hui</groupId > <artifactId > java-learn</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > java-learn</name > <description > SpringBoot框架学习</description > <properties > <java.version > 1.8</java.version > <mybatis.version > 2.1.2</mybatis.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > ${mybatis.version}</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-surefire-plugin</artifactId > <version > 2.22.2</version > <configuration > <skipTests > true</skipTests > </configuration > </plugin > </plugins > </build > </project >
3. 修改配置文件(application.yml
) server: port: 8080 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/user_bus?characterEncoding=UTF-8&useSSL=false username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver mybatis: type-aliases-package: com.hui.javalearn.model
4. 使用步骤 4.1 创建dao层 包路径 : com.hui.javalearn.dao
别名 : mapper层
用途 : 对数据库进行数据持久化操作,他的方法语句是直接针对数据库操作的,主要实现一些增删改查操作,在mybatis中方法主要与与xxx.xml内相互一一映射。
创建文件 : UserDao.java
package com.hui.javalearn.dao;import com.hui.javalearn.model.User;import org.apache.ibatis.annotations.*;@Mapper public interface UserDao { @Insert("insert into user_base(name,phone) values (#{name},#{phone})") int insert (User user) ; @Delete("delete from user_base where id = #{id}") int deleteByPrimaryKey (Integer id) ; @Update("update user_base set name =#{name},age=#{age} where id=#{id}") int updateByPrimaryKey (Integer id) ; @Select("select id,name ,age from user_base where id = #{id}") User selectByPrimaryKey (Integer id) ; }
4.2 创建model层 包路径 : com.hui.javalearn.model
别名 : domain层、entity层
用途 : 用于存放我们的实体类,与数据库中的属性值基本保持一致,实现set和get的方法。
创建文件 : UserModel.java
package com.hui.javalearn.model;public class UserModel { private int id; private String name; private String phone; public int getId () { return id; } public void setId (int id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public String getPhone () { return phone; } public void setPhone (String phone) { this .phone = phone; } public UserModel () { } public UserModel (String name, String phone) { this .name = name; this .phone = phone; } }
4.3 创建Service层 包路径 : com.hui.javalearn.service
别名 : 业务service层
用途 : 给controller层的类提供接口进行调用,在定义接口,不实现方法,具体实现在serviceImpl中。
创建文件 : UserService.java
package com.hui.javalearn.service;import com.hui.javalearn.model.UserModel;public interface UserService { int insertUser (UserModel user) ; int deleteUser (Integer id) ; int updateUserById (Integer id) ; UserModel selectUserById (Integer id) ; }
4.4 创建service实现层 包路径 : com.hui.javalearn.service.Impl
别名 : 业务service层实现层
用途 : 实现在service层定义的接口。
创建文件 : UserServiceImpl.java
package com.hui.javalearn.service.Impl;import com.hui.javalearn.dao.UserDao;import com.hui.javalearn.model.UserModel;import com.hui.javalearn.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public int insertUser (UserModel user) { return userDao.insert(user); } @Override public int deleteUser (Integer id) { return userDao.deleteByPrimaryKey(id); } @Override public int updateUserById (Integer id) { return userDao.updateByPrimaryKey(id); } @Override public UserModel selectUserById (Integer id) { return userDao.selectByPrimaryKey(id); } }
4.5 创建Controller 包路径 : com.hui.javalearn.controller
别名 : web 层
用途 : 控制层,负责具体模块的业务流程控制,需要调用service逻辑设计层的接口来控制业务流程。因为service中的方法是我们使用到的,controller通过接收前端H5或者App传过来的参数进行业务操作,再将处理结果返回到前端。
创建文件 : UserController.java
package com.hui.javalearn.controller;import com.hui.javalearn.model.UserModel;import com.hui.javalearn.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/add") public String add () { UserModel user = new UserModel ("张三" , "17600113418" ); int i = userService.insertUser(user); return "主键id" + i; } }
4.6 调用示意图