创建SSM的基本文件。
新建Maven项目:
点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp,在弹出的new project 选项卡中填写GroupId和Artifactid。
在新建的项目中添加所需要的文件/文件夹。首先在项目的根目录下新建target文件夹。
2.1在src/main目录下新建Directory:“java”,并将其设置为“Source Root“。
2.2在刚才新建的java文件下新建“com”包,再在com包下新建四个包,分别命名为:bean,service,dao,controller。在resource包下新建Directory:“mapper”(用于存放xxxMapper.xml文件)和“spring”(用于存放spring-xxx.xml配置文件),新建文件:“jdbc.properties”(mysql数据库配置文件),”log4j.properties”(日志输出配置文件),”mybatis-config.xml”(mybatis框架配置文件)。
2.3 在web-inf目录下新建“jsp”包。
下图为创建后的文件结构。
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
## 2.2配置POM文件denpendencies dependencies> 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
## 2.3applcationContext.xml
## 2.4 db.preperties文件
这个文件中记得改变为自己的JDBC的用户名和密码,不然会导致连接失败。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/idea?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
#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
public class Userinfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
然后添加geiter,setter,tostring方法。
package com.zhongruan.dao;
import com.zhongruan.bean.Userinfo;
import java.util.List;
public interface IUserDao {
int addUser(Userinfo user);
int deleteUser(long id);
int updateUser(Userinfo user);
public Userinfo queryId(long id);
public List findAll();
}
在这里插入代码片
insert into userinfo (id,username,password) value (#{id},#{username},#{password});
update userinfo
set username=#{username},password=#{password} where id=#{id};
delete from userinfo where id=#{id};
package com.zhongruan.service;
import com.zhongruan.bean.Userinfo;
import java.util.List;
public interface IUserService {
int addUser(Userinfo user);
int deleteUser(long id);
int updateUser(Userinfo user);
public Userinfo queryId(long id);
List findAll();
}
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 UserServiceimpl implements IUserService {
@Autowired
IUserDao userdao ;
@Override
public int addUser(Userinfo user)
{
return userdao.addUser(user);
}
public int deleteUser(long id)
{
return userdao.deleteUser(id);
}
public int updateUser(Userinfo user)
{
return userdao.updateUser(user);
}
public Userinfo queryId(long id)
{
return userdao.queryId(id);
}
public List findAll()
{
return userdao.findAll();
}
}
package com.zhongruan.controller;
import com.sun.org.apache.xpath.internal.operations.Mod;
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.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
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 user) {
userService.addUser(user);
return "redirect:/user/findAll.do";
}
@RequestMapping("/delete.do")
public String deleteUser(Long id) {
userService.deleteUser(id);
return "redirect:/user/findAll.do";
}
@RequestMapping("update.do")
public String updateUser(Model model, Userinfo userinfo)
{
userService.updateUser(userinfo);
userinfo= userService.queryId(userinfo.getId());
model.addAttribute("user",userinfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("toUpdateUser.do")
public String toUpdatePaper(Model model, Long id) {
model.addAttribute("user", userService.queryId(id));
return "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” %>
新增用户
<%@ 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” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + “?/”
+ request.getServerName() + “:” + request.getServerPort()
+ path + “/”;
%>
修改用户
用户列表 —— 显示所有用户
到此为止,项目基本完成了构建。在修改一些变量时,要记得在其他的放也修改对应的变量,否则会出现404,500等异常。
在这给出我的数据库创建图,要记得在一些地方要根据自己的数据库进行修改代码。我的数据库表名为idea,结构为下图