fastmybatis记录
从某公众号上看到 fastmybatis, 声称是可以不用写 sql, xml ,不用定义service方法,就可以完成CRUD,简直不要太好用!
安排!如果可以用在项目上就太好了。
项目大概如下:
源码如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>fastmybatis</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>net.oschina.durcframework</groupId>
<artifactId>fastmybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--web核心依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.hxf.fastmybatis.FastMybatisApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(@RequestBody User user){
Query query = new Query();
query.eq("name", user.getName());
query.page(1, 1);
List<User> list = userService.list(query);
return list;
}
}
定义一个Service,实现IService,但不用写方法。。。
@Service
public class UserService implements IService<User/*实体类*/, Integer/*主键类型*/> {
}
再写一个Mapper, 但也不需要写更多的代码(感觉确实还不错)
public interface UserMapper extends CrudMapper<User/*实体类*/, Integer/*主键类型*/> {
}
public class User implements Serializable {
private Long id;//编号
private String name;//用户名
private Integer age;//密码
private Integer score;
}
application.yml
server:
port: 8081
spring:
#数据库连接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/develop?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: xxxxxx
#mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.hxf.fastmybatis.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
这样一个简单的demo 就写好了,确实可用。比Mybatis 还要方便一些
但!
在一些比较复杂的业务时。。好像就还得再研究一下,或多表关联查询时,总感觉也没那么方便?
还是这个玩意本就适合在单表的CRUD ?