博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目
阅读量:6672 次
发布时间:2019-06-25

本文共 15734 字,大约阅读时间需要 52 分钟。

一、前言

如果还不了解剧情,请返回第3集的剧情             

二、在Eclipse中创建Maven类型的SSM项目

2.1:SSM简介

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。其中spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。SpringMVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架

2.2:搭建maven类型的SSM项目的步骤

2.2.1:在pom.xml文件引入SSM所需要的包

4.0.0
king.weixin
KingWeixin
war
0.0.1-SNAPSHOT
KingWeixin Maven Webapp
http://maven.apache.org
UTF-8
1.8
4.3.5.RELEASE
1.7.2
UTF-8
junit
junit
3.8.1
test
org.springframework
spring-webmvc
${org.springframework-version}
org.springframework
spring-context-support
${org.springframework-version}
org.springframework
spring-tx
${org.springframework-version}
org.springframework
spring-jdbc
${org.springframework-version}
org.springframework
spring-web
${org.springframework-version}
org.springframework
spring-context
${org.springframework-version}
commons-logging
commons-logging
mysql
mysql-connector-java
5.1.31
commons-dbcp
commons-dbcp
1.4
org.mybatis
mybatis-spring
1.3.0
org.mybatis
mybatis
3.4.1
javax.servlet
javax.servlet-api
3.0.1
provided
commons-configuration
commons-configuration
1.9
commons-fileupload
commons-fileupload
1.3.2
commons-io
commons-io
2.5
commons-net
commons-net
3.5
org.apache.commons
commons-lang3
3.2
org.apache.logging.log4j
log4j-api
2.10.0
org.apache.logging.log4j
log4j-core
2.10.0
org.apache.logging.log4j
log4j-web
2.10.0
org.apache.logging.log4j
log4j-jcl
2.10.0
org.apache.logging.log4j
log4j-slf4j-impl
2.10.0
javax.servlet
jstl
1.2
KingWeixin
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
View Code

2.2.2:修改web.xml文件

KingWeixin
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
CharacterEncodingFilter
/*
contextConfigLocation
classpath*:/config/spring-*.xml
org.springframework.web.context.ContextLoaderListener
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:/config/spring-mvc.xml
1
springDispatcherServlet
/
View Code

2.2.2:新建SSM相关文件

首先,创建spring-mvc.xml主要配置文件

View Code

其次创建spring-common.xml通用性配置文件

com.mysql.jdbc.Driver
jdbc:mysql://数据服务器IP:3306/king?user=root&password=密码&useUnicode=true&characterEncoding=utf-8
View Code

最后创建mybatis-config.xml数据持久层配置文件

View Code

关系,两个Spring文件会在web.xml中引入,mybatis-config文件会在sping定义数据源配置的spring-common.xml文件中引入,具体可以看代码,截止目前,项目搭建完毕,Maven-update  project的话,系统会根据pom.xml自动下载相关jar包,这就是Maven的好处

2.3:SSM查询实例

(1)新建entity类User.java

package com.king.weixin.entity;/** * @author KING * 2018-6-8 16:00:06 */public class User {    private int id;    private String age;    private String userName;    public User(){        super();    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public User(int id, String age, String userName) {        super();        this.id = id;        this.age = age;        this.userName = userName;    }}
View Code

(2)新建UserMapper.java接口类

package com.king.weixin.mapper;import java.util.List;import com.king.weixin.entity.User;public interface UserMapper {    void save(User user);    boolean update(User user);    boolean delete(int id);    User findById(int id);    List
findAll();}
View Code

(3)新建userMapper.xml

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}
View Code

(4)新建UserService.java接口类

package com.king.weixin.service;import java.util.List;import com.king.weixin.entity.User;public interface UserService {    void save(User user);    boolean update(User user);    boolean delete(int id);    User findById(int id);    List
findAll();}
View Code

(5)新建UserServiceImpl.java实现类

package com.king.weixin.service;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.king.weixin.mapper.UserMapper;import com.king.weixin.entity.User;import com.king.weixin.service.UserService;@Service@Transactional  //此处不再进行创建SqlSession和提交事务,都已交由spring去管理了public class UserServiceImpl implements UserService {        @Resource    private UserMapper mapper;    public boolean delete(int id) {                return mapper.delete(id);    }    public List
findAll() { List
findAllList = mapper.findAll(); return findAllList; } public User findById(int id) { User user = mapper.findById(id); return user; } public void save(User user) { mapper.save(user); } public boolean update(User user) { return mapper.update(user); } }
View Code

