VHR遇到的新知识

VHR遇到的新知识

MyBatis

SQL

批量添加

insert into menu_role (rid,mid) values (13,7) , (13,9) 

对应到mybatis的xml写法

<insert id="insertByRid">
        insert into menu_role (rid,mid)
        values
        <foreach collection="mids" separator="," item="mid">
            (#{rid},#{mid})
        </foreach>
    </insert>

Myabitais的@param使用场景

Mybatis中的@param注解的使用场景:

  • 方法有多个参数
  • 方法参数要取别名
  • XML 中的 SQL 使用了 $
  • 动态sql中参数是非自定义pojo类型

MyBatis提供的懒加载递归

<resultMap id="BaseResultMap2" type="com.fern.vhr.model.Department" extends="BaseResultMap">
    <collection property="children" javaType="java.util.List" 		ofType="com.fern.vhr.model.Department" select="getDepartments" column="id" fetchType="lazy">

    </collection>
 </resultMap>

解释如下:

  • select="com.fern.vhr.mapper.DepartmentMapper.getDepartments"要获取children属性时,会自动调用getDepartments(id) 函数,而cloumn=‘id’就是要出入值的属性名
  • fetchType="lazy"开启懒加载,不想开启懒加载删掉即可

SpringMVC

@RequestBody:

  • 主要用来接收前端传递给后端的json字符串中的数据的;
  • 使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。

MySQL存储过程

department表

id name parentId depPath enabled isParent
1 股东会 -1 .1 1 1
4 董事会 1 .1.4 1 1
5 总办 4 .1.4.5 1 1
8 财务部 5 .1.4.5.8 1 0
78 市场部 5 .1.4.5.78 1 1
81 华北市场部 78 .1.4.5.78.81 1 1
82 华南市场部 78 .1.4.5.78.82 1 0
85 石家庄市场部 81 .1.4.5.78.81.85 1 0
86 西北市场部 78 .1.4.5.78.86 1 1
87 西安市场 86 .1.4.5.78.86.87 1 1
89 莲湖区市场 87 .1.4.5.78.86.87.89 0
91 技术部 5 .1.4.5.91 1 0
92 运维部 5 .1.4.5.92 1 0
94 华东市场地区 78 .1.4.5.78.94 1 0

### Sqlyog和命令行的写法

# 自定义结束符号
# 告诉MySQL解释器,该段命令是否已经结束了,mysql是否可以执行了。默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该命令。
DELIMITER $$
# 这个很简单表示使用哪一个数据库,使用我们自定义的符号作为结束符号
USE `vhr` $$
# 如果存在addDep那么就将其删除
DROP PROCEDURE IF EXISTS `addDep` $$
# 设置这个addDep函数拥有root的权限
# addDep(in depName varchar(32),in parentId int,in enabled boolean,out result int,out result2 int)
# 带有in的参数代表是输入参数,也就是需要传入的参数
# 带有out的参数代表输出参数,也就是返回出去的值
CREATE DEFINER =`root`@`localhost` PROCEDURE `addDep`(in depName varchar(32),int parentId int,int enabled boolean,out result int out result2 int)

BEGIN
	# 声明一个int类型变量
	declare did int;
	# 声明一个varchar类型的变量,每句话必须以;号结束否则会出错
	declare pDepPath varchar(64);
	# 插入一条数据
	insert into department set name=depName,parentId=parentId,enabled=enabled;
	# 查出受影响的函数,赋给result
	select row_count() into result;
	# 查出刚刚插入的一条记录的主键 赋给did
	select last_insert_id() into did;
	# 赋值
	set result2=did;
	# 根据父节点Id查出父节点的dePath并赋值给我们定义的变量pDepPath
	select dePath into pDepPath from department where id=parentId;
	# 根据刚刚插入记录的主键修改该记录的depPath    父节点的depPath+自己的id
	# concat()是MySQL的函数
	update department set depPath=concat(pDepPath,'.',did) where id=did;
	# 将新增记录的上一级节点变成父节点
	update department set isParent=true where id=parentId;
END$$
# 再将结束符号还原
DELIMITER;

Navicat写法

  • navicat编辑器把与业务逻辑无关的代码都封装了,只需要写BEGIN-END之间的代码

在这里插入图片描述

在这里插入图片描述
Navicat值得注意的地方是:创建存储过程时定义varchar类型的参数
需要在创建好时修改varchar的大小。创建时指定不了类型大小不知道为啥
在这里插入图片描述

调用存储过程

在这里插入图片描述

MyBtais调用存储过程

不管删除还是添加都是用<select></select>

<select id="addDepartment" statementType="CALLABLE">
    call addDep(#{name,mode=IN,jdbcType=VARCHAR},
            #{parentid,mode=IN,jdbcType=INTEGER},
            #{enabled,mode=IN,jdbcType=BOOLEAN},
            #{result,mode=OUT,jdbcType=INTEGER},
            #{result2,mode=OUT,jdbcType=INTEGER})
  </select>


  <select id="deleteDepartment" statementType="CALLABLE">
    call deleteDep(#{id,mode=IN,jdbcType=INTEGER},#{result,mode=OUT,jdbcType=INTEGER})
  </select>

剩下写法就和我们平时写法一样了

String的format方法

// 补齐空格并右对齐:
String.format("%10s, world", "Hello");     // 输出 "     Hello, world"
String.format("%8d", 123);                 // 输出 "     123"

// 补齐空格并左对齐:
String.format("%-10s, world", "Hello");    // 输出 "Hello     , world"
String.format("%-8d", 123);                // 输出 "123     "

// 补齐 0 并对齐(仅对数字有效)
String.format("%08d", 123);                // 输出 "00000123"
String.format("%-08d", 123);               // 错误!不允许在右边补齐 0

// 输出最多N个字符
String.format("%.5s", "Hello, world");       // 输出 "Hello"
String.format("%.5s...", "Hello, world");    // 输出 "Hello..."
String.format("%10.5s...", "Hello, world");  // 输出 "     Hello..."

// 输出逗号分隔数字
String.format("%,d", 1234567);               // 输出 "1,234,567"

上面这些还是非常好用的

具体使用方法请点击访问

猜你喜欢

转载自blog.csdn.net/qq_35953966/article/details/104936132