1.最终完成的工程架构:
2.下面详细讲解一下
新建Maven项目:点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp。
在弹出的new project 选项卡中填写GroupId和Artifactid,自己随便写就是了。
点Next,然后点那三个点,选择自己本地的maven。
然后点那个+号,Name跟Value跟我的输一样就是了。
然后点OK,Next,Finish。
右下角那个框里点右边那个,然后等到BUILD SUCCESS。
接下来可以在数据库里建表了,类似于下面这张表。
在这之前要对项目的pom.xml文件进行配置,在此文件中配置项目所需要的jar包。
pom.xml
4.0.0
com.zhongruan
ssm00
1.0-SNAPSHOT
war
ssm00 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
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
ssm00
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
在main下面建两个Directory:分别命名为java,resources。
然后右击,找到Mark Directory as,分别选Source Root,Resources Root。
在刚才新建的java文件下新建“com.zhongruan.bean”包,依次展开,再在zhongruan包下新建三个包,分别命名为:service,dao,controller。
在src下面建一个Directory,命名为target,然后找到Mark Directory as,设置为Excluded。
在bean下面创建类UserInfo,代码如下:
package com.zhongruan.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 + '\'' +
'}';
}
}
接下来在dao下面创建接口IUserDao。
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public List findAll();
public void insert(UserInfo userInfo);
public void delete(UserInfo userInfo);
public void update(UserInfo userInfo);
public UserInfo findId(int id);
}
在controller下面创建Usercontroller
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 all = userService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("userInfos", all);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("/toAddUser.do")
public ModelAndView insert() {
ModelAndView mv = new ModelAndView();
mv.setViewName("addUser");
return mv;
}
@RequestMapping("/save.do")
public ModelAndView save(UserInfo userinfo) {
userService.insert(userinfo);
ModelAndView mv = new ModelAndView();
mv = findAll();
return mv;
}
@RequestMapping("/delete.do")
public ModelAndView delete(UserInfo userinfo) {
userService.delete(userinfo);
ModelAndView mv = new ModelAndView();
mv = findAll();
return mv;
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id) {
ModelAndView mv = new ModelAndView();
mv = findAll();
UserInfo userinfo = userService.findId(id);
mv.addObject("userInfo", userinfo);
mv.setViewName("updateUser");
return mv;
}
@RequestMapping("/update.do")
public ModelAndView update(UserInfo userinfo) {
ModelAndView mv = new ModelAndView();
userService.update(userinfo);
mv = findAll();
return mv;
}
}
在Service下面建接口IUserService:
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List findAll();
public void insert(UserInfo userinfo);
public void delete(UserInfo userinfo);
public UserInfo findId(int id);
public void update(UserInfo userinfo);
}
在service下面建一个包:impl,在这个包下面创建类:UserService。
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.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();
}
public void insert(UserInfo userinfo){
userDao.insert(userinfo);
}
public void delete(UserInfo userinfo){
userDao.delete(userinfo);
}
public void update(UserInfo userinfo){
userDao.update(userinfo);
}
public UserInfo findId(int id) {
return userDao.findId(id);
}
}
在resources下面建包mapper,然后在这个包下面新建一个UserMapper.xml文件。
insert into userinfo(id,username,password) values(#{id},#{username},#{password})
delete from userinfo where id =#{id}
update userinfo set username=#{username},password=#{password} where id=#{id}
<%@ 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框架的管理系统:简单实现增、删、改、查。
新增用户
allUser:
<%@ 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列表
updateUser:
<%@ 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框架的管理系统:简单实现增、删、改、查。
修改用户
user-list:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
数据 - AdminLTE2定制版
web.xml:
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
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
Hello World!
查询所有用户
在resorces下面建四个文件:
applicationContent.xml:
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
log4j.properties:
# 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
spring-mvc.xml:
接下来用tomcat运行。点这个Add Configuration
点加号,进去后选最底下那个33 items more。然后点 tomcat server -> local 选择下载安装到本地的tomcat服务器的地址
切换到Deployment选项页 点击 + 号选择 Arctifact
添加 项目名:war exploded 打包
最后在Application context 中选择 空白 那一项,点击 Apply 应用。
最后点Run“tomcat7”运行
后面的因为我的MySQL出问题了,连不上本机数据库,所以暂时看不了了。