集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用mapper的xml文件的。
目录
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>1.5.21.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.xiaostudy</groupId> 12 <artifactId>demo1</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo1</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <!-- spring-boot --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-test</artifactId> 34 <scope>test</scope> 35 </dependency> 36 37 <!--mybatis-plus自动的维护了mybatis以及mybatis-spring的依赖, 38 在springboot中这三者不能同时的出现,避免版本的冲突,表示:跳进过这个坑--> 39 <dependency> 40 <groupId>com.baomidou</groupId> 41 <artifactId>mybatis-plus-boot-starter</artifactId> 42 <version>3.1.1</version> 43 </dependency> 44 45 <!-- 引入Druid依赖,阿里巴巴所提供的数据源 --> 46 <dependency> 47 <groupId>com.alibaba</groupId> 48 <artifactId>druid</artifactId> 49 <version>1.0.29</version> 50 </dependency> 51 52 <!-- 提供mysql驱动 --> 53 <dependency> 54 <groupId>mysql</groupId> 55 <artifactId>mysql-connector-java</artifactId> 56 <version>5.1.38</version> 57 </dependency> 58 59 </dependencies> 60 61 <build> 62 <plugins> 63 <plugin> 64 <groupId>org.springframework.boot</groupId> 65 <artifactId>spring-boot-maven-plugin</artifactId> 66 </plugin> 67 </plugins> 68 </build> 69 70 </project>
实体类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 @Override 38 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 * <p> 9 * Mapper接口 10 * </p> 11 * 12 * @author xiaostudy 13 * @since 2019-09-15 14 */ 15 @Repository 16 public interface UserDao extends BaseMapper<User> { 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<User> { 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 @Service 10 public class UserServiceImpl extends ServiceImpl<UserDao, User> implements 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() { List<User> list = 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.Driver 11 druid: 12 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 13 initialSize: 1 14 # 最小连接池数量 15 minIdle: 1 16 # 最大连接池数量 17 maxActive: 10 18 # 配置获取连接等待超时的时间 19 maxWait: 10000 20 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 21 timeBetweenEvictionRunsMillis: 60000 22 # 配置一个连接在池中最小生存的时间,单位是毫秒 23 minEvictableIdleTimeMillis: 300000 24 # 验证连接有效与否的SQL,不同的数据配置不同 25 validationQuery: select 1 26 # 建议配置为true,不影响性能,并且保证安全性。 27 # 申请连接的时候检测,如果空闲时间大于 28 # timeBetweenEvictionRunsMillis, 29 # 执行validationQuery检测连接是否有效。 30 testWhileIdle: true 31 # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 32 # 这里建议配置为TRUE,防止取到的连接不可用 33 testOnBorrow: true 34 # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 35 testOnReturn: false 36 # 是否缓存preparedStatement,也就是PSCache。 37 # PSCache对支持游标的数据库性能提升巨大,比如说oracle。 38 # 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。 39 # 作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录, 40 # 该应该是支持PSCache。 41 # 打开PSCache,并且指定每个连接上PSCache的大小 42 poolPreparedStatements: true 43 maxPoolPreparedStatementPerConnectionSize: 20 44 # 属性类型是字符串,通过别名的方式配置扩展插件, 45 # 常用的插件有: 46 # 监控统计用的filter:stat 47 # 日志用的filter:log4j 48 # 防御sql注入的filter:wall 49 filters: stat 50 51 # ====================MybatisPlus==================== 52 mybatis-plus: 53 # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml 54 # 如果是放在resource目录 classpath:/mapper/*Mapper.xml 55 mapper-locations: classpath*:com/frame/**/**.xml,classpath*:mapping/**/**.xml 56 #实体扫描,多个package用逗号或者分号分隔 57 typeAliasesPackage: com.frame.**.entity,com.frame.**.dto 58 global-config: 59 #刷新mapper 调试神器 60 db-config: 61 #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID"; 62 id-type: UUID 63 #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断" 64 field-strategy: 2 65 #驼峰下划线转换 66 column-underline: false 67 #数据库大写下划线转换 68 # capital-mode: true 69 #逻辑删除配置 70 logic-delete-value: 1 71 logic-not-delete-value: 0 72 refresh: true 73 configuration: 74 #配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId) 75 map-underscore-to-camel-case: true 76 cache-enabled: false 77 #配置JdbcTypeForNull, oracle数据库必须配置 78 jdbc-type-for-null: ‘null‘ 79 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 80 database-id: mysql
application.yml
1 server: 2 port: 8080 3 4 spring: 5 profiles: 6 include: jdbc
测试
原文地址:https://www.cnblogs.com/xiaostudy/p/11525092.html
时间: 2024-10-30 06:05:02