关于ssm框架的项目总结一

  • debug 模式:看变量内部的值:shift+Alt +i
  • shift+alt+H 全工程替换文件名
  • 利用ajax实现异步上传图片并回显
  • 实现ajiax实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.itcast.common.web;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

/**
* 异步返回各种格式
* json
* xml
* text
* @author lx
*
*/
public class ResponseUtils {

//发送内容
public static void render(HttpServletResponse response,String contentType,String text){
response.setContentType(contentType);
try {
response.getWriter().write(text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//发送的是JSON
public static void renderJson(HttpServletResponse response,String text){
render(response, "application/json;charset=UTF-8", text);
}
//发送xml
public static void renderXml(HttpServletResponse response,String text){
render(response, "text/xml;charset=UTF-8", text);
}
//发送text
public static void renderText(HttpServletResponse response,String text){
render(response, "text/plain;charset=UTF-8", text);
}


}
  • 上传图片:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//上传图片
@RequestMapping(value = "/upload/uploadPic.do")
public void uploadPic(@RequestParam(required = false) MultipartFile pic,HttpServletResponse response){
//扩展名
String ext = FilenameUtils.getExtension(pic.getOriginalFilename());

//图片名称生成策略
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//图片名称一部分
String format = df.format(new Date());

//随机三位数
Random r = new Random();
// n 1000 0-999 99
for(int i=0 ; i<3 ;i++){
format += r.nextInt(10);
}

//实例化一个Jersey
Client client = new Client();
//保存数据库
String path = "upload/" + format + "." + ext;

//另一台服务器的请求路径是?
String url = Constants.IMAGE_URL + path;
//设置请求路径
WebResource resource = client.resource(url);

//发送开始 POST GET PUT
try {
resource.put(String.class, pic.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//返回二个路径
JSONObject jo = new JSONObject();
jo.put("url", url);
jo.put("path",path);

ResponseUtils.renderJson(response, jo.toString());
}
  • 页面的js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script type="text/javascript">
//上传图片
function uploadPic(){
//定义参数
var options = {
url : "/upload/uploadPic.do",
dataType : "json",
type : "post",
success : function(data){
//回调 二个路径
//url
//path
$("#allImgUrl").attr("src",data.url);
$("#path").val(data.path);
}
};

//jquery.form使用方式
$("#jvForm").ajaxSubmit(options);

}

</script>
  • 批量删除
1
2
3
4
5
6
7
8
9
10
<!-- 批量删除 -->
<delete id="deleteBrandByKeys" parameterType="Integer">
delete from bbs_brand
<where>
id in
<foreach collection="array" item="id" open="(" close=")" separator="," >
#{id}
</foreach>
</where>
</delete>
  • 实现批量删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//页面
<div style="margin-top:15px;"><input class="del-button" type="button" value="删除" οnclick="optDelete('${name}','${isDisplay }');"/></div>
//删除批量
function optDelete(name,isDisplay){
//请选择一个
var s = $("input[name='ids']:checked").size();
if(s <= 0){
alert("请至少选择一个!");
return;
}
if(!confirm("你确定删除吗?")){
return ;
}
//删除
$("#jvForm").attr("action","/brand/deletes.do?name=" + name +"&isDisplay=" + isDisplay);
$("#jvForm").attr("method","post").submit();

}
  • 关于mybatis的优化总结:
    • 主要是在实体类中设置一个字段来接受其他字段,这样就可以控制字段的查询,而在xml中我们使用${实体类.字段名}就可以取得相关的字段值,进行字段的查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cn.itcast.core.query.product;

import java.util.ArrayList;
import java.util.List;

/**
* 品牌 查询专用对象
* @author lx
*
*/
public class BrandQuery {


private Integer id;
private String name;
private String description;
private String imgUrl;
private Integer sort;
private Integer isDisplay;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public Integer getIsDisplay() {
return isDisplay;
}
public void setIsDisplay(Integer isDisplay) {
this.isDisplay = isDisplay;
}
/***********查询字段指定*************************************/
private String fields;
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
}
/***********查询字段Like*************************************/
private boolean nameLike;
public boolean isNameLike() {
return nameLike;
}
public void setNameLike(boolean nameLike) {
this.nameLike = nameLike;
}

/***********order by *************************************/
public class FieldOrder{
private String field; //id , name imgUrl
private String order; // desc asc

public FieldOrder(String field, String order) {
super();
this.field = field;
this.order = order;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}

}
//orderby 集合
private List<FieldOrder> fieldOrders = new ArrayList<FieldOrder>();

//按照Id排序
public void orderbyId(boolean isAsc){
fieldOrders.add(new FieldOrder("id",isAsc == true ? "asc" : "desc"));
}


//页号
private Integer pageNo = 1;//int
//开始行
private Integer startRow;//null
//每页数
private Integer pageSize = 10;


public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
this.startRow = startRow;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
//计算一次开始行
this.startRow = (pageNo - 1)*pageSize;
this.pageSize = pageSize;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
//计算一次开始行
this.startRow = (pageNo - 1)*pageSize;
this.pageNo = pageNo;
}


}
  • mybatis 的优化设置主要用了${}进行取值:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!-- resultMap  -->
<resultMap type="Brand" id="brand">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
<result column="img_url" property="imgUrl"/>
<result column="sort" property="sort"/>
<result column="is_display" property="isDisplay"/>
</resultMap>

<!-- brandSelector fields id,name id #{id} == "id" ${id} == id -->
<sql id="brandSelector">
select
<if test="fields != null">
${fields}
</if>
<if test="fields == null">
id , name ,description,img_url,sort,is_display
</if>
from bbs_brand
</sql>
<!-- 品牌条件 -->
<sql id="brandWhere">
<where>
<if test="isDisplay != null">
is_display = #{isDisplay}
</if>
<if test="name != null">
<if test="nameLike == false">
and name = #{name}
</if>
<if test="nameLike == true">
and name like "%"#{name}"%"
</if>
</if>
</where>
</sql>

<!-- 品牌 Order by -->
<sql id="brandOrderBy">
<if test="fieldOrders != null and fieldOrders.size > 0">
order by
<foreach collection="fieldOrders" item="fieldOrder" separator=",">
${fieldOrder.field} ${fieldOrder.order}
</foreach>
</if>
</sql>

<!-- limit -->
<sql id="brandLimit">
<if test="startRow != null">
limit #{startRow},#{pageSize}
</if>
</sql>

<!-- 查询品牌集合 -->
<select id="getBrandList" parameterType="BrandQuery" resultMap="brand">
<include refid="brandSelector"/>
<include refid="brandWhere"/>
<include refid="brandOrderBy"/>
<include refid="brandLimit"/>
</select>
  • maven项目的不会 自带前斜杠/,而动态的web项目是自动加前斜杠的所以资源引用的时候要注意这一点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值