先发结构图
首先需要先把dao的相关依赖导入到pom.xml,我的mysql是8版本此处有坑,mysql8的url连接有改变,根据自己mysql版本选择。
org.springframework.boot spring-boot-starter-jdbc org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime com.alibaba druid-spring-boot-starter 1.2.8
以数据库表Users为例
(1)、users表-- ---------------------------- -- Table structure for users -- ---------------------------- DROp TABLE IF EXISTS `users`; CREATE TABLE `users` ( `userId` int NOT NULL AUTO_INCREMENT, `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '账号', `password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '密码', `mail` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (`userId`) USING BTREE, INDEX `mail`(`mail`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;(2)、pom.xml
的build—> plugins 标签引入如下plugin标签
*注意org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 ${basedir}/src/main/resources/config/generatorConfig.xml true true mysql mysql-connector-java 8.0.27
需要在plugin引入你的和mysql版本一致的mysql-connector-java,我mysql是8版本的,所以引用的是
之后刷新maven,出现下面的插件就是成功了
完整的pom.xml
(3)、编写配置文件4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 com.mqb springboot02 0.0.1-SNAPSHOT springboot02 springboot02 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime com.alibaba druid-spring-boot-starter 1.2.8 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf src/main/java ***.xml ***.properties ***.yaml false org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 ${basedir}/src/main/resources/config/generatorConfig.xml true true mysql mysql-connector-java 8.0.27
generatorConfig.xml
application.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/tsingby?serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# 全局的映射,不用在xml文件写实体类的全路径
type-aliases-package: com.mqb.springboot02.pojo
点击mybatis-generator:generate插件,查看是否生成mapper和映射文件
多出来的文件都是自动生成的,我没手动加任何文件
(4)、查看生成的文件
User.java
package com.mqb.springboot02.pojo;
import org.springframework.context.annotation.Bean;
public class User {
private Integer userid;
private String username;
private String password;
private String mail;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail == null ? null : mail.trim();
}
}
UserMapper.java接口
package com.mqb.springboot02.mapper;
import com.mqb.springboot02.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseBody;
@Repository
public interface UserMapper {
int deleteByPrimaryKey(Integer userid);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer userid);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
Usermapper.xml
(4)、测试userId, username, password, mail
-
在UserMapper.java类上添加@Repository注解
-
编写Userservice接口,只使用一个方法测试
package com.mqb.springboot02.userService; import com.mqb.springboot02.pojo.User; public interface UserService { User selectByPrimaryKey(Integer userid); } -
UserServiceIMmpl实现类
package com.mqb.springboot02.userService.userServiceImpl; import com.mqb.springboot02.mapper.UserMapper; import com.mqb.springboot02.pojo.User; import com.mqb.springboot02.userService.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public User selectByPrimaryKey(Integer userid) { return userMapper.selectByPrimaryKey(userid); } } -
UserController
package com.mqb.springboot02.controller; import com.mqb.springboot02.pojo.User; import com.mqb.springboot02.userService.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; @Controller @ResponseBody public class UserController { @Autowired UserService userService; @RequestMapping("/test") public User test(){ return userService.selectByPrimaryKey(1); } }在启动主程序添加@MapperScan()注解,扫描mapper
package com.mqb.springboot02; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.mqb.springboot02.mapper") @SpringBootApplication public class Springboot02Application { public static void main(String[] args) { SpringApplication.run(Springboot02Application.class, args); } }放上目录结构
启动springboot项目,浏览器访问localhost:8080,出现信息,成功



