基于SSM框架之个人相册示例代码

网友投稿 245 2023-05-28


基于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的配置文件:

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

这里最好去官网看最新配置文件的头配置http://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

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT * FROM USER WHERE username=#{username} AND pwd=#{pwd}

PictureDao.xml

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT * FROM PICTURE

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

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

spring-dao.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

因为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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/contextNHTjIhbgQ" xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">

到此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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

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

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_jb51.rar

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/contextNHTjIhbgQ" xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">

到此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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

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

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_jb51.rar


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:整理关于Bootstrap导航的慕课笔记
下一篇:Vue2组件tree实现无限级树形菜单
相关文章

 发表评论

暂时没有评论,来抢沙发吧~