话不多说,ssm框架整合小列子,这次记录一个单表增删改查的ssm例子,所以才手写mapper,平时我都是用逆向工程自动生成mapper,简单方便。
需要了解逆向工程请看:https://blog.csdn.net/Destiny_strive/article/details/82915354
步骤
1.创建web Maven项目
2.创建数据库配置文件:jdbc.properties
3.项目总体目录:
4.添加spring配置文件:applicationContext.xml
5.添加springMVC配置文件:springMVC.xml
6.修改web.xml
7.创建数据库相关表
8.根据数据库表创建pojo类:User.java
9.编写dao层->mapper接口和xml文件 :UserMapper.java、UserMapper.xml
10.编写service层->UserService.java、UserServiceImpl.java
11.编写controller层->UserController.java
12.在jsp文件夹下创建edit.jsp和view.jsp
13.现在配置tomcat运行就能看到user表的列表了,可操作做增删改查
创建项目很简单的,不做描述,接下来在pom文件加入项目所需依赖:
org.springframework
spring-context
4.2.4.RELEASE
org.springframework
spring-tx
4.2.4.RELEASE
org.springframework
spring-webmvc
4.2.4.RELEASE
mysql
mysql-connector-java
5.1.46
com.alibaba
druid
1.1.10
org.mybatis
mybatis
3.4.1
org.mybatis
mybatis-spring
1.3.0
org.springframework
spring-jdbc
4.2.4.RELEASE
junit
junit
4.12
test
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
resources目录下添加:jdbc.properties
这里根据自己数据库做相应修改
#数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=admin
先给出项目总体目录
创建好java目录下的分层的包,resources下创建存放mapper的包,WEB-INF下创建jsp放跳转的页面(下面的配置文件中会配置这些路径)
resources目录下添加:applicationContext.xml,文件内容如下:
每个配置给出了相应的注释,请自行查看
resources目录下添加:springMVC.xml,并且根据文件中‘视图定位’的配置,所以在WEB-INF下创建jsp文件夹
文件内容如下:
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
mvc-dispatcher
/
我的jdbc.properties文件中指明了数据库名为test,在test数据库下我创建了一张user表,结构和数据如下:
pojo的编写规则:数据库字段对应类字段,字段类型要一直 。
package com.demo.pojo;
public class User {
private Integer id;
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
UserMapper.xml放在resources\mapper下,UserMapper.java放在com.demo.dao这个包下:
注意namespace="com.demo.dao.UserMapper",这里一定写对应的mapper接口具体路径
UserMapper接口的编写规则:
(1)方法名和对应的mapper配置文件中查询语句的id相同
(2)返回类型和resultType的类型一致,没有就是void。
(3)方法中的参数列表中的类型和parameterType一致。
(4)mapper配置文件的namespace对应mapper接口类的全路径。
package com.demo.dao;
import com.demo.pojo.User;
import java.util.List;
public interface UserMapper {
List list();
void del(int id);
void update(User user);
void add(User user);
User get(int id);
}
update user set name=#{name} ,password=#{password} where id=#{id}
insert into user values (null,#{name},#{password});
delete from user where id=#{id}
UserService.java:
package com.demo.service;
import com.demo.pojo.User;
import java.util.List;
public interface UserService {
List list();
void delUser(int id);
void updateUser(User user);
void addUser(User user);
User get(int id);
}
UserServiceImpl.java
package com.demo.service.impl;
import com.demo.dao.UserMapper;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
public List list() {
return userMapper.list();
}
public void addUser(User user) {
userMapper.add(user);
}
public void delUser(int id) {
userMapper.del(id);
}
public void updateUser(User user) {
userMapper.update(user);
}
public User get(int id) {
return userMapper.get(id);
}
}
package com.demo.controller;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("list")
public String list(Model model){
List us= userService.list();
model.addAttribute("us", us);
return "view";
}
@RequestMapping("add")
public String add(User user,Model model){
userService.addUser(user);
return "redirect:list";
}
@RequestMapping("del")
public String del(int id){
userService.delUser(id);
return "redirect:list";
}
@RequestMapping("editUI")
public String editUI(int id,Model model){
User user = userService.get(id);
model.addAttribute("user",user);
return "edit";
}
@RequestMapping("update")
public String update(User user){
userService.updateUser(user);
return "redirect:list";
}
}
view.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
edit.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
最后把index.jsp内容修改为:
<%
response.sendRedirect(request.getContextPath()+"/list");
%>