6.2
- select distinct
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。
关键词 DISTINCT 用于返回唯一不同的值。 - foreach标签
public List<Entity> queryEntity(List<Long> userids);
<select id="queryEntity" resultMap="BaseResultMap">
select * form Entity
where id in
<foreach collection="userids" item="userid" index="index" open="(" close=")" separator=",">
#{userid}
</foreach>
</select>
String getSpuId(@Param("sku") List<Long> sku);
<select id="getSpuId" resultType="java.lang.String">
select distinct spu_code from sku
where status = 1
<if test="sku != null">
and sku_code in
<foreach collection="sku" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
</select>
- concat模糊查询
<select id="query" resultMap="BaseResultMap" paramType="Entity">
select * from entity
<where>
<if test="name != null">
name like ('%',concat(#{name}),'%')
</if>
</where>
</select>
List<Question> list<@Param("keys") String keys,@Params("pageBean") PageBean pageBean);
<select id="list" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from question
where is_delete=0
<if test="keys!=null and keys !=''">
and (title like concat('%',#{keys},'%') or 'keys' like concat('%',#{keys},'%'))
</if>
order by update_time desc
limit #{pageBean.offset},#{pageBean.pageSize}
</select>
6.4
-
IDEA中的run和debug,在debug模式下,有些修改是热启动的,而有些情况又不是。
适用的情况:
- 修改方法体内的内容,实例方法或是静态方法。
不适用的情况:
- 新增、删除实例方法、静态方法时。
- 新增、删除实力变量、类变量时。
- 引入了新创建的内部类或外部类时。
- 方法当中有对变量或实例变量的引用,或是当外部变量被修改时。
参考 -
在Intellij IDEA中使用debug 参考
6.5
- 在IDEA中使用LeetCode插件
- LeetCode:(数组)两数之和。参考
//数组 int nums[] ,目标值 target,在数组中找出两个和为目标值的数,返回下标
//方法一:暴力法: 时间复杂度 O(n^2) 空间复杂度 O(1)
{
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j] == target){
return new int[]{i,j};
}
}
}
throw new IllegalArgumentException("no TwoSum solution");
}
//方法二:用hashmap(两次),利用键值的唯一对应关系 时间复杂度 O(n) 空间复杂度 O(n)
{
Map<Integer,Integer>map=new hashMap<>();
for(int i=0;i<nums.length;i++){
map.put(nums[i],i);
}
for(int i=0;i<nums.length;i++){
int another=target-nums[i];
if(map.containsKey(another) && map(another) != i){
return new int[]{i,map.get(another)};
}
}
throw new IllegalArgumentException("no TwoSum solution");
}
//方法三:用hashmap(一次),空间复杂度 O(n),空间复杂度 Oo(n)
{
Map<Integer,Integer> map=new hashMap<>();
for(int i=0;i<nums[i];i++){
int another = target - nums[i];
if(map.containsKey(another) && map.get(another) != i){
return new int[]{map.get(another),i};
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("no TwoSum solution");
}
6.8
- (流式计算)获取集合里面的某个字段再组成普通LIst集合
public class A {
private Long id;
}
//现有集合 as
List<A> as
//故可以写成
List<Long> ids = as.steam().map(A::getId).collect(Collectors.toList());
- (流式计算) 获取集合里的某些字段再组成map集合
public class B {
private Long id;
private String name;
}
//现有集合 bs
List<B> bs
//新建容器
Map<Long,String> map = new HsahMap<>();
//故可以写成
map = bs.stream().collet(Colletors.toMap(B::getId,B::getName));
- (流式计算)某集合B,现把B.id为键,B为值组成map集合
static <T> Function<T, T> identity(){
return t -> T;
}
public class B {
private Long id;
private String name;
}
//现有集合 bs
List<B> bs
//故可以写成
Map<Long,B> map = bs.stream().collect(Collectors.toMap(B::getId,Function.identity()));
6.9
- stream入门练习 参考
- filter 筛选
//新建Student类
public class Student{
private int id;
private String name;
private int age;
private String address;
//无参、有参、get、set、tostring
}
// >>>filter<< (筛选)
Student s1 = new Student(1L,"kenny",15,"浙江");
Student s2 = new Student(2L,"jay",15,"江苏");
Student s3 = new Student(3L,"turtle",17,"上海");
Student s4 = new Student(4L,"kongfu",17,"北京");
students.add(s1);
..
..
//筛选年龄大于15的学生
List<Student> result = students.stream().filter(s -> s.getAge()>15).collect(Collectors.toList());
//筛选住在浙江省的学生
List<Student> result = students.stream().filter(s -> "浙江".equals(s.getAddress())).collect(Collectors.toList());
- map 转换
Student s1 = new Student(1L,"kenny",15,"浙江");
Student s2 = new Student(2L,"jay",15,"江苏");
Student s3 = new Student(3L,"turtle",17,"上海");
Student s4 = new Student(4L,"kongfu",17,"北京");
students.add(s1);
..
..
//在地址前面加上部分信息,只获取地址输出
List<Student> result = students.stream().map(s -> "地址:"+s.getAddress()).collect(Collectors.toList())
//map就是将对应的元素按照给定的方法进行转换
- distinct 去重
List<String> list = Arrays.asList("11","22","33","11");
list.stream().distinct().forEach(System.out::println);
- sorted 排序
List<String> list = Arrays.asList("33","22","11");
list.stream().sorted().forEach(System.out::println); //33 22 11
//------------------------------------------------------------------
students.stream()
.sorted(stu1,stu2) -> Long.compare(stu2.getId(),sut1.getId())
.sorted(stu1,stu2) -> String.compare(stu2.getAge(),sut2.getAge())
.forEach(System.out::println);
//指定排序规则,先按照学生的id进行降序排序,再按照年龄进行降序排序
- limit 限制返回个数
//返回集合前几个元素
List<String> list = Arrays.asList("33","22","11");
list.stream().limit(2).forEach(System.out::println); //33 22
- skip 删除元素
//删除前两个元素
List<String> list = Arrays.asList("33","22","11");
list.stream().skip(2).forEach(System.out::println); // 11
- reduce 聚合
List<String> list = Arrays.asList("不相","信眼","泪");
String result = lsit.reduce("互联网",(a,b) -> (a+b);
sout(result);//互联网不相信眼泪
- min 最小值
//获取年龄最小的学生
//学生集合 stuents
Student minAgeStu = students.stream().min((stu1,stu2) -> Integer.compare(stu1.getAge(),stu2.getAge()).toget();
//最大值 max同理
- anyMatch / allMatch /noneMatch 匹配
//学生集合 students
//是否有浙江人
Boolean amyMatch = students.stream().anyMatch(s -> "浙江".equals(s.getAdress()));
//是否所有学生都满15岁
Boolean allMatch = students.stream().allMatch(s -> s.getAge()>15);
//是否没有人叫kenny
Boolean nonMatch = students.stream().nonMatch(s -> "kenny".equals(s.getName()));
- IntValue()方法
//输出int数据
Double s = 2.5;
Float k = 2.7f;
System.out.println(k.intValue());//2
System.out.println(s.intValue());//2
//就是丢掉小数位
- BigDecimal(num).setScale ROUND_HALF_UP
String num = "176.5555";
System.out.println(new BigDecimal(num).setScale(3.BigDecimal.ROUND_HALF_UP));//176.556
double num = "176.5555";
System.out.println(new BigDecimal(num).setScale(3.BigDecimal.ROUND_HALF_UP));//176.555
//故为确保准确性,尽量使用String作为传入值
- Java.math.BigDecimal.subtract()
BigDecimal bd1,bd2;
bd1 = new BigDecimal("100.123");
bd2 = new BigDecimal("50.56");
System.out.println(bd1.subtract(bd2));//49.563
6.22
- 在表里增加字段
ALTER TABLE 'sku'
ADD COLUMN 'goods_style' tinyint(4) NOT NULL DEFAULT 0 COMMENT '商品风格(0-普通商品;1-组合商品)';
- 新增表格
CREATE TABLE 'sku_combined' (
'id' int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
'sku_id' int(11) NOT NULL DEFAULT '0' COMMENT '组合商品主键',
...
'goods_price' decimal(32,2) NOT NULL DEFAULT '0.00' COMMENT '销售价',
'create_user' varchar(32) NOT NULL DEFAULT '' COMMENT '创建人',
'create_time' datetime NOT NULL COMMENT '创建时间',
PRIMARY KEY (’id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT = '组合商品sku关系';
- 遍历集合
List<carProduct> list = xxxxxservice.getListProduct(id);
list.forEach(item -> {
xxxIdList.add(item.getId());
xxxMap.put(item.getSkuId(),item.getTopicId());
...
...
});
- 将集合中某个对象的指定值,封装成list
public class CollectionExtUtil{
...
...
/**
*更符合java8理念的方式,将集合中对象的指定field value封装成list
* T:结果对象泛型
* F:原集合对象泛型
*/
public static <T, F, C extends Collection<F>> List<T> getPropertyList(C c, Function<? super F, ? extends T> mapper){
if(CollectionUtils.isEmpty(c)){
return Collections.emptyList();
}
return c.stream().map(mapper).collect(Collections.toList());
}
}
..
..
/**
*使用
*/
List<SkuEntity> skus = skuService.listByIdList(skuIdList);
List<Long> spuCodeList = CollectionExtUtil.getPropertyList(skus,SkuEntity::getSpuCode);
- 统计现金应该支付的金额
/**
*有一商品集合list,用户积分credit,售价goodsPrice,数量quantity
*商品A,售价x元,可用积分y,商品数量z
*商品B,售价xx元,可用积分yy,商品数量zz
*商品B,售价xxx元,可用积分yyy,商品数量zzz
*...
*公式为 ((售价-积分)乘数量)遍历累和
*/
long shouldCashPay = list.stream().mapToLong(item -> {
Long credit = item.getCredit();
if(credit == null || credit != 0){
return 0;
}
long margin = item.goodsPrice() - credit;
if(margin < 0){
return 0;
}
return margin * item.getQuantity();
}).sum();
6.24
- (Java8)函数是一等值,将方法作为函数式值来传递
现在有一个Apple类
//写两个方法作判断
public static boolean isGreenApple(Apple apple){
return "green".equals(apple.getColor);
}
public static boolean isHeavyApple(Apple apple){
return apple.getWeight()>150;
}
//Java8会把条件代码作为参数传递进去
public interface predicate<T>{
boolean test(T t);
} //方法作为predicate参数p传递进去
static List<Apple> filterApples(List<Apple> inventory,predicate<Apple> p){
List<Apple> result = new arrayList<>();
for(Apple apple : inventory){
if(p.test(apple){//苹果符合p所代表的条件吗
result.add(apple);
}
}
return result;
}
//当要使用这个方法的时候,可以这样
List<Apple> filterColer = filterApples(inventory, Apple::isGreenApple);
List<Apple> filterWeight = filterApples(inventory, Apple::isHeavyApple);
- 关于java8第一章 的笔记,(Stream和编程思想)
- 显而易见,Stream API和现有的集合API的行为都是差不多:他们都能访问数据项目的序列。不过有一点需要注意,
Collection主要是为了存储和访问数据,而Stream则主要是用于描述对数据的计算
(这个也很好理解,我们使用Stream就是为了应对各种各样的计算场景) - 筛选一个Collection的最快方法常常是将其转化为Stream,进行并行处理,然后再转回List(这里就解释了为什么作为小白的我看到的流式计算都是先
集合.stream()
最后又toList()
结尾) - 串行
.stream()
和并行parallelStream()
- 函数是一等值:记住方法是如何作为函数式值来传递的
问题:
- 方法引用,一等值和二等值(不理解)😵😵😵
- 怎么理解【接口如今可以包含实现类没有提供实现的方法签名了!那谁来实现它呢?缺失的方法主体随接口提供了(因此就有了默认实现),而不是由实现类提供】这句话😵😵😵
- 怎么理解默认方法默认实现default😵😵😵
- 如何理解再没有可变共享状态时,函数或方法可以有效、安全地并行执行😵😵😵
7.14
- 需求背景:数据库里有一个字段名为跟踪(0-未跟踪,1-跟踪),现有一个需求为根据是否跟踪过来筛选列表。
预期是,不传值时为查全部,传0值为查为跟踪,1值为跟踪。
结果是,不传值时,后台拿到的是0 。
原因:因为我将实体类中的isTrack字段属性定义成了int类型,它将空值自动变为了0,改为Integer可解决。
2.25
1.在开发中,有一些值是规定了的,比如购物车的最大商品数量,比如首页轮播图数量等等,这些可以全放在一个类里。
public class Constants{
private final static int INDEX_CAROUSEL_NUMBLE = 5;
..
..
}
2.日志,在controller层,可以通过使用日志类,来打印日志
public class A{
private static final Logger logger = LoggerFactory.getLogger(A.class);
public String methodA(Long id,String name){
logger.info("logger here. id={},name={}",id,name)
}
}
3.1
Long[] ids 转换为List<Long> ids2
Arrays.asList(ids)
// Arrays.asList 和 Collections.singletonList()的区别
1.
Arrays.asList() 得到的List是可变的,根据数组大小确定。不具备add方法,但是可以用set方法进行增值,默认长度是10
而Collections.singletonList是不可变的
2.
Collections.singletionList()得到的元素只能有一个,不要尝试对其元素进行修改
3.15
一:新增的接口,注意几点
1.可以用PosttMapping
2.参数里的类的注解可以用 @RequestBody
二:服务层需要加@Transactional注解
三:intValue() //输出int数据
四:xml里。
```
<update id="xxxx" parameterType = "类名">
update xxx_xx_xx
<set>
<if test
...
...
</set>
where xxxxxx
</update>
```
```
<insert id="xxxx" parameterType = "类名">
insert into xxx_xx_xx
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test
...
...
</trim >
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test
...
...
</trim >
</insert >
```
3.25
-
实体类里的注解
@Data,lombok注解,可以省去getset方法的步骤
@ApiModelProperty:如果有用到swagger,那这个注解为Swagger接口文档所需
@NotEmtpy:参数验证的注解,意为非空
@RestController
@Api(value="v1", tage = "用户操作相关接口")
@RequestMapping("/api/v1")
public class PersonAPI{
@Resource
private UserService userService;
private static final Logger logger = LoggerFactory.getLogger(PersonAPI.class);
//1.定义成static final,logger变量不可变,读取速度快
//2.static修饰的变量是不管创建了new了多少个实例,也只能创建一次,节省空间,不然每次都创建Logger的话比较浪费内存;final修饰表示不可更改,
//3.将域定义为static,每个类中只有一个这样的域,而每一个对象对于所有的实例域却都有自己的一份拷贝,用static修饰既节省空间,效率也好。final是指本logger不能再指向其他Logger对象
@PostMapping("/user/login")
@ApiOperation(value="登陆接口",notes = "返回token")
public Result<String>login(@ResuestBody @Valid UserLoginParam userLoginParam){
...
...
...
}
}
@PostMapping("/user/login")
表示登陆请求为post,请求路径为/api/v1/user/login
首先使用@RequestBody
注解对登陆参数进行接收并封装📦成UserLoginParam对象,@Valid注解作为验证参数,定义参数时我们使用了@NotEmpty注解,表示该参数不能为空,如果这里不加@Valid注解,则非空验证则不会执行
4.13
1.分页数据封装类
用到了pageHelper工具类,分页用到了五个数据,当前页(pageNum)、页面大小(pageSize)、总页数(totalPage)、总条数(total)、数据(List< T > list)
public class CommonPage<T>{
private Integer pageNum;
private Integer pageSize;
private Integer totalPage;
private Long total;
private List<T> list;
//
get and set
//
//将pagehelper分页后的list转为分页信息
public static<T> CommonPage<T> restPage(List<T> list){
CommonPage<T> result = new CommonPage<T>();
PageInfo<T> pageinfo = new PageInfo<T>();
//封装result并返回
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setTotalPage(pageInfo.getPages());
result.setTotal(pageInfo.getTotal());
result.setList(pageInfo.getList());
return result;
}
}
4.19
报错cannot resolve symbol String
重新选一下jdk
表单列横向排列
.el-form-item {
display: inline-block !important;
}