集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用mapper的xml文件的。
目录
pom.xml文件
1 24 4.0.0 56 11org.springframework.boot 7spring-boot-starter-parent 81.5.21.RELEASE 910 com.xiaostudy 12demo1 130.0.1-SNAPSHOT 14demo1 15Demo project for Spring Boot 16 1718 20 211.8 1922 23 60 6124 27org.springframework.boot 25spring-boot-starter-web 2628 31org.springframework.boot 29spring-boot-starter 3032 36 37 39org.springframework.boot 33spring-boot-starter-test 34test 3540 44 45 46com.baomidou 41mybatis-plus-boot-starter 423.1.1 4347 51 52 53com.alibaba 48druid 491.0.29 5054 58 59mysql 55mysql-connector-java 565.1.38 5762 69 7063 6864 67org.springframework.boot 65spring-boot-maven-plugin 66
实体类User
1 package com.xiaostudy.demo1.entity; 2 3 import com.baomidou.mybatisplus.annotation.TableName; 4 5 import java.io.Serializable; 6 7 @TableName("t_user") 8 public class User implements Serializable { 9 private String id;10 private String userName;11 private String userPassword;12 13 public String getId() {14 return id;15 }16 17 public void setId(String id) {18 this.id = id;19 }20 21 public String getUserName() {22 return userName;23 }24 25 public void setUserName(String userName) {26 this.userName = userName;27 }28 29 public String getUserPassword() {30 return userPassword;31 }32 33 public void setUserPassword(String userPassword) {34 this.userPassword = userPassword;35 }36 37 @Override38 public String toString() {39 return "User{" +40 "id='" + id + '\'' +41 ", userName='" + userName + '\'' +42 ", userPassword='" + userPassword + '\'' +43 '}';44 }45 }
Dao[也叫Mapper]
1 package com.xiaostudy.demo1.mapper; 2 3 import com.baomidou.mybatisplus.core.mapper.BaseMapper; 4 import com.xiaostudy.demo1.entity.User; 5 import org.springframework.stereotype.Repository; 6 7 /** 8 *9 * Mapper接口10 *
11 *12 * @author xiaostudy13 * @since 2019-09-1514 */15 @Repository16 public interface UserDao extends BaseMapper{17 }
Service
1 package com.xiaostudy.demo1.service;2 3 import com.baomidou.mybatisplus.extension.service.IService;4 import com.xiaostudy.demo1.entity.User;5 6 public interface UserService extends IService{7 }
Service实现类
1 package com.xiaostudy.demo1.service.impl; 2 3 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 4 import com.xiaostudy.demo1.entity.User; 5 import com.xiaostudy.demo1.mapper.UserDao; 6 import com.xiaostudy.demo1.service.UserService; 7 import org.springframework.stereotype.Service; 8 9 @Service10 public class UserServiceImpl extends ServiceImplimplements UserService {11 }
Controller
package com.xiaostudy.demo1.controller;import com.xiaostudy.demo1.entity.User;import com.xiaostudy.demo1.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Date;import java.util.List;@RestController@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @RequestMapping("/getAll") public String getAll() { Listlist = userService.list(); System.out.println("list:" + list); return list.toString(); } @RequestMapping("/insert") public String insert() { User user = new User(); // 不设置id的话,会自动生成一个UUID// user.setId(new Date().getTime() + ""); user.setUserName("aaa"); user.setUserPassword("bbb"); boolean save = userService.save(user); return getAll(); }}
application-jdbc.yml
1 #数据源 2 spring: 3 security: 4 basic: 5 enabled: false 6 datasource: 7 url: jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false 8 username: root 9 password: 密码10 driver-class-name: com.mysql.jdbc.Driver11 druid:12 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时13 initialSize: 114 # 最小连接池数量15 minIdle: 116 # 最大连接池数量17 maxActive: 1018 # 配置获取连接等待超时的时间19 maxWait: 1000020 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒21 timeBetweenEvictionRunsMillis: 6000022 # 配置一个连接在池中最小生存的时间,单位是毫秒23 minEvictableIdleTimeMillis: 30000024 # 验证连接有效与否的SQL,不同的数据配置不同25 validationQuery: select 126 # 建议配置为true,不影响性能,并且保证安全性。27 # 申请连接的时候检测,如果空闲时间大于28 # timeBetweenEvictionRunsMillis,29 # 执行validationQuery检测连接是否有效。30 testWhileIdle: true31 # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。32 # 这里建议配置为TRUE,防止取到的连接不可用33 testOnBorrow: true34 # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能35 testOnReturn: false36 # 是否缓存preparedStatement,也就是PSCache。37 # PSCache对支持游标的数据库性能提升巨大,比如说oracle。38 # 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。39 # 作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录,40 # 该应该是支持PSCache。41 # 打开PSCache,并且指定每个连接上PSCache的大小42 poolPreparedStatements: true43 maxPoolPreparedStatementPerConnectionSize: 2044 # 属性类型是字符串,通过别名的方式配置扩展插件,45 # 常用的插件有:46 # 监控统计用的filter:stat47 # 日志用的filter:log4j48 # 防御sql注入的filter:wall49 filters: stat50 51 # ====================MybatisPlus====================52 mybatis-plus:53 # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml54 # 如果是放在resource目录 classpath:/mapper/*Mapper.xml55 mapper-locations: classpath*:com/frame/**/**.xml,classpath*:mapping/**/**.xml56 #实体扫描,多个package用逗号或者分号分隔57 typeAliasesPackage: com.frame.**.entity,com.frame.**.dto58 global-config:59 #刷新mapper 调试神器60 db-config:61 #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";62 id-type: UUID63 #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"64 field-strategy: 265 #驼峰下划线转换66 column-underline: false67 #数据库大写下划线转换68 # capital-mode: true69 #逻辑删除配置70 logic-delete-value: 171 logic-not-delete-value: 072 refresh: true73 configuration:74 #配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)75 map-underscore-to-camel-case: true76 cache-enabled: false77 #配置JdbcTypeForNull, oracle数据库必须配置78 jdbc-type-for-null: 'null'79 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl80 database-id: mysql
application.yml
1 server:2 port: 80803 4 spring:5 profiles:6 include: jdbc
测试