注意:对于性别gender字段的值只有男和女两种情况,tinyint(1)类型使用场景为只有true和false两种情况的, 这种最好值为0或者1,不要写成1或者2之类的,如果想写成1或者2最好将类型改为tinyint(2)。如果tinyint(1)设置值为1或者2在MyBatis-Plus中会有问题,可能会认为这是一个boolean值,会将1翻译为true,将2也翻译为true,感觉很奇怪。
INSERT INTO `tbl_user` VALUES (1, 'laolu', '陆汉卿', 30, 0, NULL, '六哥的上线,是一名郎中,也是一名坚定的地下党员', 0, 0, 3, 1, '2020-10-04 12:14:55', 1, '2020-10-04 12:14:55'); INSERT INTO `tbl_user` VALUES (2, 'sixbrother', '六哥', 36, 0, NULL, '内敛隐忍、狡黠机智且心狠手辣', 0, 0, 1, 1, '2020-10-04 11:17:53', 1, '2020-10-04 11:28:40'); INSERT INTO `tbl_user` VALUES (3, 'gongshu', '宫庶', 29, 0, NULL, '郑耀先的弟子,继承郑耀先衣钵', 0, 0, 1, 2, '2020-10-04 11:19:15', 1, '2020-10-04 11:19:51'); INSERT INTO `tbl_user` VALUES (4, 'xiaowu', '马小五', 28, 0, NULL, NULL, 0, 0, 1, 2, '2020-10-04 11:21:25', 1, '2020-10-04 11:21:25'); INSERT INTO `tbl_user` VALUES (5, 'xiaoan', '宋孝安', NULL, 0, NULL, '郑耀先原先在军统内部的好兄弟,为人重情重义', 0, 0, 1, 2, '2020-10-04 11:23:33', 1, '2020-10-04 11:23:33'); INSERT INTO `tbl_user` VALUES (6, 'xubaichuan', '徐百川', NULL, 0, NULL, '军统特务戴笠的实力助手之', 0, 0, 1, 2, '2020-10-04 11:24:50', 1, '2020-10-04 11:24:50'); INSERT INTO `tbl_user` VALUES (7, 'lintao', '林桃', NULL, 1, NULL, '国民党中统女特工,代号“剃刀”', 0, 0, 1, 2, '2020-10-04 11:26:09', 1, '2020-10-04 11:26:09');引入mybatis对应的spring-boot依赖。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 可选,不是必须的 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>注意:日志级别trace比debug更低,如果想看更详细的日志可以配置成trace级别。
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf8 username: root password: root123 mybatis: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/*.xml logging: level: com.example.mybatisplus.mapper: debugSQL2Java 在线生成工具 : 通过建表sql生成对应的实体类。
注意,注意,注意:如果字段名是mysql关键字的话在执行sql时会报错BadSqlGrammarException,可以使用@TableField("字段名") 来对字段名加上引号。
@Data @AllArgsConstructor @RequiredArgsConstructor public class User implements Serializable { private static final long serialVersionUID = 1L; /** * 主键 */ private Long id; /** * 用户名 */ private String username; /** * 姓名 */ private String name; /** * 年龄 */ private Integer age; /** * 性别(1:男,2:女) */ private Integer gender; /** * 余额 */ private BigDecimal money; /** * 备注 */ private String remark; /** * 状态(0:正常,1:冻结,2:关闭) */ private Integer status; /** * 是否删除(0: 未删除,1:已删除) */ private Integer deleted; /** * 版本号 */ private Long version; /** * 创建人id */ private Long createId; /** * 创建时间 */ private Date createTime; /** * 修改人id */ private Long updateId; /** * 修改时间 */ private Date updateTime; }这里提供两个方法,一个方法将SQL写在xml中,另一个方法将SQL写在注解里。 注解的好处:当SQL非常简单时将SQL写在接口中减少了接口和XML文件的切换。
public interface UserMapper { List<User> findAll(); @Select("select * from tbl_user where id = #{id}") User findById(@Param("id") Long id); }使用@MapperScan注解配置要扫描的mapper接口所在的包位置。
@SpringBootApplication @MapperScan("com.example.mybatisplus.mapper") public class SpringbootMybatisplusApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisplusApplication.class, args); } }去掉 mybatis-spring-boot-starter 依赖使用 mybatis-plus-boot-starter。 注意:mybatis-plus-boot-starter和mybatis-spring-boot-starter不能同时存在,同时存在可能会导致Jar包冲突,项目启动不了。
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency>将原来的mybatis该为mybatis-plus。
mybatis-plus: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/*.xmlBaseMapper中为我们提供了一套通用的增删改查方法。
import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapper<User> { List<User> findAll(); @Select("select * from tbl_user where id = #{id}") User findById(@Param("id") Long id); }使用@TableName注解配置User实体所对应的表名。配置表名可以使用@TableName注解,也可以配置全局的表前缀,如果配置了全表前缀就不需要再使用@TableName进行注解了。只有当数据库中的所有表的前缀都一样是配置全局前缀才有意义,个人不太建议配置全局表前缀。
mybatis-plus.global-config.db-config.table-prefix=tbl_ @TableName("tbl_user") public class User implements Serializable { }直接调用BaseMapper中提供的方法来实现增删改查,我们不需要写SQl,只需要调用方法就可以完成,这就是MyBatis-Plus最核心的最重要的功能。
@SpringBootTest class SpringbootMybatisplusApplicationTests { @Autowired private UserMapper userMapper; @Test void testMyBatisPlus() { // SELECT id,username,name,age,...,update_time FROM tbl_user List<User> users = userMapper.selectList(null); System.out.println(users); } }