使用方法
在SpringBoot项目中
pom.xml添加fastmybatis-spring-boot-starter
<dependency>
<groupId>net.oschina.durcframework</groupId>
<artifactId>fastmybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
数据库新建user表
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户名',
`password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '密码',
`avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '头像',
`nick_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '昵称',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '地址',
`email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '邮箱',
`birthday` datetime DEFAULT NULL COMMENT '生日',
`age` int DEFAULT NULL COMMENT '年龄',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
`delete_flag` int DEFAULT NULL COMMENT '0:未删除 1:已删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
添加对应的实体类User.java
public class User {
private Integer id;
private String username;
private String password;
private String nickName;
private String avatar;
private String address;
private String email;
@DateTimeFormat(pattern="yyyy-MM-dd")
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
private LocalDateTime birthday;
private Integer age;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime createTime;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime updateTime;
private Integer deleteFlag;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public LocalDateTime getBirthday() {
return birthday;
}
public void setBirthday(LocalDateTime birthday) {
this.birthday = birthday;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
public Integer getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(Integer deleteFlag) {
this.deleteFlag = deleteFlag;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username=" + username +
", password=" + password +
", nickName=" + nickName +
", address=" + address +
", email=" + email +
", birthday=" + birthday +
", age=" + age +
", createTime=" + createTime +
", updateTime=" + updateTime +
", deleteFlag=" + deleteFlag +
"}" ;
}
}
UserService.java
@Service
public class UserService implements IService<User,Integer> {
}
Mapper类UserMapper.java
public interface TUserMapper extends CrudMapper<User,Integer> {
}
Mapper方法列表
Entity getById(ID id);
Entity getByQuery(@Param("query")Query query);
Entity getByColumn(@Param("column")String column,@Param("value")Object value);
long getCount(@Param("query")Query query);
List<Entity> listByColumn(@Param("column")String column,@Param("value")Object value);
List<Entity> list(@Param("query")Query query);
List<Map<String, Object>> listMap(@Param("columns")List<String> columns, @Param("query")Query query);
int save(Entity entity);
int saveIgnoreNull(Entity entity);
int saveBatch(@Param("entitys")List<Entity> entitys);
int saveMulti(@Param("entitys")List<Entity> entitys);
int update(Entity entity);
int updateIgnoreNull(Entity entity);
int updateByQuery(@Param("entity") Object entity, @Param("query") Query query);
int delete(Entity entity);
int deleteById(ID id);
int deleteByQuery(@Param("query")Query query);
测试类MapperTest.java
@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperTest {
@Resource
private UserMapper mapper;
@Test
public void test() {
User byId = mapper.getById(1);
System.out.println(byId);
}
@Test
public void test2() {
Query query = new Query().eq("username", "张三").eq("age", 12);
List<User> users = mapper.list(query);
System.out.println(users);
}
@Test
public void test3() {
Query query = new Query().in("age", Arrays.asList(10, 11, 12));
List<User> users = mapper.list(query);
System.out.println(users);
}
@Test
public void test4() {
Query query = new Query().gt("create_time", "2022-01-01");
List<User> users = mapper.list(query);
System.out.println(users);
}
@Test
public void test5() {
Query query = new Query().eq("address","北京市").ge("age",10).orderby("age", Sort.DESC);
List<User> users = mapper.list(query);
System.out.println(users);
}
@Test
public void test6(){
Query query = new Query().gt("age",10).page(1,2);
List<User> users = mapper.list(query);
System.out.println(users);
}
@Test
public void test7(){
User user = new User();
Query query=new Query().eq(StringUtils.isNotBlank(user.getAddress()),"address",user.getAddress())
.eq(StringUtils.isNotBlank(user.getUsername()),"username",user.getUsername());
List<User> users = mapper.list(query);
long count = mapper.getCount(query);
System.out.println(count);
users.forEach(System.out::println);
}
}
|