快速使用mybatis plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
导入这几个包
在application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
logging.level.com.springboot.dao=debug
#在日志里面显示出执行的sql语句,logging.level.具体的包名
然后创建一个dao包里面写mapper
public interface UserMapper extends BaseMapper<User> {
}
最后单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Resource
private UserMapper userMapper;
@Test
public void find(){
userMapper.selectById(1);
}
}
后面一些具体的mybatisplus配置,比如
classpath:/com/yourpackage/*/mapper/*Mapper.xml
现在还不是很懂, 加上了反而报错,以后再来补充。
一个很重要的问题:
mysql的包一定要导入5开头的版本,当初导入了8开头的版本,一直报错
还有一个实体类问题:
一定要在主键前面加@TableId()不然在命名下划线的属性就会报错
//该注解添加在Id上(准确来说是添加在主键上)
@TableId(value = "id",type = IdType.AUTO)//指定自增策略
//该注解添加在成员变量上,若没有开启驼峰命名,或者表中列名不符合驼峰规则,可通过该注解指定数据库表中的列名(value),exist标明数据表中有没有对应列,因为使用通用mapper时传入参数时一个对象而非对象的各项数据。
@TableField(value = "last_name",exist = true)
作者:dreamlike
链接:https://juejin.im/post/5d933421f265da5b555f56ad
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。