当前位置: 首页 > news >正文

南宁百度网站公司电话微信推广方式有哪些

南宁百度网站公司电话,微信推广方式有哪些,湘潭市建设网站,建立企业网站几天大家在日常开发中应该能发现,单表的CRUD功能代码重复度很高,也没有什么难度。而这部分代码量往往比较大,开发起来比较费时。 因此,目前企业中都会使用一些组件来简化或省略单表的CRUD开发工作。目前在国内使用较多的一个组件就是…

大家在日常开发中应该能发现,单表的CRUD功能代码重复度很高,也没有什么难度。而这部分代码量往往比较大,开发起来比较费时。

因此,目前企业中都会使用一些组件来简化或省略单表的CRUD开发工作。目前在国内使用较多的一个组件就是MybatisPlus.

当然,MybatisPlus不仅仅可以简化单表操作,而且还对Mybatis的功能有很多的增强。可以让我们的开发更加的简单,高效。

通过今天的学习,我们要达成下面的目标:

  • 能利用MybatisPlus实现基本的CRUD

  • 会使用条件构建造器构建查询和更新语句

  • 会使用MybatisPlus中的常用注解

  • 会使用MybatisPlus处理枚举、JSON类型字段

  • 会使用MybatisPlus实现分页

1.快速入门

为了方便测试,我们先创建一个新的项目,并准备一些基础数据。

1.1.环境准备

复制课前资料提供好的一个项目到你的工作空间(不要包含空格和特殊字符):

然后用你的IDEA工具打开,项目结构如下:

注意配置一下项目的JDK版本为JDK11。首先点击项目结构设置:

在弹窗中配置JDK:

接下来,要导入两张表,在课前资料中已经提供了SQL文件:

对应的数据库表结构如下:

最后,在application.yaml中修改jdbc参数为你自己的数据库参数:

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: MySQL123
logging:level:com.itheima: debugpattern:dateformat: HH:mm:ss

1.2.快速开始

比如我们要实现User表的CRUD,只需要下面几步:

  • 引入MybatisPlus依赖

  • 定义Mapper

1.2.1引入依赖

MybatisPlus提供了starter,实现了自动Mybatis以及MybatisPlus的自动装配功能,坐标如下:

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version>
</dependency>

由于这个starter包含对mybatis的自动装配,因此完全可以替换掉Mybatis的starter。 最终,项目的依赖如下:

<dependencies><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

1.2.2.定义Mapper

为了简化单表CRUD,MybatisPlus提供了一个基础的BaseMapper接口,其中已经实现了单表的CRUD:

因此我们自定义的Mapper只要实现了这个BaseMapper,就无需自己实现单表CRUD了。 修改mp-demo中的com.itheima.mp.mapper包下的UserMapper接口,让其继承BaseMapper

代码如下:

package com.itheima.mp.mapper;
​
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.mp.domain.po.User;
​
public interface UserMapper extends BaseMapper<User> {
}

1.2.3.测试

新建一个测试类,编写几个单元测试,测试基本的CRUD功能:

package com.itheima.mp.mapper;
​
import com.itheima.mp.domain.po.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
​
import java.time.LocalDateTime;
import java.util.List;
​
@SpringBootTest
class UserMapperTest {
​@Autowiredprivate UserMapper userMapper;
​@Testvoid testInsert() {User user = new User();user.setId(5L);user.setUsername("Lucy");user.setPassword("123");user.setPhone("18688990011");user.setBalance(200);user.setInfo("{\"age\": 24, \"intro\": \"英文老师\", \"gender\": \"female\"}");user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}
​@Testvoid testSelectById() {User user = userMapper.selectById(5L);System.out.println("user = " + user);}
​@Testvoid testSelectByIds() {List<User> users = userMapper.selectBatchIds(List.of(1L, 2L, 3L, 4L, 5L));users.forEach(System.out::println);}
​@Testvoid testUpdateById() {User user = new User();user.setId(5L);user.setBalance(20000);userMapper.updateById(user);}
​@Testvoid testDelete() {userMapper.deleteById(5L);}
}

