成都网站建设设计

将想法与焦点和您一起共享

快速搭建SSM框架(Maven)五步曲的方法步骤

项目完整搭建链接:https://gitee.com/DaNanHai04/ssm_parent.git

创新互联公司主要从事成都网站设计、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务保德,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

第一步 创建一个父工程:

导入父工程的pom坐标:


 
 
  org.springframework
  spring-context
  5.0.2.RELEASE
 
 
 
 
  org.springframework
  spring-webmvc
  5.0.2.RELEASE
 
 
 
 
  org.springframework
  spring-jdbc
  5.0.2.RELEASE
 
 
  org.springframework
  spring-tx
  5.0.2.RELEASE
 
 
  org.aspectj
  aspectjweaver
  1.8.7
 
 
 
 
  org.springframework
  spring-test
  5.0.2.RELEASE
 
 
 
 
  org.mybatis
  mybatis
  3.4.5
 
 
 
 
  org.mybatis
  mybatis-spring
  1.3.1
 
 
 
 
  MySQL
  mysql-connector-java
  5.1.30
 
 
  com.alibaba
  druid
  1.1.10
 
 
 
 
  com.alibaba
  fastjson
  1.2.56
 
 
 
 
  jstl
  jstl
  1.2
 
 
 
 
  log4j
  log4j
  1.2.17
 
 
  junit
  junit
  4.12
 
 

第二步 创建ssm_pojo子模块(mybatis逆向这里不做赘述,直接将生成好的复制即可)

创建实体类TbBrand实体类

package com.itcode.pojo;
 
import java.io.Serializable;
 
public class TbBrand implements Serializable {
  private Long id;
 
  private String name;
 
  private String firstChar;
  
  //此处省略get/set方法以及toString方法

创建TbBrandExample条件类 

package com.itcode.pojo;
 
import java.util.ArrayList;
import java.util.List;
 
public class TbBrandExample {
  protected String orderByClause;
 
  protected boolean distinct;
 
  protected List oredCriteria;
 
  public TbBrandExample() {
    oredCriteria = new ArrayList();
  }
 
  public void setOrderByClause(String orderByClause) {
    this.orderByClause = orderByClause;
  }
 
  public String getOrderByClause() {
    return orderByClause;
  }
 
  public void setDistinct(boolean distinct) {
    this.distinct = distinct;
  }
 
  public boolean isDistinct() {
    return distinct;
  }
 
  public List getOredCriteria() {
    return oredCriteria;
  }
 
  public void or(Criteria criteria) {
    oredCriteria.add(criteria);
  }
 
  public Criteria or() {
    Criteria criteria = createCriteriaInternal();
    oredCriteria.add(criteria);
    return criteria;
  }
 
  public Criteria createCriteria() {
    Criteria criteria = createCriteriaInternal();
    if (oredCriteria.size() == 0) {
      oredCriteria.add(criteria);
    }
    return criteria;
  }
 
  protected Criteria createCriteriaInternal() {
    Criteria criteria = new Criteria();
    return criteria;
  }
 
  public void clear() {
    oredCriteria.clear();
    orderByClause = null;
    distinct = false;
  }
 
  protected abstract static class GeneratedCriteria {
    protected List criteria;
 
    protected GeneratedCriteria() {
      super();
      criteria = new ArrayList();
    }
 
    public boolean isValid() {
      return criteria.size() > 0;
    }
 
    public List getAllCriteria() {
      return criteria;
    }
 
    public List getCriteria() {
      return criteria;
    }
 
    protected void addCriterion(String condition) {
      if (condition == null) {
        throw new RuntimeException("Value for condition cannot be null");
      }
      criteria.add(new Criterion(condition));
    }
 
    protected void addCriterion(String condition, Object value, String property) {
      if (value == null) {
        throw new RuntimeException("Value for " + property + " cannot be null");
      }
      criteria.add(new Criterion(condition, value));
    }
 
    protected void addCriterion(String condition, Object value1, Object value2, String property) {
      if (value1 == null || value2 == null) {
        throw new RuntimeException("Between values for " + property + " cannot be null");
      }
      criteria.add(new Criterion(condition, value1, value2));
    }
 
    public Criteria andIdIsNull() {
      addCriterion("id is null");
      return (Criteria) this;
    }
 
    public Criteria andIdIsNotNull() {
      addCriterion("id is not null");
      return (Criteria) this;
    }
 
