spring中配置mybatis-plus
- 在pom.xml中下载依赖包mybatis-plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.0</version>
</dependency>
- 在实体类中给注解
(1)类的注解方式@TableName(数据库表名)
(2)id的注解方式@TableId(value = “数据库id名字”,type = IdType.AUTO)
(3)其他属性的注解方式@TableField(“数据库属性名字”)
@TableName("t_product")
public class Product {
@TableId(value = "pid",type = IdType.AUTO)
private long pid;
@TableField("p_ptitle")
private String ptitle;
- 在Mapper层中继承BaseMapper泛型必须要给上一步实体类的类名
public interface ProductMapper extends BaseMapper<Product> {
- 在service层中要分两步走
(1)service层要继承Iservice这里的泛型也必须给,跟上面同样
public interface ProductService extends IService<Product> {
(2)在service的实现层serviceimpl中要继承ServiceImpl<ProductMapper,Product>这里的泛型有两个,分别是Maaper和实体类,实现层肯定要实现ProductService
public class ProduceServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService {
- 这样就配置好了,然后去测试
(1)建一个测试类 我这里用的是注解 具体步骤如代码所示,在这里我只是简单的测试配置成功没,还有很多方法没有测试,自己可以去测试一下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test4 {
@Autowired
UserServicr userServicr;
@Test
public void test1(){
List<User> list = userServicr.list(new QueryWrapper<User>());
for (User user : list) {
System.out.println(user);
}
}
}
如果对你有所帮助,麻烦点赞支持一下,谢谢