SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为
相关工具环境配置可参考本人上篇博客
1.新建Maven项目
在IDEA界面依次点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp (注意:不要误选为下图画×一项)
输入GroupId组名(项目组织唯一标识)和ArtififactId项目名(项目唯一标识),学习时可随意设置。
注意:当用户第一次配置时,可能无法从下图路径成功创建,因为在个人用户下无.m2文件夹,这个时候可选择个人maven路径下的settings.xml文件。该项目创建成功后,若出现.m2文件夹,则将上述settings.xml(已配制好阿里云服务器)拷入到.m2,以后按照默认路径启动即可。
2. 在新建项目中添加一些包和目录。
弹出项目窗口后,软件会自动下载maven的一些包,当项目中出现src文件夹和下方消息框出现BUILD SUCCESS字样时为下载成功。(若下载速度很慢,可重新配置一下启动的setting.xml文件,确认是否添加了阿里云镜像,尤其注意一下粘贴导入文本时的格式和导入位置)
右击java,将该文件夹设置为sources root之后,可向该文件夹中写java文件,同理设置resource文件夹设置为Resources root。
在resource文件夹下新建Directory:“mapper”(用于存放xxxMapper.xml文件),新建文件:“db.properties”(mysql数据库配置文件),”log4j.properties”(日志输出配置文件),”applicationContext.xml”(mybatis框架配置文件),"spring-mvc.xml"配置文件。
在webapp目录下新建“jsp”包,存放xxx.jsp显示界面。
4.0.0
com.tyx
ssm0628
1.0-SNAPSHOT
war
ssm0628 Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
5.0.2.RELEASE
3.2.6
1.7.7
1.2.17
0.9.5.2
1.1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.mchange
c3p0
${c3p0.version}
taglibs
standard
${taglibs.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
ssm0628
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
若出现红色报红,说明缺少一些jar包,可右击pom.xml,依次点击下图画横线部分,软件会自动下载缺少的包(若网速很慢,同样可检查一下seetings.xml配置)
在bean文件夹下新建UserInfo.java文件。(注意不要复制package 一行)
getter/setter()以及toString()方法都可以通过快捷键ctrl+alt+s,然后点击Generate…,选择对应方法即可。
package com.tyx.bean;不要复制此行
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}`
在service包下新建IUserService.java
package com.tyx.service;
import com.tyx.bean.UserInfo;
;以上根据个人情况
import java.security.PublicKey;
import java.util.List;
public interface IUserService {
public List findAll();
int addUser(UserInfo user);
int deleteUser(UserInfo user);
int updateUser(UserInfo user);
}
在dao包下新建IUserDao.java。
package com.tyx.dao;
import com.tyx.bean.UserInfo;
;以上根据个人情况
import java.util.List;
public interface IUserDao {
public List findAll();
int addUser(UserInfo user);
int deleteUser(UserInfo user);
int updateUser(UserInfo user);
}
package com.tyx.service.impl;
import com.tyx.bean.UserInfo;
import com.tyx.dao.IUserDao;
import com.tyx.service.IUserService;
;以上根据自己的情况修改
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List findAll() {
return userDao.findAll();
}
@Override
public int addUser(UserInfo user) {
return userDao.addUser(user);
}
@Override
public int deleteUser(UserInfo user) {
return userDao.deleteUser(user);
}
@Override
public int updateUser(UserInfo user) {
return userDao.updateUser(user);
}
}
package com.tyx.controller;
import com.tyx.bean.UserInfo;
import com.tyx.service.IUserService;
import com.tyx.service.impl.UserService;
;以上根据自己修改
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll(){
List userInfos =userService.findAll();
ModelAndView mv =new ModelAndView();
mv.addObject("ui",userInfos);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("toAddUser.do")
public String toAddUser() {
return "addUser";
}
@RequestMapping("save.do")
public String addUser(UserInfo userInfo) {
userService.addUser(userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("delete.do")
public String deleteUser(UserInfo userInfo) {
userService.deleteUser(userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("toUpdate.do")
public String toUpdateUser(UserInfo userInfo) {
//model.addAttribute("user", userService.queryById(username));
return "updateUser";
}
@RequestMapping("updateUser.do")
public String updateUser(UserInfo userInfo) {
userService.updateUser(userInfo);
// userInfo = userService.queryById(userInfo.getUsername());
//model.addAttribute("user", userInfo);
return "redirect:/user/findAll.do";
}
}
insert into userinfo(id,username,password) VALUE (#{id},#{username}, #{password})
delete from userinfo where id =#{id}
update userinfo
SET id = #{id},username = #{username},password = #{password}
WHERE id = #{id}
insert into userinfo(id,username,password) VALUE (#{id},#{username}, #{password})
delete from userinfo where id =#{id}
update userinfo
SET id = #{id},username = #{username},password = #{password}
WHERE id = #{id}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbc?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=TYX19981022
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
*.do
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
Hello World!
查询所有用户
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
新增用户
基于SSM框架的管理系统:简单实现增、删、改、查。
新增用户
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
user列表
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
修改用户
基于SSM框架的管理系统:简单实现增、删、改、查。
修改用户
最后祝大家都能够成功搭建!