    public Criteria andIdEqualTo(Long value) {
      addCriterion("id =", value, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdNotEqualTo(Long value) {
      addCriterion("id <>", value, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdGreaterThan(Long value) {
      addCriterion("id >", value, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdGreaterThanOrEqualTo(Long value) {
      addCriterion("id >=", value, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdLessThan(Long value) {
      addCriterion("id <", value, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdLessThanOrEqualTo(Long value) {
      addCriterion("id <=", value, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdIn(List values) {
      addCriterion("id in", values, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdNotIn(List values) {
      addCriterion("id not in", values, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdBetween(Long value1, Long value2) {
      addCriterion("id between", value1, value2, "id");
      return (Criteria) this;
    }
 
    public Criteria andIdNotBetween(Long value1, Long value2) {
      addCriterion("id not between", value1, value2, "id");
      return (Criteria) this;
    }
 
    public Criteria andNameIsNull() {
      addCriterion("name is null");
      return (Criteria) this;
    }
 
    public Criteria andNameIsNotNull() {
      addCriterion("name is not null");
      return (Criteria) this;
    }
 
    public Criteria andNameEqualTo(String value) {
      addCriterion("name =", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameNotEqualTo(String value) {
      addCriterion("name <>", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameGreaterThan(String value) {
      addCriterion("name >", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameGreaterThanOrEqualTo(String value) {
      addCriterion("name >=", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameLessThan(String value) {
      addCriterion("name <", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameLessThanOrEqualTo(String value) {
      addCriterion("name <=", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameLike(String value) {
      addCriterion("name like", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameNotLike(String value) {
      addCriterion("name not like", value, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameIn(List values) {
      addCriterion("name in", values, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameNotIn(List values) {
      addCriterion("name not in", values, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameBetween(String value1, String value2) {
      addCriterion("name between", value1, value2, "name");
      return (Criteria) this;
    }
 
    public Criteria andNameNotBetween(String value1, String value2) {
      addCriterion("name not between", value1, value2, "name");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharIsNull() {
      addCriterion("first_char is null");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharIsNotNull() {
      addCriterion("first_char is not null");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharEqualTo(String value) {
      addCriterion("first_char =", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharNotEqualTo(String value) {
      addCriterion("first_char <>", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharGreaterThan(String value) {
      addCriterion("first_char >", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharGreaterThanOrEqualTo(String value) {
      addCriterion("first_char >=", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharLessThan(String value) {
      addCriterion("first_char <", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharLessThanOrEqualTo(String value) {
      addCriterion("first_char <=", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharLike(String value) {
      addCriterion("first_char like", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharNotLike(String value) {
      addCriterion("first_char not like", value, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharIn(List values) {
      addCriterion("first_char in", values, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharNotIn(List values) {
      addCriterion("first_char not in", values, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharBetween(String value1, String value2) {
      addCriterion("first_char between", value1, value2, "firstChar");
      return (Criteria) this;
    }
 
    public Criteria andFirstCharNotBetween(String value1, String value2) {
      addCriterion("first_char not between", value1, value2, "firstChar");
      return (Criteria) this;
    }
  }
 
  public static class Criteria extends GeneratedCriteria {
 
    protected Criteria() {
      super();
    }
  }
 
  public static class Criterion {
    private String condition;
 
    private Object value;
 
    private Object secondValue;
 
    private boolean noValue;
 
    private boolean singleValue;
 
    private boolean betweenValue;
 
    private boolean listValue;
 
    private String typeHandler;
 
    public String getCondition() {
      return condition;
    }
 
    public Object getValue() {
      return value;
    }
 
    public Object getSecondValue() {
      return secondValue;
    }
 
    public boolean isNoValue() {
      return noValue;
    }
 
    public boolean isSingleValue() {
      return singleValue;
    }
 
    public boolean isBetweenValue() {
      return betweenValue;
    }
 
    public boolean isListValue() {
      return listValue;
    }
 
    public String getTypeHandler() {
      return typeHandler;
    }
 
    protected Criterion(String condition) {
      super();
      this.condition = condition;
      this.typeHandler = null;
      this.noValue = true;
    }
 
    protected Criterion(String condition, Object value, String typeHandler) {
      super();
      this.condition = condition;
      this.value = value;
      this.typeHandler = typeHandler;
      if (value instanceof List<?>) {
        this.listValue = true;
      } else {
        this.singleValue = true;
      }
    }
 
    protected Criterion(String condition, Object value) {
      this(condition, value, null);
    }
 
    protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
      super();
      this.condition = condition;
      this.value = value;
      this.secondValue = secondValue;
      this.typeHandler = typeHandler;
      this.betweenValue = true;
    }
 
    protected Criterion(String condition, Object value, Object secondValue) {
      this(condition, value, secondValue, null);
    }
  }
}

创建一个Result返回通知类

package com.itcode.pojo;
 
import java.io.Serializable;
 
 
public class Result implements Serializable {
  private boolean success; //判断该变量
  private String message; //返回的字符串
 
  public Result(boolean success, String message) {
    this.success = success;
    this.message = message;
  }
  //此处省略get/set方法,自行补全

第三步 创建ssm_dao子模块(逆向工程也可以生成)

创建TbBrandMapper接口类

package com.itcode.dao;
 
import com.itcode.pojo.TbBrand;
import com.itcode.pojo.TbBrandExample;
import org.apache.ibatis.annotations.Param;
 
import java.util.List;
import java.util.Map;
 
public interface TbBrandMapper {
  int countByExample(TbBrandExample example);
 
  int deleteByExample(TbBrandExample example);
 
  int deleteByPrimaryKey(Long id);
 
  int insert(TbBrand record);
 
  int insertSelective(TbBrand record);
 
  List selectByExample(TbBrandExample example);
 
  TbBrand selectByPrimaryKey(Long id);
 
  int updateByExampleSelective(@Param("record") TbBrand record, @Param("example") TbBrandExample example);
 
  int updateByExample(@Param("record") TbBrand record, @Param("example") TbBrandExample example);
 
  int updateByPrimaryKeySelective(TbBrand record);
 
  int updateByPrimaryKey(TbBrand record);
 
  List selectOptionList();
}

创建TbBrandMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>


 
  
  
  
 
 
  
   
    
     
      
       
        
         and ${criterion.condition}
        
        
         and ${criterion.condition} #{criterion.value}
        
        
         and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
        
        
         and ${criterion.condition}
         
          #{listItem}
         
        
       
      
     
    
   
  
 
 
  
   
    
     
      
       
        
         and ${criterion.condition}
        
        
         and ${criterion.condition} #{criterion.value}
        
        
         and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
        
        
         and ${criterion.condition}
         
          #{listItem}
         
        
       
      
     
    
   
  
 
 
  id, name, first_char
 
 
 
 
  delete from tb_brand
  where id = #{id,jdbcType=BIGINT}
 
 
  delete from tb_brand
  
   
  
 
 
  insert into tb_brand (id, name, first_char
   )
  values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{firstChar,jdbcType=VARCHAR}
   )
 
 
  insert into tb_brand
  
   
    id,
   
   
    name,
   
   
    first_char,
   
  
  
   
    #{id,jdbcType=BIGINT},
   
   
    #{name,jdbcType=VARCHAR},
   
   
    #{firstChar,jdbcType=VARCHAR},
   
  
 
 
 
  update tb_brand
  
   
    id = #{record.id,jdbcType=BIGINT},
   
   
    name = #{record.name,jdbcType=VARCHAR},
   
   
    first_char = #{record.firstChar,jdbcType=VARCHAR},
   
  
  
   
  
 
 
  update tb_brand
  set id = #{record.id,jdbcType=BIGINT},
   name = #{record.name,jdbcType=VARCHAR},
   first_char = #{record.firstChar,jdbcType=VARCHAR}
  
   
  
 
 
  update tb_brand
  
   
    name = #{name,jdbcType=VARCHAR},
   
   
    first_char = #{firstChar,jdbcType=VARCHAR},
   
  
  where id = #{id,jdbcType=BIGINT}
 
 
  update tb_brand
  set name = #{name,jdbcType=VARCHAR},
   first_char = #{firstChar,jdbcType=VARCHAR}
  where id = #{id,jdbcType=BIGINT}
 
 
 

第四步 创建ssm_service子模块

创建一个service接口

package com.itcode.service;
 
import com.itcode.pojo.TbBrand;
 
import java.util.List;
 
public interface BrandService {
  List findAll();
 
  void insert(TbBrand brand);
}

创建一个service的实现类

package com.itcode.service.impl;
 
import com.itcode.dao.TbBrandMapper;
import com.itcode.pojo.TbBrand;
import com.itcode.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
@Transactional
public class BrandServiceImpl implements BrandService {
  @Autowired
  private TbBrandMapper tbBrandDao;
  @Override
  public List findAll() {
    return tbBrandDao.selectByExample(null);
  }
 
  @Override
  public void insert(TbBrand brand) {
    tbBrandDao.insert(brand);
  }
}

jdbc.properties配置(在resource中配置)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf-8
jdbc.username=用户名
jdbc.password=密码

applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>

 
  
  
 
  
  
 
  
  
    
    
    
    
  
 
  
  
    
    
  
 
  
  
    
  
 
  
  
    
  
 
  
  
 
  
  
  

第五步 创建一个ssm_web子模块

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

 
  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
    
      forceEncoding
      true
    
  
  
    CharacterEncodingFilter
    /*
  
 
  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springMVC.xml
    
  
  
    dispatcherServlet
    *.do
  
 
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

springMVC.xml配置说明

<?xml version="1.0" encoding="UTF-8"?>

 
  
  
 
  
  
    
      
        
        
          
            WriteMapNullValue
            WriteDateUseDateFormat
          
        
      
    
  

创建一个BrandController类

package com.itcode.controller; 
import com.itcode.pojo.Result;
import com.itcode.pojo.TbBrand;
import com.itcode.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
@RequestMapping("/brand")
public class BrandController {
 
  @Autowired
  private BrandService brandService;
 
  @RequestMapping("/findAll")
  public List findAll(){
    return brandService.findAll();
  }
 
  @RequestMapping("/insert")
  public Result insert(@RequestBody TbBrand brand){
    try {
      brandService.insert(brand);
      return new Result(true, "新增成功");
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, "新增失败");
    }
  }
}

最后配置一下tomcat,启动tomcat可以了,如果想要测试我们的接口可以使用Postman进行测试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页名称:快速搭建SSM框架(Maven)五步曲的方法步骤
标题URL:http://chengdu.cdxwcx.cn/article/jeseig.html