关于电商网站规划方案,四种营销策略,品牌开发者应掌握的技能,自己接单赚钱app#x1f40c;个人主页#xff1a; #x1f40c; 叶落闲庭 #x1f4a8;我的专栏#xff1a;#x1f4a8; c语言 数据结构 javaEE 操作系统 Redis 石可破也#xff0c;而不可夺坚#xff1b;丹可磨也#xff0c;而不可夺赤。 MyBatisPlus 一、快速入门1.1 引入MyBatisP… 个人主页 叶落闲庭 我的专栏 c语言 数据结构 javaEE 操作系统 Redis 石可破也而不可夺坚丹可磨也而不可夺赤。 MyBatisPlus 一、快速入门1.1 引入MyBatisPlus起步依赖1.2 自定义的Mapper继承MyBatisPlus的BaseMapper接口1.3 对比Mybatis1.4 MyBatisPlus的增删改查方法 二、MyBatisPlus常用注解2.1 MyBatisPlus常用注解如下 三、MyBatisPlus常用配置 一、快速入门 MyBatisPlus官方提供了starter,其中集成了Mybatis和MybatisPlus的所有功能并且实现了自动装配效果。 1.1 引入MyBatisPlus起步依赖
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.2/version
/dependency1.2 自定义的Mapper继承MyBatisPlus的BaseMapper接口
public interface UserMapper extends BaseMapperUser {
}1.3 对比Mybatis
要操作的数据库表结构
create table user
(id int not null primary key,account int null
);Mybatis的UserMapper.java:
public interface UserMapper {int insert(User row);int insertSelective(User row);User selectByPrimaryKey(Integer id);int updateByPrimaryKeySelective(User row);int updateByPrimaryKey(User row);
}Mybatis的UserMapper .xml
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.demo.mapper.UserMapperresultMap idBaseResultMap typecom.demo.po.User/resultMapsql idBase_Column_Listid, account/sqlselect idselectByPrimaryKey parameterTypejava.lang.Integer resultMapBaseResultMapselect include refidBase_Column_List /from userwhere id #{id,jdbcTypeINTEGER}/selectinsert idinsert parameterTypecom.demo.po.Userinsert into user (id, account)values (#{id,jdbcTypeINTEGER}, #{account,jdbcTypeINTEGER})/insertinsert idinsertSelective parameterTypecom.demo.po.Userinsert into usertrim prefix( suffix) suffixOverrides,if testid ! nullid,/ifif testaccount ! nullaccount,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testid ! null#{id,jdbcTypeINTEGER},/ifif testaccount ! null#{account,jdbcTypeINTEGER},/if/trim/insertupdate idupdateByPrimaryKeySelective parameterTypecom.demo.po.Userupdate usersetif testaccount ! nullaccount #{account,jdbcTypeINTEGER},/if/setwhere id #{id,jdbcTypeINTEGER}/updateupdate idupdateByPrimaryKey parameterTypecom.demo.po.Userupdate userset account #{account,jdbcTypeINTEGER}where id #{id,jdbcTypeINTEGER}/update
/mapperMyBatisPlus只需UserMapper.java继承 BaseMapper即可
public interface UserMapper extends BaseMapperUser {
}1.4 MyBatisPlus的增删改查方法
SpringBootTest
class DemoApplicationTests {Autowiredprivate UserMapper userMapper;Testvoid testInsert() {User user new User();user.setId(5);user.setAccount(2000);userMapper.insert(user);}Testvoid testSelectById() {User user userMapper.selectById(5);System.out.println(user);}Testvoid testUpdateById() {User user new User();user.setId(5);user.setAccount(8000);userMapper.updateById(user);}Testvoid testDeleteById() {userMapper.deleteById(5);}}
二、MyBatisPlus常用注解 MyBatisPlus:通过扫描实体类并基于反射获取实体类信息作为数据库表信息。 类名驼峰转下划线作为表名名为id的字段作为主键变量名驼峰转下划线作为表的字段名
2.1 MyBatisPlus常用注解如下
TableName用来指定表名TableId用来指定表中的主键字段信息TableFiled用来指定表中的普通字段信息 MyBatisPlus官网https://www.baomidou.com/pages/223848/#tablename 三、MyBatisPlus常用配置 MyBatisPlus中的配置大都是默认配置好的我们使用的时候基本不用修改大量的配置除非遇到特殊的情况需要设置一些配置可以参考MyBatisPlus的官方文档进行修改。 mybatis-plus:type-aliases-package: com.demo.pomapper-locations: classpath*:mapper/**/*.xml # 默认global-config:db-config:id-type: auto # id类型自增长