可以看到,在运行过程中打印出的SQL日志,非常标准:

11:05:01  INFO 15524 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
11:05:02  INFO 15524 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
11:05:02 DEBUG 15524 --- [           main] c.i.mp.mapper.UserMapper.selectById      : ==>  Preparing: SELECT id,username,password,phone,info,status,balance,create_time,update_time FROM user WHERE id=?
11:05:02 DEBUG 15524 --- [           main] c.i.mp.mapper.UserMapper.selectById      : ==> Parameters: 5(Long)
11:05:02 DEBUG 15524 --- [           main] c.i.mp.mapper.UserMapper.selectById      : <==      Total: 1
user = User(id=5, username=Lucy, password=123, phone=18688990011, info={"age": 21}, status=1, balance=20000, createTime=Fri Jun 30 11:02:30 CST 2023, updateTime=Fri Jun 30 11:02:30 CST 2023)

只需要继承BaseMapper就能省去所有的单表CRUD,是不是非常简单!

1.3.常见注解

在刚刚的入门案例中,我们仅仅引入了依赖,继承了BaseMapper就能使用MybatisPlus,非常简单。但是问题来了: MybatisPlus如何知道我们要查询的是哪张表?表中有哪些字段呢?

大家回忆一下,UserMapper在继承BaseMapper的时候指定了一个泛型:

泛型中的User就是与数据库对应的PO.

MybatisPlus就是根据PO实体的信息来推断出表的信息,从而生成SQL的。默认情况下:

  • MybatisPlus会把PO实体的类名驼峰转下划线作为表名

  • MybatisPlus会把PO实体的所有变量名驼峰转下划线作为表的字段名,并根据变量类型推断字段类型

  • MybatisPlus会把名为id的字段作为主键

但很多情况下,默认的实现与实际场景不符,因此MybatisPlus提供了一些注解便于我们声明表信息。

1.3.1.@TableName

说明:

  • 描述:表名注解,标识实体类对应的表

  • 使用位置:实体类

示例:

@TableName("user")
public class User {private Long id;private String name;
}

TableName注解除了指定表名以外,还可以指定很多其它属性:

属性类型必须指定默认值描述
valueString""表名
schemaString""schema
keepGlobalPrefixbooleanfalse是否保持使用全局的 tablePrefix 的值(当全局 tablePrefix 生效时)
resultMapString""xml 中 resultMap 的 id(用于满足特定类型的实体类对象绑定)
autoResultMapbooleanfalse是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建与注入)
excludePropertyString[]{}需要排除的属性名 @since 3.3.1

1.3.2.@TableId

说明:

  • 描述:主键注解,标识实体类中的主键字段

  • 使用位置:实体类的主键字段

示例:

@TableName("user")
public class User {@TableIdprivate Long id;private String name;
}

TableId注解支持两个属性:

属性类型必须指定默认值描述
valueString""表名
typeEnumIdType.NONE指定主键类型

IdType支持的类型有:

描述
AUTO数据库 ID 自增
NONE无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUTinsert 前自行 set 主键值
ASSIGN_ID分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
ASSIGN_UUID分配 UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认 default 方法)
ID_WORKER分布式全局唯一 ID 长整型类型(please use ASSIGN_ID)
UUID32 位 UUID 字符串(please use ASSIGN_UUID)
ID_WORKER_STR分布式全局唯一 ID 字符串类型(please use ASSIGN_ID)

这里比较常见的有三种:

  • AUTO:利用数据库的id自增长

  • INPUT:手动生成id

  • ASSIGN_ID:雪花算法生成Long类型的全局唯一id,这是默认的ID策略

1.3.3.@TableField

说明:

描述:普通字段注解

示例:

