基于SSM框架之个人相册

学习了一阵子的SSM框架,一直在各种博客,,慕课网学习,最后终于自己撸出来一个简单的个人相册。


项目的演示效果:

基于SSM框架之个人相册_第1张图片
个人相册.gif

开发的工具及环境:
IntelliJ IDEA: 2016
Maven :3.0x
Hbuilder(前端部分,可以用记事本代替2333)
Java 8


项目流程(dao->service->web):
1.添加所有依赖:


 
            junit
            junit
            4.11
            test
        
        
        
        
            org.slf4j
            slf4j-api
            1.7.12
        
        
            ch.qos.logback
            logback-core
            1.1.1
        
        
        
            ch.qos.logback
            logback-classic
            1.1.1
        
        
        
            mysql
            mysql-connector-java
            5.1.35
            runtime
        
        
            c3p0
            c3p0
            0.9.1.1
        
        
        
            org.mybatis
            mybatis
            3.3.0
        
        
        
            org.mybatis
            mybatis-spring
            1.2.3
        
        
        
            taglibs
            standard
            1.1.2
        
        
            jstl
            jstl
            1.2
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.5.4
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
        
        
            org.springframework
            spring-core
            4.1.7.RELEASE
        
        
            org.springframework
            spring-beans
            4.1.7.RELEASE
        
        
            org.springframework
            spring-context
            4.1.7.RELEASE
        
        
        
            org.springframework
            spring-jdbc
            4.1.7.RELEASE
        
        
            org.springframework
            spring-tx
            4.1.7.RELEASE
        
        
        
            org.springframework
            spring-web
            4.1.7.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.1.7.RELEASE
        
        
        
            org.springframework
            spring-test
            4.1.7.RELEASE
        
        
        
            redis.clients
            jedis
            2.7.3
        
        
        
            com.dyuproject.protostuff
            protostuff-core
            1.0.8
        
        
            com.dyuproject.protostuff
            protostuff-runtime
            1.0.8
        
        
            commons-fileupload
            commons-fileupload
            1.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-dispatcher
   org.springframework.web.servlet.DispatcherServlet
   
       contextConfigLocation
       classpath:spring/spring-*.xml
   
   
   
       seckill-dispatcher
       
       /
   

   
       characterEncodingFilter
       org.springframework.web.filter.CharacterEncodingFilter
       
           encoding
           UTF-8
       
       
           forceEncoding
           true
       
   
   
       characterEncodingFilter
       /*
   

然后在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";
   }
}

至此项目就到此为止!


期间遇到好多好多坑,比如mybatis无端端就连不上,或者莫名其妙扫描不到我的Spring配置文件,可是我在我IDEA明明ctrl点击找的到,反正很多很多,有些解决了,有些还是不懂,不过以后慢慢接触应该就会越来越熟悉!
后面的计划应该会给项目加Shiro用户提权,虽然我的也是要登陆才能上传图片,所以我不太清楚Shiro对我好像用处不大,不过还是学一学,加进去。然后再学点高并发并且加进去,虽然如果我搭建了我这个项目到我的服务器,也不可能有很多人同时访问这个网站,但是高并发这种东西还是要学学的。
最后献上我的项目的地址:https://github.com/Elricyo/SSM

你可能感兴趣的:(基于SSM框架之个人相册)