Flask接口签名sign原理与实例代码浅析
243
2023-06-04
Maven+SSM框架实现简单的增删改查
Spring介绍:
spring 使用基本的 javaBean 来完成以前只可能由 EJB 完成的事情。然而, Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java 应用都可以从 Spring 中受益。 简单来说, Spring 是一个轻量级的控制反转(IoC )和面向切面( AOP )的容器框架。
SpringMVC介绍
Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在Spring Web Flow 里面。 Spring MVC 分离了 控制器 、模型 对象 、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
MyBatis介绍
MyBatis 本是 apache 的一个开源项目 iBatis , 2010 年这个项目由apache software foundation 迁移到了 google code ,并且改名为 MyBatis 。MyBatis 是一个基于 Java 的 持久层 框架。 iBATIS 提供的 持久层 框架包括SQL Maps 和 Data Access Objects ( DAO ) MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。 MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs ( Plain Old Java Objects ,普通的 Java 对象)映射成数据库中的记录。
首先不是maven架构整合的 SSM 框架
创建数据库、表db_zsl t_user user_t【最下面有sql语句】
1.项目整体结构
2.添加相关jar 包
3.项目整体结构出来了
4.创建model【User】
package comlytmodel;
/**
* 用户
* @author
*
*/
public class User {
private int id;
private String userName;
private String age;
@Override
public String toString() {
return "User [id=" + id + ", age=" + age + ", userName=" + userName
+ "]";
}
public User(){
super();
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
thisage = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
thisuserName = userName;
}
public User(int id, String age, String userName) {
super();
thisid = id;
thisage = age;
thisuserName = userName;
}
}
5.创建DAO接口【在mybatis中 mapper就相当于dao】
package comlytmapper;
import javautilList;
import comlytmodelUser;
/**
* 做为 DAO的接口
* @author
*/
public interface UserMapper {
void save(User user);
boolean update(User user);
boolean delete(int id);
User findById(int id);
List
}
6.实现dao接口【mybatis中就是UserMapper.xml文件】
"http://mybatisorg/dtd/mybatis-3-mapperdtd">
insert into t_user(user_name,user_age) values(#{userName},#{age})
update t_user set user_name=#{userName},user_age=#{age} where user_id=#{id}
delete from t_user where user_id=#{id}
select user_id id,user_name userName,user_age age from t_user where user_id=#{id}
select user_id id,user_name userName,user_age age from t_user
这里对这个xml文件作几点说明:
1、namespace必须与对应的接口全类名一致。
2、id必须与对应接口的某个对应的方法名一致即必须要和UserMapper.java接口中的方法同名。
7.mybatis与Spring整合【spring-common.xml】放在src的根目录下
对于Mybatis和Spring的整合是这篇博文的重点,需要配置的内容在下面有详细的解释。
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:p="http://wwwspringframeworkorg/schema/p" xmlns:context="http://wwwspringframeworkorg/schema/context" xmlns:tx="http://wwwspringframeworkorg/schema/tx" xsi:schemaLocation=" http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd http://wwwspringframeworkorg/schema/tx http://wwwspringframeworkorg/schema/tx/spring-tx-xsd"> class="orgspringframeworkjdbcdatasourceDriverManagerDataSource"> class="orgspringframeworkjdbcdatasourceDataSourceTransactionManager">
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:p="http://wwwspringframeworkorg/schema/p"
xmlns:context="http://wwwspringframeworkorg/schema/context" xmlns:tx="http://wwwspringframeworkorg/schema/tx"
xsi:schemaLocation="
http://wwwspringframeworkorg/schema/beans
http://wwwspringframeworkorg/schema/beans/spring-beans-xsd
http://wwwspringframeworkorg/schema/context
http://wwwspringframeworkorg/schema/context/spring-context-xsd
http://wwwspringframeworkorg/schema/tx
http://wwwspringframeworkorg/schema/tx/spring-tx-xsd">
class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">
class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">
class="orgspringframeworkjdbcdatasourceDataSourceTransactionManager">
class="orgspringframeworkjdbcdatasourceDataSourceTransactionManager">
8.mybatis配置文件【mybatis-config.xml】放在src目录下
"http://mybatisorg/dtd/mybatis-3-configdtd">
配置log4j.properties
### set log levels ###
log4jrootLogger = info , Console , D
#Console
log4jappenderConsole=orgapachelog4jConsoleAppender
log4jappenderConsolelayout=orgapachelog4jPatternLayout
log4jappenderConsolelayoutConversionPattern=%d [%t] %-5p [%c] - %m%n
log4jloggerjavasqlResultSet=INFO
log4jloggerorgapache=INFO
log4jloggerjavasqlConnection=INFO
log4jloggerjavasqlStatement=INFO
log4jloggerjavasqlPreparedStatement=INFO
#output2file
log4jappenderD = orgapachelog4jDailyRollingFileAppender
log4jappenderDFile = D\:/logs/loglog
log4jappenderDAppend = true
log4jappenderDThreshold = INFO \#\# \u00E8\u00BE\u0093\u00E5\u0087\u00BAinfo\u00E7\u00BA\u00A7\u00E5\u0088\u00AB\u00E4\u00BB\u00A5\u00E4\u00B8\u008A\u00E7\u009A\u0084\u00E6\u0097\u00A5\u00E5\u00BF\u0097
log4jappenderDlayout = orgapachelog4jPatternLayout
log4jappenderDlayoutConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
10.整合Springmvc
配置【spring-mvc.xml】放在src根目录下
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:context="http://wwwspringframeworkorg/schema/context" xmlns:mvc="http://wwwspringframeworkorg/schema/mvc" xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beansxsd http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd http://wwwspringframeworkorg/schema/mvc http://wwwspringframeworkorg/schema/mvc/spring-mvc-xsd"> class="orgspringframeworkwebservletviewInternalResourceViewResolver">
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:context="http://wwwspringframeworkorg/schema/context"
xmlns:mvc="http://wwwspringframeworkorg/schema/mvc"
xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans
http://wwwspringframeworkorg/schema/beans/spring-beansxsd
http://wwwspringframeworkorg/schema/context
http://wwwspringframeworkorg/schema/context/spring-context-xsd
http://wwwspringframeworkorg/schema/mvc
http://wwwspringframeworkorg/schema/mvc/spring-mvc-xsd">
class="orgspringframeworkwebservletviewInternalResourceViewResolver">
class="orgspringframeworkwebservletviewInternalResourceViewResolver">
11.配置web.xml
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xsi:schemaLocation="http://javasuncom/xml/ns/javaee http://javasuncom/xml/ns/javaee/web-app_2_xsd">
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance"
xsi:schemaLocation="http://javasuncom/xml/ns/javaee
http://javasuncom/xml/ns/javaee/web-app_2_xsd">
12.建立service以及service实现【UserService.java】
package comlytservice;
import javautilList;
import comlytmodelUser;
public interface UserService {
void save(User user);
boolean update(User user);
boolean delete(int id);
User findById(int id);
List
}
【UserServiceImpl.java】
package comlytserviceimpl;
import javautilList;
import javaxannotationResource;
import orgspringframeworkstereotypeService;
import orgspringframeworktransactionannotationTransactional;
import comlytmapperUserMapper;
import comlytmodelUser;
import comlytserviceUserService;
@Service
@Transactional
// 此处不再进行创建SqlSession和提交事务,都已交由spring去管理了。
public class UserServiceImpl implements UserService {
@Resource
private UserMapper mapper;
/**
* 根据 id 删除 数据
*/
public boolean delete(int id) {
return mapperdelete(id);
}
/**
* 查询User的全部数据
*/
public List
List
return findAllList;
}
/**
* 根据 id 查询 对应数据
*/
public User findById(int id) {
User user = mapperfindById(id);
return user;
}
/**
* 新增数据
*/
public void save(User user) {
mappersave(user);
}
/**
* 根据 id 修改对应数据
*/
public boolean update(User user) {
return mapperupdate(user);
}
}
13.建立Controller【UserController.java】
package comlytcontroller;
import javaioIOException;
import javaioPrintWriter;
import javautilList;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgspringframeworkbeansfactoryannotationAutowired;
import orgspringframeworkstereotypeController;
import orgspringframeworkuiModel;
import orgspringframeworkwebbindannotationRequestMapping;
import comlytmodelUser;
import comlytserviceUserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 获取所有用户列表
* @param request
* @return
*/
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request,Model model){
List
modeladdAttribute("userList", user);
requestsetAttribute("userList", user);
return "/allUser";
}
/**
* 跳转到添加用户界面
* @param request
* @return
*/
@RequestMapping("/toAddUser")
public String toAddUser(){
return "/addUser";
}
/**
* 添加用户并重定向
* @param user
* @param request
* @return
*/
@RequestMapping("/addUser")
public String addUser(User user,Model model){
userServicesave(user);
return "redirect:/user/getAllUser";
}
/**
*编辑用户
* @param user
* @param request
* @return
*/
@RequestMapping("/updateUser")
public String updateUser(User user,HttpServletRequest request,Model model){
if(userServiceupdate(user)){
user = userServicefindById(usergetId());
requestsetAttribute("user", user);
modeladdAttribute("user", user);
return "redirect:/user/getAllUser";
}else{
return "/error";
}
}
/**
* 根据id查询单个用户
* @param id
* @param request
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id,HttpServletRequest request,Model model){
requestsetAttribute("user", userServicefindById(id));
modeladdAttribute("user", userServicefindById(id));
return "/editUser";
}
/**
* 删除用户
* @param id
* @param request
* @param response
*/
@RequestMapping("/delUser")
public void delUser(int id,HttpServletRequest request,HttpServletResponse response){
String result = "{\"result\":\"error\"}";
if(userServicedelete(id)){
result = "{\"resulthttp://\":\"success\"}";
}
responsesetContentType("application/json");
try {
PrintWriter out = responsegetWriter();
outwrite(result);
} catch (IOException e) {
eprintStackTrace();
}
}
}
至此 大部分架构以及代码已经搭建完成
我们来做前台页面--修改index.jsp
<%@ page language="java" import="javautil*" pageEncoding="utf-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme() + "://"
+ requestgetServerName() + ":" + requestgetServerPort()
+ path + "/";
%>
在 WEB-INF 下新建jsp文件夹【里面新建以下文件】【addUser.jsp】【allUser.jsp】【editUser.jsp】
addUser.jsp内容
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requeshttp://tgetServerName()+":"+requestgetServerPort()+path+"/";
%>
<%@ taglib uri="http://javasuncom/jsp/jstl/core" prefix="c"%>
姓名:
年龄:
allUser.jsp
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>
<%@ taglib uri="http://javasuncom/jsp/jstl/core" prefix="c"%>
editUser.jsp
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>
<%@ taglib uri="http://javasuncom/jsp/jstl/core" prefix="c"%>
姓名:
年龄:
到此 简单的整合SSM 【基本的操作内容已经完成】
让你们看下界面运行,地址:http://localhost:8088/SpringMVC_Spring_mybatis/
首页:
点击编辑进去:
点击添加进去:
由于我在网络问题 图片暂时没有上传上去。【现在我把图片都正上去了,希望多多支持】
对应的数据库 表
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50515
Source Host : localhost:3306
Source Database : db_zsl
Target Server Type : MYSQL
Target Server Version : 50515
File Encoding : 65001
Date: 2016-05-16 16:10:51
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `t_user`
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) NOT NULL,
`user_age` varchar(20) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('2', '朱佳鹏', '25');
INSERT INTO `t_user` VALUES ('3', '傻狍子', '150');
INSERT INTO `t_user` VALUES ('4', 'jiuqiyuliang', '23');
INSERT INTO `t_user` VALUES ('5', 'jiuqiyuliang', '23');
INSERT INTO `t_user` VALUES ('6', 'jiuqiyuliang', '23');
INSERT INTO `t_user` VALUES ('7', 'jiuqiyuliang', '23');
INSERT INTO `t_user` VALUES ('9', 'liyintao123', '23123');
-- ----------------------------
-- Table structure for `user_t`
-- ----------------------------
DROP TABLE IF EXISTS `user_t`;
CREATE TABLE `user_t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(40) NOT NULL,
`password` varchar(255) NOT NULL,
`age` int(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_t
-- ----------------------------
INSERT INTO `user_t` VALUES ('1', '测试', 'sfasgfaf', '24');
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~