(6)新建UserController.java控制类

package com.king.weixin.controller;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.king.weixin.entity.User;import com.king.weixin.service.UserService;@Controller@RequestMapping("/user")public class UserController {    @Autowired    private UserService userService;        /**     * 获取所有用户列表     * @param request     * @return     */    @RequestMapping("/getAllUser")    public String getAllUser(HttpServletRequest request){                System.out.println("访问的是我");        List
findAll = userService.findAll(); request.setAttribute("userList", findAll); return "/index"; } /** * 跳转到添加用户界面 * @param request * @return */ @RequestMapping("/toAddUser") public String toAddUser(HttpServletRequest request){ return "/addUser"; } /** * 添加用户并重定向 * @param user * @param request * @return */ @RequestMapping("/addUser") public String addUser(User user,HttpServletRequest request){ userService.save(user); return "redirect:/user/getAllUser"; } /** *编辑用户 * @param user * @param request * @return */ @RequestMapping("/updateUser") public String updateUser(User user,HttpServletRequest request){ if(userService.update(user)){ user = userService.findById(user.getId()); request.setAttribute("user", user); return "redirect:/user/getAllUser"; }else{ return "/error"; } } /** * 根据id查询单个用户 * @param id * @param request * @return */ @RequestMapping("/getUser") public String getUser(int id,HttpServletRequest request){ request.setAttribute("user", userService.findById(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(userService.delete(id)){ result = "{\"result\":\"success\"}"; } response.setContentType("application/json"); try { PrintWriter out = response.getWriter(); out.write(result); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping("/GoLogin") public String GoLogin(HttpServletRequest request,HttpServletResponse response) { return "/login"; } @RequestMapping("/CheckLogin") public void CheckLogin(HttpServletRequest request,HttpServletResponse response) throws IOException { String sysusername="king"; String syspassword="123456"; String username=request.getParameter("userName"); String password=request.getParameter("passWord"); System.out.println("用户名:"+username); System.out.println("密码:"+password); HttpSession session=request.getSession(); if(username.equals(sysusername)&&password.equals(syspassword)) { session.setAttribute("username",username ); response.getWriter().write("success"); } else { response.getWriter().write("err"); } }}
View Code

后台的类已创建完成,下面我们新建一个index.jsp页面做测试,页面内容如下,这里我引入了jstl标签,具体代码请看下面的代码

<%@ page language="java" contentType="text/html; charset=utf8"    pageEncoding="utf8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
SpringMvc测试页

Hello,world

用户编号 年龄 姓名
${user.id} ${user.age} ${user.userName}
View Code

启动项目,访问如下图所示,访问到首页

点击查询所有用户按钮

后台我在UserController里面的getAllUser方法做了打印,可以看到请求已经到了Controller里面,实战结束

 

 

转载地址:http://rkgxo.baihongyu.com/

你可能感兴趣的文章
Codeforces Round #283 (Div. 2) ABCDE
查看>>
cacti安装spine 解决WARNING: Result from CMD not valid. Partial Result: U错误
查看>>
一步步构建大型网站架构
查看>>
(五)hadoop系列之__集群搭建SSH无密访问多台机器
查看>>
node + npm 命令
查看>>
laravel的路由设置,路由参数和路由命名(三)
查看>>
centos7下编译安装php7.3
查看>>
打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器...
查看>>
(九)Centos之搜索命令whereis、which和字符串搜索命令grep
查看>>
(二十一)JSP基础
查看>>
Web API 路由访问设置
查看>>
如何配置JDK环境变量
查看>>
四则运算二项目计划总结
查看>>
CSS3实现Animation动画
查看>>
WORD神操作!第一个技巧你就傻眼了!
查看>>
js中数组连接concat()
查看>>
HDU 2159 FATE
查看>>
docker-compose 意见搭建Samba文件共享服务
查看>>
Keepalived+Nginx搭建主从高可用并带nginx检测
查看>>
VMware 中如何打开U盘弹出U盘或者移动硬盘的(两种方法)
查看>>