springboot整合mybatisplus
环境声明
springboot-2.0.7.RELEASE
mybaits-plus-3.0.5
1.pom文件添加依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2.在入口类上添加@MapperScan(“hmjack.mapper”)
3.创建entity类
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
private Integer version;
}
@Data注解能够自动生成getting和setting方法
4.创建mapper类
@Repository
public interface UserMapper extends BaseMapper<User> {
}
@Repository注解能够让@Autowired注入时不报错
5.Test类测试mybatis-plus
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisplusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void contextLoads() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
}
当你将springboot高版本降低到低版本后出现Test不可用问题,你需要手动导入@Test的包:import org.junit.Test;
总结
mybatis-plus有很多很方便的功能,还有很多使用技巧等着你我去发现和使用。感谢你的阅读,希望这篇文章能给你带来一点点帮助。我是黑马Jack,用心学Java,不断学习新技术!