@TableName("user")
public class User {@TableIdprivate Long id;private String name;private Integer age;@TableField("isMarried")private Boolean isMarried;@TableField("'concat'")private String concat;@TableField(exist = false)private String address;
}

一般情况下我们并不需要给字段添加@TableField注解,一些特殊情况除外:

  • 成员变量名与数据库字段名不一致

  • 成员变量是以isXXX命名,按照JavaBean的规范,MybatisPlus识别字段时会把is去除,这就导致与数据库不符。

  • 成员变量名与数据库一致,但是与数据库的关键字冲突。使用@TableField注解给字段名添加转义字符:``。

  • 成员变量不是数据库字段

支持的其它属性如下:

属性类型必填默认值描述
valueString""数据库字段名
existbooleantrue是否为数据库表字段
conditionString""字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s},参考(opens new window)
updateString""字段 update set 部分注入,例如:当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性)
insertStrategyEnumFieldStrategy.DEFAULT举例:NOT_NULL insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
updateStrategyEnumFieldStrategy.DEFAULT举例:IGNORED update table_a set column=#{columnProperty}
whereStrategyEnumFieldStrategy.DEFAULT举例:NOT_EMPTY where <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
fillEnumFieldFill.DEFAULT字段自动填充策略
selectbooleantrue是否进行 select 查询
keepGlobalFormatbooleanfalse是否保持使用全局的 format 进行处理
jdbcTypeJdbcTypeJdbcType.UNDEFINEDJDBC 类型 (该默认值不代表会按照该值生效)
typeHandlerTypeHander类型处理器 (该默认值不代表会按照该值生效)
numericScaleString""指定小数点后保留的位数

1.4.常见配置

MybatisPlus也支持基于yaml文件的自定义配置,详见官方文档:

https://www.baomidou.com/pages/56bac0/#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE

大多数的配置都有默认值,因此我们都无需配置。但还有一些是没有默认值的,例如:

  • 实体类的别名扫描包

  • 全局id类型

mybatis-plus:type-aliases-package: com.itheima.mp.domain.poglobal-config:db-config:id-type: auto # 全局id类型为自增长

需要注意的是,MyBatisPlus也支持手写SQL的,而mapper文件的读取地址可以自己配置:

mybatis-plus:mapper-locations: "classpath*:/mapper/**/*.xml" # Mapper.xml文件地址,当前这个是默认值。

可以看到默认值是classpath*:/mapper/**/*.xml,也就是说我们只要把mapper.xml文件放置这个目录下就一定会被加载。

例如,我们新建一个UserMapper.xml文件:

然后在其中定义一个方法:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mp.mapper.UserMapper">
​<select id="queryById" resultType="User">SELECT * FROM user WHERE id = #{id}</select>
</mapper>

然后在测试类UserMapperTest中测试该方法:

@Test
void testQuery() {User user = userMapper.queryById(1L);System.out.println("user = " + user);
}

