ssm综合增删改查
项目结构
使用mybatis逆向工程生成需要的Bean类和Mapper复制到项目中
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
mydata.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解-->
<mvc:annotation-driven/>
<!--配置自动扫描包-->
<context:component-scan base-package="com.weiwei.ssmdemo"/>
<!--配置加载mydata.properties-->
<context:property-placeholder location="classpath:mydata.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置SqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--注入sql映射文件路径-->
<property name="mapperLocations" value="classpath:mapper/UserBeanMapper.xml"/>
</bean>
<!--配置扫描数据库访问接口包,创建数据库访问接口对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.weiwei.ssmdemo.mapper"/>
</bean>
</beans>
Service层
package com.weiwei.ssmdemo.service;
import com.weiwei.ssmdemo.bean.UserBean;
import java.util.List;
public interface UserBeanService {
void insertUserBean(UserBean userBean);
void updateUserBean(UserBean userBean);
void deleteUserBean(int userid);
List<UserBean> selectUserBean();
List<UserBean> selectUserBeanByWhere(UserBean userBean);
}
package com.weiwei.ssmdemo.service;
import com.weiwei.ssmdemo.bean.UserBean;
import com.weiwei.ssmdemo.bean.UserBeanExample;
import com.weiwei.ssmdemo.mapper.UserBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userBeanService")
public class UserBeanServiceImpl implements UserBeanService{
@Autowired
private UserBeanMapper userBeanMapper;
@Override
public void insertUserBean(UserBean userBean) {
userBeanMapper.insert(userBean);
}
@Override
public void updateUserBean(UserBean userBean) {
userBeanMapper.updateByPrimaryKey(userBean);
}
@Override
public void deleteUserBean(int userid) {
userBeanMapper.deleteByPrimaryKey(userid);
}
@Override
public List<UserBean> selectUserBean() {
return userBeanMapper.selectByExample(null);
}
@Override
public List<UserBean> selectUserBeanByWhere(UserBean userBean) {
UserBeanExample userBeanExample = new UserBeanExample();
UserBeanExample.Criteria criteria = userBeanExample.createCriteria();
if(userBean.getUserName()!=null&&userBean.getUserName().length()>0){
criteria.andUserNameLike("%"+userBean.getUserName()+"%");
}
if(userBean.getUserAge()!=null){
criteria.andUserAgeEqualTo(userBean.getUserAge());
}
if (userBean.getUserId()!=null){
criteria.andUserIdEqualTo(userBean.getUserId());
}
return userBeanMapper.selectByExample(userBeanExample);
}
}
Controller层
package com.weiwei.ssmdemo.controller;
import com.weiwei.ssmdemo.bean.ResBean;
import com.weiwei.ssmdemo.bean.UserBean;
import com.weiwei.ssmdemo.service.UserBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserBeanController {
@Autowired
private UserBeanService userBeanService;
@RequestMapping("adduser.do")
@ResponseBody
//添加用户信息
public ResBean addUser(@RequestBody UserBean userBean){
try {
userBeanService.insertUserBean(userBean);
return new ResBean(true,"添加成功");
}catch (Exception e){
e.printStackTrace();
return new ResBean(false,"添加失败");
}
}
//修改用户信息
@RequestMapping("updateuser.do")
@ResponseBody
public ResBean updateUser(@RequestBody UserBean userBean){
try {
userBeanService.updateUserBean(userBean);
return new ResBean(true,"修改成功");
}catch (Exception e){
e.printStackTrace();
return new ResBean(false,"修改失败");
}
}
//删除用户信息
@RequestMapping("deleteuser.do")
@ResponseBody
public ResBean deleteUser(int userid){
try {
userBeanService.deleteUserBean(userid);
return new ResBean(true,"删除成功");
}catch (Exception e){
e.printStackTrace();
return new ResBean(false,"删除失败");
}
}
//查询所有用户信息
@RequestMapping("selectall.do")
@ResponseBody
public List<UserBean> selectAllUser(){
try {
return userBeanService.selectUserBean();
}catch (Exception e){
e.printStackTrace();
return null;
}
}
//条件查询
@RequestMapping("selectwhere.do")
@ResponseBody
public List<UserBean> selectWhere(@RequestBody UserBean userBean){
try{
return userBeanService.selectUserBeanByWhere(userBean);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
返回类ResBean
package com.weiwei.ssmdemo.bean;
public class ResBean {
private boolean flag;
private String success;
public ResBean(boolean flag, String success) {
this.flag = flag;
this.success = success;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
}
返给前端的接口文档
1、添加用户信息
简要描述
- ssm增删改查
请求URL
- http://localhost:8080/ssmdemo/user/adduser.do
请求方式
- post
请求参数示例
{ "userName": "大三", "userAge": 27, "userAddress": "存储" } |
json字段说明
字段名 | 必选 | 类型 | 说明 |
userName | 是 | string | 用户姓名 |
userAge | 是 | number | 用户年龄 |
userAddress | 是 | string | 用户地址 |
备注
2、修改用户信息
简要描述
- 修改用户信息
请求URL
- http://localhost:8080/ssmdemo/user/updateuser.do
请求方式
- post
请求参数示例
{ "userId": 3, "userName": "韩信", "userAge": 40, "userAddress": "军营" } |
json字段说明
字段名 | 必选 | 类型 | 说明 |
userId | 是 | number | 需要修改的用户id |
userName | 是 | string | 需要修改的用户姓名 |
userAge | 是 | number | 需要修改的用户年龄 |
userAddress | 是 | string | 需要修改的用户地址 |
备注
3、删除用户信息
简要描述
- 删除用户信息
请求URL
- http://localhost:8080/ssmdemo/user/deleteuser.do
请求方式
- get
请求Query参数
参数名 | 必选 | 类型 | 说明 |
userid | 是 | number | 需要删除的用户id |
备注
4、查询所有用户信息
简要描述
- 查询所有用户信息
请求URL
- http://localhost:8080/ssmdemo/user/selectall.do
请求方式
- post
成功返回示例
[ { "userId": 2, "userName": "张三", "userAge": 12, "userAddress": "西安" }, { "userId": 3, "userName": "韩信", "userAge": 40, "userAddress": "军营" }, { "userId": 4, "userName": "李思思", "userAge": 54, "userAddress": "重庆" } ] |
备注
5、条件查询
简要描述
- 条件查询
请求URL
- http://localhost:8080/ssmdemo/user/selectwhere.do
请求方式
- post
请求参数示例
{ "userName": "三" } |
成功返回示例
[ { "userId": 2, "userName": "张三", "userAge": 12, "userAddress": "西安" }, { "userId": 7, "userName": "小三", "userAge": 27, "userAddress": "存储" }, { "userId": 8, "userName": "大三", "userAge": 27, "userAddress": "存储" } ] |
备注