crud template 用例演示
依赖
- 本次使用的是快照版本,请做出如下的maven调整
- pom.xml
com.github.huifer
for-mybatis
0.0.4-SNAPSHOT
oss-snapshots
oss快照
https://oss.sonatype.org/content/repositories/snapshots
true
always
- setting.xml
aliyunmaven
*
阿里云公共仓库
https://maven.aliyun.com/repository/public
oss-sp
oss-snapshots
oss快照
https://oss.sonatype.org/content/repositories/snapshots
oss-snapshots
alibaba
true
false
https://maven.aliyun.com/repository/public
nexus_snapshot_repository
false
true
https://oss.sonatype.org/content/repositories/snapshots
default
- 如果不想修改配置可以直接下载项目: crud 切换到dev分支. 用例内容在 simple-example 中
crud 测试
- 首先贴出相关代码
@SpringBootApplication
@EnableCrudTemplate(daoType = DaoType.MYBATIS,scanPackages = {"com.github.huifer.mybatis"})
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class, args);
}
}
@Mapper
@CacheKey(key = "issues", type = IssuesEntity.class)
public interface IssuesMapper extends A {
@Insert(" insert into issue(new_title)values(#{newTitle,jdbcType=VARCHAR})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insertSelective(IssuesEntity record);
@Select("select * from issue where id = #{integer} ")
IssuesEntity selectByPrimaryKey(Integer integer);
@Override
@Update("UPDATE `issue` SET `new_title` = #{newTitle} WHERE `id` = #{id} ")
int updateByPrimaryKeySelective(IssuesEntity record);
}
public class IssuesEntity implements BaseEntity {
private Integer id;
private String newTitle;
private String ooo() {
return "OOO" + this.newTitle;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNewTitle() {
return newTitle;
}
public void setNewTitle(String newTitle) {
this.newTitle = newTitle;
}
}
测试新增对象
@Test
void testInsert() {
IssuesEntity issuesEntity = new IssuesEntity();
issuesEntity.setNewTitle("mybatis_test");
crudFacade.insert(issuesEntity);
}
开启 mapper上的@CacheKey
- 希望结果时redis和数据库都存有
- 执行结果如下图
注释 mapper上的@CacheKey
- 注释掉mapper上面的
@CacheKey
@Mapper
@CacheKey(key = "issues", type = IssuesEntity.class)
public interface IssuesMapper extends A {
@Insert(" insert into issue(new_title)values(#{newTitle,jdbcType=VARCHAR})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insertSelective(IssuesEntity record);
@Select("select id as id , new_title as newTitle from issue where id = #{integer} ")
IssuesEntity selectByPrimaryKey(Integer integer);
@Override
@Update("UPDATE `issue` SET `new_title` = #{newTitle} WHERE `id` = #{id} ")
int updateByPrimaryKeySelective(IssuesEntity record);
@Override
@Delete("delete from issue where id = #{integer}")
int deleteByPrimaryKey(Integer integer);
}
- 再次执行测试用例,希望结果时redis没有,数库存有
测试查询
@Test
void testById() {
IssuesEntity issuesEntity66 = crudFacade.byId(new IntIdInterface() {
@Override
public Integer id() {
return 66;
}
}, IssuesEntity.class);
IssuesEntity issuesEntity67 = crudFacade.byId(new IntIdInterface() {
@Override
public Integer id() {
return 67;
}
}, IssuesEntity.class);
}
id = 66 的用例希望从redis中获取,为了证明从redis中获取,修改一下属性方便确认
修改属性为
{"id":66,"newTitle":"mybatis_test_test_by_id"}
- 测试符合期望值
- id = 67 的用例希望从数据库获取并且放入redis
- redis 中加入成功符合期望
测试修改
@Test
void testEditor() {
IssuesEntity issuesEntity66 = crudFacade.byId(new IntIdInterface() {
@Override
public Integer id() {
return 66;
}
}, IssuesEntity.class);
issuesEntity66.setNewTitle("mybatis_editor_test");
crudFacade.editor(issuesEntity66);
}
- 希望redis 和数据库同时修改
- 修改一条redis中没有的数据,查看redis是否会被更新
@Test
void testEditor2() {
IssuesEntity issuesEntity66 = crudFacade.byId(new IntIdInterface() {
@Override
public Integer id() {
return 68;
}
}, IssuesEntity.class);
issuesEntity66.setNewTitle("mybatis_editor_test");
crudFacade.editor(issuesEntity66);
}
- redis 更新成功
测试删除
- redis 和 数据库都删除即可通过
@Test
void testDel() {
crudFacade.del(new IntIdInterface() {
@Override
public Integer id() {
return 67;
}
}, IssuesEntity.class);
}
- redis 和数据库都删除了
- 注意:这里是物理删除还是逻辑删除取决于
deleteByPrimaryKey
的sql
实体redis测试
@CacheKey(key = "tt", type = IssuesEntity.class, idFiled = "newTitle")
public class IssuesEntity {
private Integer id;
private String newTitle;
private String ooo() {
return "OOO" + this.newTitle;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNewTitle() {
return newTitle;
}
public void setNewTitle(String newTitle) {
this.newTitle = newTitle;
}
}
测试新增
@Test
void testInsert() {
IssuesEntity issuesEntity = new IssuesEntity();
issuesEntity.setNewTitle("insert");
crudEntityFacade.insert(issuesEntity);
}
测试查询
@Test
void testById() {
IssuesEntity issuesEntity = crudEntityFacade.byId(new StrIdInterface() {
@Override
public String id() {
return "insert";
}
}, IssuesEntity.class);
System.out.println();
}
测试修改
@Test
void testUpdate() {
IssuesEntity issuesEntity = crudEntityFacade.byId(new StrIdInterface() {
@Override
public String id() {
return "insert";
}
}, IssuesEntity.class);
issuesEntity.setId(999);
crudEntityFacade.editor(issuesEntity);
}
测试删除
@Test
void testDel() {
crudEntityFacade.del(new StrIdInterface() {
@Override
public String id() {
return "insert";
}
}, IssuesEntity.class);
}