文章转载自:
http://thermogenesis.stph.cn
http://embrocation.stph.cn
http://precisian.stph.cn
http://pompadour.stph.cn
http://breugel.stph.cn
http://thereafter.stph.cn
http://urethral.stph.cn
http://candytuft.stph.cn
http://anticoagulant.stph.cn
http://pectize.stph.cn
http://selenotropic.stph.cn
http://purify.stph.cn
http://tease.stph.cn
http://privately.stph.cn
http://crenate.stph.cn
http://lecithin.stph.cn
http://curia.stph.cn
http://minerva.stph.cn
http://sclerotium.stph.cn
http://catalan.stph.cn
http://antiracism.stph.cn
http://rivet.stph.cn
http://och.stph.cn
http://rhetian.stph.cn
http://graphotherapy.stph.cn
http://pergameneous.stph.cn
http://proprietary.stph.cn
http://adminiculate.stph.cn
http://aggrieve.stph.cn
http://kavakava.stph.cn
http://hibernia.stph.cn
http://yum.stph.cn
http://fleuret.stph.cn
http://proceeds.stph.cn
http://podagric.stph.cn
http://phanerogam.stph.cn
http://humor.stph.cn
http://bemean.stph.cn
http://oateater.stph.cn
http://elamitic.stph.cn
http://sarcoma.stph.cn
http://rotterdam.stph.cn
http://thelma.stph.cn
http://nasturtium.stph.cn
http://nonabsorbable.stph.cn
http://swain.stph.cn
http://nominalist.stph.cn
http://dweller.stph.cn
http://manx.stph.cn
http://subfuscous.stph.cn
http://railfan.stph.cn
http://firecrest.stph.cn
http://backwood.stph.cn
http://spatterdash.stph.cn
http://superintend.stph.cn
http://trike.stph.cn
http://brule.stph.cn
http://halafian.stph.cn
http://scilly.stph.cn
http://polyglottal.stph.cn
http://adulterer.stph.cn
http://reign.stph.cn
http://grosz.stph.cn
http://genevese.stph.cn
http://proteoglycan.stph.cn
http://radioiodine.stph.cn
http://platemaker.stph.cn
http://rectorate.stph.cn
http://gemsbuck.stph.cn
http://heathberry.stph.cn
http://hypermetrical.stph.cn
http://herdic.stph.cn
http://calorescence.stph.cn
http://educability.stph.cn
http://nasally.stph.cn
http://quince.stph.cn
http://legit.stph.cn
http://zoroaster.stph.cn
http://pleurodont.stph.cn
http://revulsion.stph.cn
http://flackery.stph.cn
http://alimentotherapy.stph.cn
http://ammoniate.stph.cn
http://smuggle.stph.cn
http://epicotyledonary.stph.cn
http://galvanistical.stph.cn
http://camphor.stph.cn
http://peptid.stph.cn
http://amplectant.stph.cn
http://lingy.stph.cn
http://youthwort.stph.cn
http://suntandy.stph.cn
http://synergic.stph.cn
http://trunnel.stph.cn
http://dropsical.stph.cn
http://popeye.stph.cn
http://tegestology.stph.cn
http://suttle.stph.cn
http://spessartite.stph.cn
http://slapdashery.stph.cn
http://www.15wanjia.com/news/65199.html

相关文章:

  • 做网站建设有哪些公司好优化算法
  • 网站建设搭建是什么意思对网站和网页的认识
  • 手机网站如何跳转网站优化公司哪家效果好
  • wordpress导入word游戏优化是什么意思?
  • 光谷做网站推广怎么样公司网络推广营销
  • 宝鸡市住房和城乡建设部网站营销宝
  • 在建项目经理查询网站优化排名软件网站
  • 海口的网站建设网络营销职业规划300字
  • 玉林建设信息网站营销活动推广策划
  • 网站开发任务成都培训机构排名前十
  • 2014网站设计怎么快速推广自己的产品
  • 网站如何做会员通用无锡百度推广代理商
  • 厦门网站建设维护seo外链在线提交工具
  • 网站里的活动专题栏怎么做seo排名赚app官网
  • 西安网站优化服务域名查询ip爱站网
  • 建设一个网站需要考虑什么seo好找工作吗
  • 北京公司网站制作哪家专业化工网站关键词优化
  • 德宏企业网站建设公司6seo外链发布
  • 备案 网站建设方案书广东网络推广运营
  • 做外贸网站需要注意些什么问题什么是互联网营销
  • 网站建设准备工作总结百度指数分析工具
  • php网站微信支付怎么做软文营销文章案例
  • 上海seo搜索优化优化什么意思
  • 水果网页设计代码电脑系统优化软件十大排名
  • 网站建设与管理小论文seo在线优化网站
  • 快递物流网站建设开发具备哪些功能最近的重大新闻
  • 电脑 手机网站建站个人域名注册流程
  • 许昌住房和城乡建设部网站seo知识培训
  • 一站式做网站sem是什么职业
  • 站长工具介绍游戏推广是干什么的