步骤:
- 1,导入依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
- 2.使用mybatisplus的日志打印 application.properties
#配置mybatis-plus的日志打印
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
- 3.编写实体类
/**
*
* @author Sir.tang
集成Lombok 优化实体类
* @return
* @exception
* @date 2021/5/10 9:18
*/
@Data //它包含getter、setter注解,即当使用当前注解时,会自动生成包含的所有方法;
@AllArgsConstructor //生成对应的有参构造方法
@NoArgsConstructor //生成对应的无参构造方法
public class User {
/*ID_WORKER 默认生成id方式 全局唯一id*/
@TableId(type =ID_WORKER )
private Long id;
private String name;
private Integer age;
private String email;
/*标注这个创建时间只会在 数据库生成第一条记录时生成*/
@TableField(fill = INSERT)
private Date createTime;
/*标注这个创建时间只会在 数据库修改记录时生成*/
@TableField(fill = INSERT_UPDATE)
private Date updateTime;
}
- 4.编写mapper接口 mapper已经有很多自带的方法
需要继承BaseMapper它里面是个泛型, @Mapper 表示这是dao
/*User实体对应的 Mapper接口*/
@Mapper //表示这是dao
public interface UserMapper extends BaseMapper<User> {
@Select("select * from User where name = #{jone}")
User selects(@Param("jone") String jone);
@Select({"<script>",
"SELECT * FROM User",
"WHERE 1=1",
"<when test='Jack!=null'>",
"AND name = #{Jack}",
"</when>",
"</script>"})
User selects2(@Param("Jack") String Jack);
}
- 5.测试:
@Test
void contextLoad66( ) {
/* List<User> users = userMapper.selectList(null);
users.forEach(System.out::print);
*/
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
String jone = "Jone";
String jone1 = "Jack";
User selects = userMapper.selects( jone);
User selects2 = userMapper.selects2( jone1);
//断言预期值与实际值符合不
Assert.assertEquals(8, userList.size());
userList.forEach(System.out::println);
System.out.println(selects2);
}
图片.png
网友评论