关于电商项目的一些心得
先附上我自己写的后端接口链接,感兴趣的可以看一看,写的不是很好,可以联系我互相讨论讨论。
https://gitee.com/yangguojun1/ssm
1、持久层,sql语句的实现
- 模糊查询
"select * from good where name like CONCAT('%',#{name},'%');"
用到了concat(str1,str2,....)
函数,返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。
- 更新商品
"<script>" +
" update good " +
"<set>" +
"<if test=\"name != null and name!=''\">" +
"name=#{name}," +
"</if>" +
"<if test=\"price != null and price!=''\">" +
"price=#{price}," +
"</if>" +
"<if test=\"weight != null and weight!=''\">" +
"weight=#{weight}," +
"</if>" +
"<if test=\"number != null and number!=''\">" +
"number=#{number}," +
"</if>" +
"<if test=\"status != null and status!=''\">" +
"status=#{status}," +
"</if>" +
"<if test=\"time != null and time!=''\">" +
"time=#{time}," +
"</if>" +
"<if test=\"url != null and url!=''\">" +
"url=#{url}" +
"</if>" +
"</set>" +
"where id=#{id}" +
" </script>"
运用动态sql。<set> ....</set>
使用set标签可以动态的配置SET 关键字和剔除追加到条件末尾的任何不相关的逗号。有效的解决了使用<if>....</if>
if标签时,如果有一个参数为null,都会导致错误,或者前面的if没有执行,则可能导致有多余逗号的错误。
- 进行批量的操作(删除,修改)
"<script>" +
" delete from good " +
"where id in" +
" <foreach collection='list' item='id' open='(' separator=',' close=')'>" +
"#{id}" +
"</foreach>" +
"</script>"
"<script>" +
" update good " +
" set status=1 " +
"where id in" +
" <foreach collection='list' item='id' open='(' separator=',' close=')'>" +
"#{id}" +
"</foreach>" +
"</script>"
运用动态sql。<foreach>....</foreach>
使用foreach标签做循环遍历。foreach元素的属性主要有 item、index、 collection、open、separator、close。
- collection 表示要做foreach循环的对象
- item 表示集合中每一个元素或者该集合的对象,支持对象点属性的方式获取属性。
- open 表示以什么开始
- close表示以什么结束
- separator 表示每次进行迭代之间以什么符号作为分隔符
- index 表示循环的下标,从0开始
当然也要注意where
条件判断是in....
而不是=
关于数据库相关的知识补充一点:有时候当给表的某一个属性赋予中文数据时报错,这是因为你的数据库编码集的问题。
解决方法:
- 首先检查数据库编码是否是
utf-8
,如果不是设置数据库字符集编码为utf-8
- 修改表字段编码方式为
utf-8
,SQL语句如下:alter table 'tablename' convert to character set utf-8
修改一张表的所有字段的编码格式
2. 业务层
- 主要是代码逻辑,值得注意的是,给
JavaBean
对象赋值时(调用set
方法时),有可能会出现空指针异常。
解决方法
创建一个`JavaBean`对象,也就是`new`一个出来。
3. 表现层
需要了解一下注解的使用:
//value指的是访问路径,produces表示将生产json格式的数据,此时根据请求头中的Accept进行匹配,如请求头“Accept:application/json”时即可匹配;
@RequestMapping(value = "/findAll", produces = "application/json;charset=utf-8")
@RequestMapping(value = "/findById/{id}", produces = "application/json;charset=utf-8")
//@ResponseBody的作用其实是将java对象转为json格式的数据或者是XML数据。
@ResponseBody
//@PathVariable注解表示将从请求路径上获得数据 == ‘{id}’
public Good findById(@PathVariable("id") String id){
Good good = goodService.findById(id);
return good;
}
//@CrossOrigin注解是在前后端分离开发时,解决跨域问题的
@CrossOrigin
处理json数据
需要用到jar包:
如果没有用maven
配置,则需要jackson-databind,jackson-core,jackson-annotations
三个jar包。
如果用到了maven
配置,则只需要导入jackson-databind
依赖就行
- 将json数据处理为JavaBean对象
ObjectMapper mapper = new ObjectMapper();
Good good = mapper.readValue(json, Good.class);
- 将JavaBean对象处理为json数据
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(good);
- 同理将list、map集合 处理为json数据
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(list);
String json = mapper.writeValueAsString(map);
其余一些处理数据方法
- 将一个
String
类型的字符串(有符号为分隔符的数字)转为List<String>
集合
List<String> list = Arrays.asList(str.split(","));
- 将一个
List<T>
集合转为字符串
,并以逗号为分隔符。
String join = StringUtils.join(list,",");
- 将一个字符串转为
String[]
数组。
String shoppings = myorder.getShoppings();
String[] split = shoppings.split(",");