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

开发的工具及环境:
- 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";
}
}
至此项目就到此为止!
听爱站技术频道小编介绍完基于SSM框架的个人相册样例代码,相信大家都有所收获,如果遇到有任何疑问,这里都将为你答疑解惑。
