#要层次清晰,bean、dao、service、controller、mybatis,按照这样分层来做商品的新增(有商品id,商品名称,商品图片),我是用myeclipse开发工具,你们可以用form表单提交,也可以用ajax提交
9条回答 默认 最新
- Always_MyLoverX 2018-01-09 01:37关注
系统环境搭建就不说了,大致说下代码吧,前提你那个商城系统运行的起来
mysql建张表 t_goods_pic
字段:
id varchar(100) 主键
name varchar(100)
pic longblob --这里存头像图片文件的二进制流,使用longblob类型
对应字段初始化一条记录:good1,我是商品1,nullmodel:com.xProgram.manage.model.TGoodsPic
package com.xProgram.manage.model; public class TGoodsPic { private String id; private String name; private byte[] pic; public String getId() { return id; } public void setId(String id) { this.id = id == null ? null : id.trim(); } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public byte[] getPic() { return pic; } public void setPic(byte[] pic) { this.pic = pic; } }
mapper:com.xProgram.manage.mapper.TGoodsPicMapper
主要用到最后两个接口,这里可以称为dao层,只不过用的mybatis实现package com.xProgram.manage.mapper; import org.apache.ibatis.annotations.Param; import com.xProgram.manage.model.TGoodsPic; public interface TGoodsPicMapper { int insert(TGoodsPic record); int insertSelective(TGoodsPic record); int updateGoods(TGoodsPic record); TGoodsPic selectAllGoodsById(@Param(value="id")String id); }
mapping:com.xProgram.manage.mapping.TGoodsPicMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xProgram.manage.mapper.TGoodsPicMapper"> <resultMap id="BaseResultMap" type="com.xProgram.manage.model.TGoodsPic"> <result column="ID" jdbcType="VARCHAR" property="id" /> <result column="NAME" jdbcType="VARCHAR" property="name" /> </resultMap> <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.xProgram.manage.model.TGoodsPic"> <result column="PIC" jdbcType="LONGVARBINARY" property="pic" /> </resultMap> <sql id="Blob_Column_List"> PIC </sql> <insert id="insert" parameterType="com.xProgram.manage.model.TGoodsPic"> insert into t_goods_pic (ID, NAME, PIC ) values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{pic,jdbcType=LONGVARBINARY} ) </insert> <insert id="insertSelective" parameterType="com.xProgram.manage.model.TGoodsPic"> insert into t_goods_pic <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> ID, </if> <if test="name != null"> NAME, </if> <if test="pic != null"> PIC, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=VARCHAR}, </if> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="pic != null"> #{pic,jdbcType=LONGVARBINARY}, </if> </trim> </insert> <update id="updateGoods" parameterType="com.xProgram.manage.model.TGoodsPic"> update t_goods_pic <set> <if test="name != null"> name=#{name,jdbcType=VARCHAR}, </if> <if test="pic != null"> pic=#{pic,jdbcType=LONGVARBINARY}, </if> </set> where id = #{id,jdbcType=VARCHAR} </update> <select id="selectAllGoodsById" resultMap="BaseResultMap" useCache="false"> select id,name,pic from t_goods_pic <where> id=#{id,jdbcType=VARCHAR} </where> </select> </mapper>
service:com.xProgram.manage.service.TGoodsPicService
服务层接口package com.xProgram.manage.service; import org.apache.ibatis.annotations.Param; import com.xProgram.manage.model.TGoodsPic; public interface TGoodsPicService { int insert(TGoodsPic record); int insertSelective(TGoodsPic record); int updateGoods(TGoodsPic record); TGoodsPic selectAllGoodsById(@Param(value="id")String id); }
serviceImpl:com.xProgram.manage.serviceImpl.TGoodsPicServiceImpl
注意开始有注解了@Service("tgoodspicService"),dao层与service层建立关系package com.xProgram.manage.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xProgram.manage.mapper.TGoodsPicMapper; import com.xProgram.manage.model.TGoodsPic; import com.xProgram.manage.service.TGoodsPicService; @Service("tgoodspicService") public class TGoodsPicServiceImpl implements TGoodsPicService{ private TGoodsPicMapper tgoodspicMapper; public TGoodsPicMapper getTgoodspicMapper() { return tgoodspicMapper; } @Autowired public void setTgoodspicMapper(TGoodsPicMapper tgoodspicMapper) { this.tgoodspicMapper = tgoodspicMapper; } @Override public int insert(TGoodsPic record) { return 0; } @Override public int insertSelective(TGoodsPic record) { return 0; } @Override public int updateGoods(TGoodsPic record) { return tgoodspicMapper.updateGoods(record); } @Override public TGoodsPic selectAllGoodsById(String id) { return tgoodspicMapper.selectAllGoodsById(id); } }
controller:com.xProgram.manage.controller.TGoodsPicController
控制层,全靠注解
@Controller
@RequestMapping("/tgoodspicService") 注解在类上
@RequestMapping(value="/uploadPic") 注解在方法上
访问指定方法:/tgoodspicService/uploadPic.do?param=xxxpackage com.xProgram.manage.controller; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.xProgram.manage.model.TGoodsPic; import com.xProgram.manage.service.TGoodsPicService; @Controller @RequestMapping("/tgoodspicService") public class TGoodsPicController { private TGoodsPicService tgoodspicService; public TGoodsPicService getTgoodspicService() { return tgoodspicService; } @Autowired public void setTgoodspicService(TGoodsPicService tgoodspicService) { this.tgoodspicService = tgoodspicService; } /** * 上传头像 * @param request * @return */ @RequestMapping(value="/uploadPic") public void uploadPic(HttpServletRequest request,HttpServletResponse response) { String filePath = request.getParameter("filepath");//前端传递过来的上传路劲 String goodid = request.getParameter("goodid");//前端传递过来的id TGoodsPic good = new TGoodsPic(); good.setId(goodid); good.setName("我是商品1"); byte []pic=null; try{ //通过下面代码把文件转成byte直接存储就行 File file=new File(filePath); InputStream inputStream=new FileInputStream(file); pic=new byte[inputStream.available()]; inputStream.read(pic); inputStream.close(); good.setPic(pic); tgoodspicService.updateGoods(good);//头像存库 response.getWriter().write("success"); response.getWriter().flush(); }catch(Exception e){ e.printStackTrace(); } } /** * 获取头像 * @param request * @return */ @RequestMapping(value="/getGoodsPic") public void getGoodsPic(HttpServletRequest request,HttpServletResponse response){ response.setHeader("Content-Type","image/jped");//设置响应的媒体类型,这样浏览器会识别出响应的是图片 String goodid = request.getParameter("goodid"); TGoodsPic good = tgoodspicService.selectAllGoodsById(goodid); try { response.getOutputStream().write(good.getPic());//流输出 response.getOutputStream().flush(); } catch (IOException e) { e.printStackTrace(); } } }
前端:login.html
<div class="modal-footer"> <div id="message1" class="pull-left message"> <input name="file1" type="file" value="选择" size="50" style="line-height: 18px;" height="18px" id="fileUpload1"> </div> <button type="button" class="btn btn-primary" onclick="uploadpic();"> 上传头像 </button> </div>
js操作
//上传头像 function uploadpic(){ var filepath = $("#fileUpload1").val(); //商品id,数据库写死记录 var goodid = "good1"; if(filepath == ""|| filepath==null){ return; } jQuery.ajax({ type: 'post', url: "tgoodspicService/uploadPic.do", async: false, cache: false, contentType:'application/x-www-form-urlencoded; charset=UTF-8', data: {"goodid":goodid,"filepath":filepath}, success: function (data) { //头像存库后再读取出来响应到img标签 showPic(goodid); } }); } //展示头像 function showPic(goodid){ //img标签src属性支持流输出,后面要给上随机参数,预防读缓存 $(".img_").attr("src","tgoodspicService/getGoodsPic.do?goodid="+goodid+"&mas="+Math.random()); }
web.xml :对/tgoodspicService开放拦截,不登录也能演示
<filter> <filter-name>SessionFilter</filter-name> <filter-class>com.xProgram.manage.filter.IsLoginFilter</filter-class> <init-param> <param-name>loginStrings</param-name><!-- 对登录页面不进行过滤 --> <param-value>/login.html;/seller;/register;version.html;/test;/inswept;/tgoodspicService</param-value> </init-param> <init-param> <param-name>includeStrings</param-name><!-- 只对指定过滤参数后缀进行过滤 --> <param-value>.html;.jsp;.do</param-value> </init-param> <init-param> <param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 --> <param-value>/login.html</param-value> </init-param> </filter>
效果:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报