Flask接口签名sign原理与实例代码浅析
274
2023-05-28
基于SSM框架之个人相册示例代码
学习了一阵子的SSM框架,一直在各种博客,简书,慕课网学习,最后终于自己撸出来一个简单的个人相册。
项目的演示效果:
开发的工具及环境:
IntelliJ IDEA: 2016
Maven :3.0x
Hbuilder(前端部分,可以用记事本代替2333)
java 8
项目流程(dao->service->web):
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
/**上传图片,并且将图片名,图片描述信息插入数据库
* @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
/**
* 这个服务就是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
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的前端控制器: 然后在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 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 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的前端控制器:
然后在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
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
model.addAttribute("pictures", pictures);
return "index";
}else
return "index";
}
}
至此项目就到此为止!
最后献上我的项目的地址:SSM_jb51.rar
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~