基于SSM框架的个人相册样例代码

来源:爱站网时间:2020-10-20编辑:网友分享
相信很多新手程序员在实现某个程序功能的时候,都要经过不断的学习和摸索,本文是爱站技术频道小编为大家整理的基于SSM框架的个人相册样例代码,感兴趣的小伙伴们可以参考本文的介绍。

相信很多新手程序员在实现某个程序功能的时候,都要经过不断的学习和摸索,本文是爱站技术频道小编为大家整理的基于SSM框架的个人相册样例代码,感兴趣的小伙伴们可以参考本文的介绍。

项目的演示效果:

开发的工具及环境:

  1. IntelliJ IDEA: 2016
  2. Maven :3.0x
  3. Hbuilder(前端部分,可以用记事本代替2333)
  4. Java 8

项目流程(dao->service->web):

1.添加所有依赖:

 junitjunit4.11testorg.slf4jslf4j-api1.7.12ch.qos.logbacklogback-core1.1.1ch.qos.logbacklogback-classic1.1.1mysqlmysql-connector-java5.1.35runtimec3p0c3p00.9.1.1org.mybatismybatis3.3.0org.mybatismybatis-spring1.2.3taglibsstandard1.1.2jstljstl1.2com.fasterxml.jackson.corejackson-databind2.5.4javax.servletjavax.servlet-api3.1.0org.springframeworkspring-core4.1.7.RELEASEorg.springframeworkspring-beans4.1.7.RELEASEorg.springframeworkspring-context4.1.7.RELEASEorg.springframeworkspring-jdbc4.1.7.RELEASEorg.springframeworkspring-tx4.1.7.RELEASEorg.springframeworkspring-web4.1.7.RELEASEorg.springframeworkspring-webmvc4.1.7.RELEASEorg.springframeworkspring-test4.1.7.RELEASEredis.clientsjedis2.7.3com.dyuproject.protostuffprotostuff-core1.0.8com.dyuproject.protostuffprotostuff-runtime1.0.8commons-fileuploadcommons-fileupload1.3.1

2.添加Mybatis的配置文件:

这里最好去官网看最新配置文件的头配置http://www.mybatis.org/mybatis-3/zh/index.html

然后编写dao层的代码:

相册实体类

public interface PictureDao {
  /**
   * @return 返回所有图片
   */
  List getAllPictures();

  /**上传图片,并且将图片名,图片描述信息插入数据库
   * @param picName
   * @param content
   * @return插入成功返回1,失败0
   */
  int InsertPicture(@Param("picName") String picName, @Param("content") String content);
}

用户实体类

public interface UserDao {
  /**如果查询到该用户就会返回1
   * @param username,pwd
   * @return数据库被修改的行数
   */
  User getUserByName(@Param("username") String username, @Param("pwd") String pwd);
}

实体类创建好,我们就在resource文件夹下创建一个mapper文件夹,放我们dao层的映射文件。

UserDao.xml

PictureDao.xml


    INSERT INTO `picture` (`picname`,`content`) VALUES (#{picName},#{content})
  

最后整合到Spring里面。所以我再次在resource文件夹下创建一个spring文件夹,并且创建一个文件名为:

spring-dao.xml

因为spring-dao.xml里面有些属性要连接到我们的数据库,所以我们把我们的数据库的连接驱动,用户名什么鬼都写在一个叫

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/picture?useUnicode=true&characterEncoding=utf-8
jdbc.username=Elric
jdbc.password=881010

dao层编写结束(表示写blog比敲代码还累23333)!

3.编写Service层

因为这是个小Demo(博主刚学不久,还是一只小菜鸡)。所以Service的实现大抵跟dao差不多。

先写两个Service接口:

UserService

public interface UserService {
  /**本次中我们只需要对用户身份做出判断然后给予url
   * @return 数据库查询到为1
   */
  User CheckUser(String username, String pwd);
}

PictureService

public interface PictureService {
  /**查询所有照片
   * @return 所有照片
   */
  List getAllPicture();

  /**
   * 这个服务就是PictureDao中的InsertP
   * @param picName
   * @param content
   * @return 数据库成功返回1,失败返回0
   */
  int InsertPicture(String picName, String content);
}

然后再写两个实现Service接口的实现类:PictureServiceImpl

@Service
public class PictureServiceImpl implements PictureService {
  @Autowired
  private PictureDao pictureDao;

  public List getAllPicture() {
    return pictureDao.getAllPictures();
  }
  public int InsertPicture(String picName, String content) {
    return pictureDao.InsertPicture(picName,content);
  }
}

UserServiceImpl

PictureServiceImpl

@Service
public class UserServiceImpl implements com.koali.service.UserService {
  @Autowired
  private UserDao userDao;
  public User CheckUser(String username, String pwd) {
    return userDao.getUserByName(username,pwd);
  }
}

然后写配置文件:

在resource中的spring文件夹下创建spring-service.xml

spring-service.xml

到此Service层就写好了,这个比较简单。

3.web层的编写:

现在web.xml添加spring-mvc的前端控制器:

  seckill-dispatcherorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/spring-*.xmlseckill-dispatcher/characterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8forceEncodingtruecharacterEncodingFilter/*

然后在resourced的spring文件夹创建spring-web.xml

spring-web.xml

最后编写我们的前端控制器:

MainController

@Controller
public class MainController {
  @Autowired
  private PictureService pictureService;
  @Autowired
  private UserService userService;
  @RequestMapping(value = "/")
  public String index(Model model){
    List pictures =pictureService.getAllPicture();
    System.out.println(pictures.size());
    model.addAttribute("pictures",pictures);
    return "index";
  }
  @RequestMapping(value = "login")
  public String login(){
    return "login";
  }
  @RequestMapping(value = "checkandRedict")
  public String checkAndRedict(@Param("username") String username,@Param("pwd") String pwd){
    User user = userService.CheckUser(username,pwd);
    System.out.println(user);
    if (user!=null){
      return "upload";
    }else {
      return "index";
    }
  }
  @RequestMapping(value = "upload",method = RequestMethod.POST)
  public String upload(@RequestParam("file") MultipartFile file,@Param("content") String content, HttpServletRequest request,Model model) throws IOException{
    //获取项目的根路径,将上传图片的路径与我们的资源路径在一起,才能显示
    HttpSession session= request.getSession();
    String path = session.getServletContext().getRealPath("/");
    System.out.println("getRealPath('/'):"+path);
    int end = path.indexOf("t",19);
    String prePath = path.substring(0,end);
    String realPath = prePath+"target\\demo\\WEB-INF\\jsp\\images";
    System.out.println("DEBUG:"+realPath);
    String picName = new Date().getTime()+".jpg";
    if (!file.isEmpty()){
      FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath,new Date().getTime()+".jpg"));
    }else if(content==null){
      content = "";//如果输入为null数据库不允许插入
    }
    //图片类的名字保存为路径+名字方便后期前端提取
    //将图片名字用时间戳保存,反正上传图片为中文乱码等问题
    int code = pictureService.InsertPicture("images/"+picName,content);
    if (code==1) {
      List pictures = pictureService.getAllPicture();
      model.addAttribute("pictures", pictures);
      return "index";
    }else
      return "index";
  }
}

至此项目就到此为止!

听爱站技术频道小编介绍完基于SSM框架的个人相册样例代码,相信大家都有所收获,如果遇到有任何疑问,这里都将为你答疑解惑。

上一篇:简述Java开发中StringBuilder字符串类型的操作方法

下一篇:论Timer和TimerTask与线程的微妙关系

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载