唯有热爱,可抵岁月长河
contains() 方法用于判断字符串中是否包含指定的字符或字符串。
String name= "123";
Boolean flag = name.contains("2");
valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等
// 数字转字符串
String s = String.valueOf(2)
valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等
// 字符串转数字
Integer b = Integer.valueOf("444",16); // 使用 16 进制
// add() 将元素插入到指定位置的 arraylist 中
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(33);
// addAll() 添加集合中的所有元素到 arraylist 中
// Collections.addAll(); 拼接多个字符串到数组中
Collections.addAll(myNumbers , 33, 44);
// remove() 删除 arraylist 里的单个元素
myNumbers.remove(33);
// subList() 截取部分 arraylist 的元素
// clear() 删除 arraylist 中的所有元素
List<String> subList = myNumbers.subList(myNumbers.size() - 1, fileDatum.size());
subList.clear();
System.out.println(myNumbers);
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
Set<Integer> set = new HashSet<>(list);
// 方法一(会导致顺序改变)
List<T> list = ...; // 原始List
Set<T> set = new HashSet<>(list);
List<T> resultList = new ArrayList<>(set);
// 方法二
List<T> list = ...; // 原始List
List<T> resultList = list.stream().distinct().collect(Collectors.toList());
// 方法三
List<T> list = ...; // 原始List
Set<T> set = new LinkedHashSet<>(list);
List<T> resultList = new ArrayList<>(set);
Set<String> set = new HashSet<>();
List<String> list = new ArrayList<>(set);
// addAll()
Set<String> set = new HashSet<>();
List<String> list = new ArrayList<>();
list.addAll(set);
@Override
public void createTree() {
List<Tree> tree = getTree();
}
/**
* 获取树数据
* @return
*/
public List<Tree> getTree() {
//获取所有符合条件数据
List<Tree> all = treeDao.getAll();
//获取所有根节点
List<Tree> rootNode = treeDao.getRootNode();
//通过根节点一层一层的找到所有子节点
for (Tree tree : rootNode) {
getTreeChildren(all, tree);
}
return rootNode;
}
/**
* 处理树数据,递归
*
* @param listA
* @return
*/
public void getTreeChildren(List<Tree> listA, Tree tree) {
// 每次执行删除父节点,提高效率,防止死循环
listA.remove(tree);
// 获取每一个父节点的下级节点List
for (Tree tree2 : listA) {
if (tree.getId().equals(tree2.getParentId())) {
tree.getChildren().add(tree2);
}
}
// 递归获取树数据
if (tree.getChildren() != null && tree.getChildren().size() != 0) {
for (Tree tree3 : tree.getChildren()) {
getTreeChildren(listA, tree3);
}
}
}
<!--批量插入-->
<insert id="saveForEach" parameterType="java.util.List">
INSERT INTO table_name (
id,
name,
ref_id)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id},
#{item.name},
#{item.refId},
)
</foreach>
</insert>
<!--批量删除-->
<delete id="deleteForEach">
delete from
table_name
where
ref_id
in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item.refId}
</foreach>
</delete>
<select id="select" resultType="xx.vo.XxVO">
select
*
from
table_name
</select>
用于过滤记录。
<select id="select" resultType="xx.vo.XxVO">
select
*
from
table_name
where
1 = 1
<if test="name != null and name != '' ">
and name = #{name}
</if>
<if test="refId != null and refId != '' ">
and ref_id = #{refId}
</if>
-- 日期过滤
<if test='timeFrom != null and !"".equals(timeFrom)'>
and upd_dtm <![CDATA[>=]]> #{timeFrom}
</if>
<if test='timeTo != null and !"".equals(timeTo)'>
and upd_dtm <![CDATA[<=]]> #{timeTo}
</if>
</select>
关键字用于对结果集进行排序。
<select id="select">
SELECT
*
FROM
table_name
order by
creat_time
-- string 转 int
-- cast(policy_version as UNSIGNED INTEGER) desc
</select>
GROUP BY 语句可结合一些聚合函数来使用
<select id="select" resultType="xx.vo.XxVO">
select
*
from
table_name
GROUP BY
version
</select>
BeanUtils是一个Java类库,用于简化Java Bean对象之间的属性拷贝和赋值操作
BeanUtils.copyProperties(destination, source);
BeanUtils还提供了一些其他的方法,例如getProperty()、setProperty()、populate()等等,可以用于获取属性值、设置属性值、批量设置属性值等操作。
String value = BeanUtils.getProperty(bean, "propertyName");
BeanUtils.setProperty(bean, "propertyName", "value");
/**
* xx实体类
*/
@TableName("demo_table")
public class DemoTableEntity implements Serializable {
/**
* ID
*/
@TableId
private String demoId;
/**
* 名称
*/
private String demoName;
public String getDemoId() {
return demoId;
}
public void setDemoId(String demoId) {
this.demoId= demoId;
}
public String getDemoName() {
return demoName;
}
public void setDemoName(String demoName) {
this.demoName= demoName;
}
}
@RestController
@RequestMapping("/demoTable")
public class DemoTableController extends BaseController {
@Autowired
private DemoTableService demoTableService;
/**
* 新增
* @param demoTableEntity
* @return
*/
@PostMapping("/save")
public AjaxResult save(@RequestBody DemoTableEntity demoTableEntity){
return toAjax(demoTableService.save(demoTableEntity));
}
/**
* 修改
* @param demoTableEntity
* @return
*/
@PostMapping("/update")
public AjaxResult update(@RequestBody DemoTableEntity demoTableEntity){
return toAjax(demoTableService.update(demoTableEntity));
}
/**
* 查询列表
* @param demoTableEntity
* @return
*/
@GetMapping("/selectList")
public TableDataInfo selectList(DemoTableEntity demoTableEntity){
startPage();
List<DemoTableEntity> list = demoTableService.selectList(demoTableEntity);
return getDataTable(list);
}
/**
* 删除
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
public AjaxResult deleteById(@PathVariable String id){
return toAjax(demoTableService.deleteById(id));
}
}
public interface DemoTableService {
/**
* 新增服务实例
* @param demoTableEntity
* @return
*/
Integer save(DemoTableEntity demoTableEntity);
/**
* 修改服务实例
* @param demoTableEntity
* @return
*/
Integer update(DemoTableEntity demoTableEntity);
/**
* 查询服务实例列表
* @param demoTableEntity
* @return
*/
List<DemoTableEntity> selectList(DemoTableEntity demoTableEntity);
/**
* 根据id删除服务实例
* @param id
* @return
*/
Integer deleteById(String id);
}
@Service
public class DemoTableServiceImpl implements DemoTableService {
@Autowired
private DemoTableMapper demoTableMapper;
/**
* 新增服务实例
* @param demoTableEntity
* @return
*/
@Override
public Integer save(DemoTableEntity demoTableEntity){
return demoTableMapper.insert(demoTableEntity);
}
/**
* 修改
* @param demoTableEntity
* @return
*/
@Override
public Integer update(DemoTableEntity demoTableEntity){
UpdateWrapper<DemoTableEntity> wrapper = new UpdateWrapper<>();
return demoTableMapper.update(demoTableEntity, wrapper);
}
/**
* 查询列表
* @param demoTableEntity
* @return
*/
@Override
public List<DemoTableEntity> selectList(DemoTableEntity demoTableEntity){
QueryWrapper queryWrapper = new QueryWrapper();
return demoTableMapper.selectList(queryWrapper);
}
/**
* 根据id删除
* @param id
* @return
*/
@Override
public Integer deleteById(String id){
return demoTableMapper.deleteById(id);
}
}
public interface DemoTableMapper extends BaseMapper<DemoTableEntity> {
}
bug场景: 访问 Controller 的接口返回404
// 解决方案: 启动类添加 @MapperScan注解
@MapperScan("com.gientech.armp.empower.mapper.*")
@MapperScan
注解指定了 Mapper 接口所在的包路径为 com.gientech.armp.empower.mapper
。这样,在应用启动时,Spring Boot 就会自动扫描该包下的所有 Mapper 接口,并将其实例化后交给 Spring 容器管理。