- 最近在研究Springboot+MyIbatisPlus+Mysql整合,不足之处,烦请指教, 本文采用编辑器为IDEA,目录结构如下.
- pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.6.2</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>demo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo</name> 15 <description>Demo project for Spring Boot</description> 16 <properties> 17 <java.version>1.8</java.version> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter</artifactId> 23 </dependency> 24 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-devtools</artifactId> 28 <scope>runtime</scope> 29 <optional>true</optional> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 <version>2.2.5.RELEASE</version> 36 </dependency> 37 38 <dependency> 39 <groupId>org.projectlombok</groupId> 40 <artifactId>lombok</artifactId> 41 <optional>true</optional> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-test</artifactId> 46 <scope>test</scope> 47 </dependency> 48 49 <dependency> 50 <groupId>mysql</groupId> 51 <artifactId>mysql-connector-java</artifactId> 52 <version>5.1.35</version> 53 </dependency> 54 55 56 <dependency> 57 <groupId>com.fasterxml.jackson.core</groupId> 58 <artifactId>jackson-core</artifactId> 59 </dependency> 60 <dependency> 61 <groupId>com.fasterxml.jackson.core</groupId> 62 <artifactId>jackson-databind</artifactId> 63 </dependency> 64 <dependency> 65 <groupId>com.fasterxml.jackson.datatype</groupId> 66 <artifactId>jackson-datatype-joda</artifactId> 67 </dependency> 68 <dependency> 69 <groupId>com.fasterxml.jackson.module</groupId> 70 <artifactId>jackson-module-parameter-names</artifactId> 71 </dependency> 72 <!-- 分页插件 --> 73 <!-- <dependency>--> 74 <!-- <groupId>com.github.pagehelper</groupId>--> 75 <!-- <artifactId>pagehelper-spring-boot-starter</artifactId>--> 76 <!-- <version>1.1.2</version>--> 77 <!-- </dependency>--> 78 <!-- alibaba的druid数据库连接池 --> 79 <dependency> 80 <groupId>com.alibaba</groupId> 81 <artifactId>druid-spring-boot-starter</artifactId> 82 <version>1.1.20</version> 83 </dependency> 84 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-jdbc</artifactId> 88 <version>5.3.4</version> 89 </dependency> 90 91 <dependency> 92 <groupId>com.baomidou</groupId> 93 <artifactId>mybatis-plus-boot-starter</artifactId> 94 <version>3.2.0</version> 95 </dependency> 96 97 <dependency> 98 <groupId>com.baomidou</groupId> 99 <artifactId>mybatis-plus-generator</artifactId> 100 <version>3.2.0</version> 101 </dependency> 102 103 <dependency> 104 <groupId>org.projectlombok</groupId> 105 <artifactId>lombok</artifactId> 106 <optional>true</optional> 107 </dependency> 108 </dependencies> 109 110 <build> 111 <plugins> 112 <plugin> 113 <groupId>org.springframework.boot</groupId> 114 <artifactId>spring-boot-maven-plugin</artifactId> 115 <configuration> 116 <excludes> 117 <exclude> 118 <groupId>org.projectlombok</groupId> 119 <artifactId>lombok</artifactId> 120 </exclude> 121 </excludes> 122 </configuration> 123 </plugin> 124 </plugins> 125 </build> 126 127 </project>
- controller
1 package com.example.demo.controller; 2 3 import com.example.demo.domain.User; 4 import com.example.demo.service.imp.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 import java.util.List; 10 11 @RestController 12 /** 13 * DemoApplication class 14 * @author wuliang
* @date 20点43分 16 */ 17 public class UserController { 18 19 @Autowired 20 private UserService userService; 21 22 @GetMapping(value = "/getUsers") 23 public List<User> getUsers() { 24 return userService.list(); 25 } 26 27 28 }
- domain
1 package com.example.demo.domain; 2 3 import com.baomidou.mybatisplus.annotation.IdType; 4 import com.baomidou.mybatisplus.annotation.TableField; 5 import com.baomidou.mybatisplus.annotation.TableId; 6 import com.baomidou.mybatisplus.annotation.TableName; 7 import java.io.Serializable; 8 import lombok.AllArgsConstructor; 9 import lombok.Builder; 10 import lombok.Data; 11 import lombok.NoArgsConstructor; 12 13 @Data 14 @Builder 15 @AllArgsConstructor 16 @NoArgsConstructor 17 @TableName(value = "t_user") 18 /** 19 * DemoApplication class 20 * @author wuliang 21 * @date 20点43分 22 */ 23 public class User implements Serializable { 24 @TableId(value = "user_id", type = IdType.AUTO) 25 private Integer userId; 26 27 @TableField(value = "user_name") 28 private String userName; 29 30 @TableField(value = "password") 31 private String password; 32 33 @TableField(value = "phone") 34 private String phone; 35 36 private static final long serialVersionUID = 1L; 37 38 public static final String COL_USER_ID = "user_id"; 39 40 public static final String COL_USER_NAME = "user_name"; 41 42 public static final String COL_PASSWORD = "password"; 43 44 public static final String COL_PHONE = "phone"; 45 }
- service
1 package com.example.demo.service.imp; 2 3 import com.example.demo.domain.User; 4 import com.baomidou.mybatisplus.extension.service.IService; 5 /** 6 * DemoApplication class 7 * @author wuliang 8 * @date 20点43分 9 */ 10 public interface UserService extends IService<User>{ 11 12 13 }
- serviceimp
1 package com.example.demo.service; 2 3 import org.springframework.stereotype.Service; 4 import javax.annotation.Resource; 5 import java.util.List; 6 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 7 import com.example.demo.domain.User; 8 import com.example.demo.mapper.UserMapper; 9 import com.example.demo.service.imp.UserService; 10 @Service 11 /** 12 * DemoApplication class 13 * @author wuliang 14 * @date 20点43分 15 */ 16 public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{ 17 18 }
- DemoApplication启动类
1 package com.example.demo; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 @MapperScan("com.example.demo.mapper") 9 /** 10 * DemoApplication class 11 * @author wuliang 12 * @date 20点43分 13 */ 14 public class DemoApplication { 15 16 public static void main(String[] args) { 17 SpringApplication.run(DemoApplication.class, args); 18 } 19 20 }
UserMapper.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.example.demo.mapper.UserMapper"> 4 <resultMap id="BaseResultMap" type="com.example.demo.domain.User"> 5 <!--@mbg.generated--> 6 <!--@Table t_user--> 7 <id column="user_id" jdbcType="INTEGER" property="userId" /> 8 <result column="user_name" jdbcType="VARCHAR" property="userName" /> 9 <result column="password" jdbcType="VARCHAR" property="password" /> 10 <result column="phone" jdbcType="VARCHAR" property="phone" /> 11 </resultMap> 12 <sql id="Base_Column_List"> 13 <!--@mbg.generated--> 14 user_id, user_name, `password`, phone 15 </sql> 16 </mapper>
application.yml
server: port: 8080 spring: datasource: name: test url: jdbc:mysql://localhost:3306/springboot?useSSL=false&characterEncoding=UTF-8&?useUnicode=true username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 ## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别 mybatis: mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径 type-aliases-package: com.example.demo.domain #注意:对应实体类的路径 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #pagehelper分页插件pagehelper: # helperDialect: mysql # reasonable: true # supportMethodsArguments: true # params: count=countSql
mysql 表结构
1 /* 2 Navicat Premium Data Transfer 3 4 Source Server : localhost-root 5 Source Server Type : MySQL 6 Source Server Version : 50731 7 Source Host : localhost:3306 8 Source Schema : springboot 9 10 Target Server Type : MySQL 11 Target Server Version : 50731 12 File Encoding : 65001 13 14 Date: 08/01/2022 21:10:29 15 */ 16 17 SET NAMES utf8mb4; 18 SET FOREIGN_KEY_CHECKS = 0; 19 20 -- ---------------------------- 21 -- Table structure for t_user 22 -- ---------------------------- 23 DROP TABLE IF EXISTS `t_user`; 24 CREATE TABLE `t_user` ( 25 `user_id` int(11) NOT NULL AUTO_INCREMENT, 26 `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, 27 `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, 28 `phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, 29 PRIMARY KEY (`user_id`) USING BTREE 30 ) ENGINE = InnoDB AUTO_INCREMENT = 1000 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 31 32 -- ---------------------------- 33 -- Records of t_user 34 -- ---------------------------- 35 INSERT INTO `t_user` VALUES (1, '张三', '324324234', '18122311198'); 36 37 SET FOREIGN_KEY_CHECKS = 1;
访问地址 :http://localhost:8080/getUsers
效果截图