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

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

第一步 创建一个父工程:

导入父工程的pom坐标:

<dependencies>
 <!--spring核心-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
 </dependency>

 <!--spring-webmvc-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.0.2.RELEASE</version>
 </dependency>

 <!--spring-jdbc支持、spring-tx事务支持、aspectj 支持-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.0.2.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.0.2.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.7</version>
 </dependency>

 <!--spring-test-->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>5.0.2.RELEASE</version>
 </dependency>

 <!--mybatis支持包-->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.5</version>
 </dependency>

 <!-- mybatis-spring整合包-->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.1</version>
 </dependency>

 <!--数据库驱动包、连接池-->
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.30</version>
 </dependency>
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.10</version>
 </dependency>

 <!-- fastJSON -->
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.56</version>
 </dependency>

 <!-- jstl 支持包-->
 <dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
 </dependency>

 <!--log4j、junit测试支持-->
 <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
 </dependency>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
 </dependency>

</dependencies>

第二步 创建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<Criteria> oredCriteria;

  public TbBrandExample() {
    oredCriteria = new ArrayList<Criteria>();
  }

  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<Criteria> 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<Criterion> criteria;

    protected GeneratedCriteria() {
      super();
      criteria = new ArrayList<Criterion>();
    }

    public boolean isValid() {
      return criteria.size() > 0;
    }

    public List<Criterion> getAllCriteria() {
      return criteria;
    }

    public List<Criterion> 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<Long> values) {
      addCriterion("id in", values, "id");
      return (Criteria) this;
    }

    public Criteria andIdNotIn(List<Long> 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<String> values) {
      addCriterion("name in", values, "name");
      return (Criteria) this;
    }

    public Criteria andNameNotIn(List<String> 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<String> values) {
      addCriterion("first_char in", values, "firstChar");
      return (Criteria) this;
    }

    public Criteria andFirstCharNotIn(List<String> 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<TbBrand> 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<Map> selectOptionList();
}

创建TbBrandMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.itcode.dao.TbBrandMapper" >
 <resultMap id="BaseResultMap" type="com.itcode.pojo.TbBrand" >
  <id column="id" property="id" jdbcType="BIGINT" />
  <result column="name" property="name" jdbcType="VARCHAR" />
  <result column="first_char" property="firstChar" jdbcType="VARCHAR" />
 </resultMap>
 <sql id="Example_Where_Clause" >
  <where >
   <foreach collection="oredCriteria" item="criteria" separator="or" >
    <if test="criteria.valid" >
     <trim prefix="(" suffix=")" prefixOverrides="and" >
      <foreach collection="criteria.criteria" item="criterion" >
       <choose >
        <when test="criterion.noValue" >
         and ${criterion.condition}
        </when>
        <when test="criterion.singleValue" >
         and ${criterion.condition} #{criterion.value}
        </when>
        <when test="criterion.betweenValue" >
         and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
        </when>
        <when test="criterion.listValue" >
         and ${criterion.condition}
         <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
          #{listItem}
         </foreach>
        </when>
       </choose>
      </foreach>
     </trim>
    </if>
   </foreach>
  </where>
 </sql>
 <sql id="Update_By_Example_Where_Clause" >
  <where >
   <foreach collection="example.oredCriteria" item="criteria" separator="or" >
    <if test="criteria.valid" >
     <trim prefix="(" suffix=")" prefixOverrides="and" >
      <foreach collection="criteria.criteria" item="criterion" >
       <choose >
        <when test="criterion.noValue" >
         and ${criterion.condition}
        </when>
        <when test="criterion.singleValue" >
         and ${criterion.condition} #{criterion.value}
        </when>
        <when test="criterion.betweenValue" >
         and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
        </when>
        <when test="criterion.listValue" >
         and ${criterion.condition}
         <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
          #{listItem}
         </foreach>
        </when>
       </choose>
      </foreach>
     </trim>
    </if>
   </foreach>
  </where>
 </sql>
 <sql id="Base_Column_List" >
  id, name, first_char
 </sql>
 <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.itcode.pojo.TbBrandExample" >
  select
  <if test="distinct" >
   distinct
  </if>
  <include refid="Base_Column_List" />
  from tb_brand
  <if test="_parameter != null" >
   <include refid="Example_Where_Clause" />
  </if>
  <if test="orderByClause != null" >
   order by ${orderByClause}
  </if>
 </select>
 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
  select
  <include refid="Base_Column_List" />
  from tb_brand
  where id = #{id,jdbcType=BIGINT}
 </select>
 <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
  delete from tb_brand
  where id = #{id,jdbcType=BIGINT}
 </delete>
 <delete id="deleteByExample" parameterType="com.itcode.pojo.TbBrandExample" >
  delete from tb_brand
  <if test="_parameter != null" >
   <include refid="Example_Where_Clause" />
  </if>
 </delete>
 <insert id="insert" parameterType="com.itcode.pojo.TbBrand" >
  insert into tb_brand (id, name, first_char
   )
  values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{firstChar,jdbcType=VARCHAR}
   )
 </insert>
 <insert id="insertSelective" parameterType="com.itcode.pojo.TbBrand" >
  insert into tb_brand
  <trim prefix="(" suffix=")" suffixOverrides="," >
   <if test="id != null" >
    id,
   </if>
   <if test="name != null" >
    name,
   </if>
   <if test="firstChar != null" >
    first_char,
   </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides="," >
   <if test="id != null" >
    #{id,jdbcType=BIGINT},
   </if>
   <if test="name != null" >
    #{name,jdbcType=VARCHAR},
   </if>
   <if test="firstChar != null" >
    #{firstChar,jdbcType=VARCHAR},
   </if>
  </trim>
 </insert>
 <select id="countByExample" parameterType="com.itcode.pojo.TbBrandExample" resultType="java.lang.Integer" >
  select count(*) from tb_brand
  <if test="_parameter != null" >
   <include refid="Example_Where_Clause" />
  </if>
 </select>
 <update id="updateByExampleSelective" parameterType="map" >
  update tb_brand
  <set >
   <if test="record.id != null" >
    id = #{record.id,jdbcType=BIGINT},
   </if>
   <if test="record.name != null" >
    name = #{record.name,jdbcType=VARCHAR},
   </if>
   <if test="record.firstChar != null" >
    first_char = #{record.firstChar,jdbcType=VARCHAR},
   </if>
  </set>
  <if test="_parameter != null" >
   <include refid="Update_By_Example_Where_Clause" />
  </if>
 </update>
 <update id="updateByExample" parameterType="map" >
  update tb_brand
  set id = #{record.id,jdbcType=BIGINT},
   name = #{record.name,jdbcType=VARCHAR},
   first_char = #{record.firstChar,jdbcType=VARCHAR}
  <if test="_parameter != null" >
   <include refid="Update_By_Example_Where_Clause" />
  </if>
 </update>
 <update id="updateByPrimaryKeySelective" parameterType="com.itcode.pojo.TbBrand" >
  update tb_brand
  <set >
   <if test="name != null" >
    name = #{name,jdbcType=VARCHAR},
   </if>
   <if test="firstChar != null" >
    first_char = #{firstChar,jdbcType=VARCHAR},
   </if>
  </set>
  where id = #{id,jdbcType=BIGINT}
 </update>
 <update id="updateByPrimaryKey" parameterType="com.itcode.pojo.TbBrand" >
  update tb_brand
  set name = #{name,jdbcType=VARCHAR},
   first_char = #{firstChar,jdbcType=VARCHAR}
  where id = #{id,jdbcType=BIGINT}
 </update>
 <select id="selectOptionList" resultType="java.util.Map">
   select id,name as text from tb_brand
 </select>

</mapper>

第四步 创建ssm_service子模块

创建一个service接口

package com.itcode.service;

import com.itcode.pojo.TbBrand;

import java.util.List;

public interface BrandService {
  List<TbBrand> 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<TbBrand> 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"?>
<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:tx="http://www.springframework.org/schema/tx"
    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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!--1.注解扫描: 只扫描service包。-->
  <context:component-scan base-package="com.itcode.service"/>

  <!--2. 加载properties-->
  <context:property-placeholder location="classpath:jdbc.properties"/>

  <!--3. 创建连接池-->
  <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>

  <!--4. Spring整合Mybatis:把SqlSessionFactory对象的创建交给Spring完成-->
  <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--<property name="mapperLocations" value="classpath:com/itcode/dao/TbBrandMapper.xml"/>-->
  </bean>

  <!--5. 扫描dao的接口所在包,自动对该包下所有的dao接口自动生成代理对象-->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.itcode.dao"/>
  </bean>

  <!--6. 事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

  <!--7. 开启事务控制的注解支持 -->
  <tx:annotation-driven transaction-manager="transactionManager"/>

  <!--以下配置请忽略只是为了回顾用-->
  <!--此处是第二种方式 Spring声明式事务管理配置-->
  <!--6.1 事务管理器
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>
   6.2 事务通知规则
  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
      <tx:method name="*" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
  </tx:advice>
   6.3 Aop配置 = 切入点表达式 + 通知规则的引用
  <aop:config>
    <aop:pointcut id="pt" expression="execution(* com..*ServiceImpl.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
  </aop:config>  -->
</beans>

第五步 创建一个ssm_web子模块

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

  <!-- 解决post乱码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--SpringMVC前端控制器-->
  <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:springMVC.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!--配置监听器,加载applicationContext.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
</web-app>

springMVC.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">

  <!--1. 注解扫描: 之扫描controller包下的注解-->
  <context:component-scan base-package="com.itcode.controller"/>

  <!--2. 注解驱动,controller返回值支持json-->
  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json"/>
        <property name="features">
          <array>
            <value>WriteMapNullValue</value>
            <value>WriteDateUseDateFormat</value>
          </array>
        </property>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>
</beans>

创建一个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<TbBrand> 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进行测试。

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

(0)

相关推荐

  • 基于maven的ssm框架整合的示例代码

    基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用

  • Maven+SSM框架实现简单的增删改查

    Spring介绍: spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情.然而, Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java 应用都可以从 Spring 中受益. 简单来说, Spring 是一个轻量级的控制反转(IoC )和面向切面( AOP )的容器框架. SpringMVC介绍 Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在Spring Web Flow 里面. Spring

  • 使用eclipse + maven一步步搭建SSM框架教程详解

    SSM (SSM 框架集) SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. SpringMVC分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它们更容易进行定制. MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架. 0.系统环境 1)Windows

  • Maven+oracle+SSM搭建简单项目的方法

    简单谈一下maven搭建 ssm 项目 (使用数据库oracle,比 mysql 麻烦一点,所以这里谈一下) 在创建maven 的web项目时,常常会缺了main/java , main/test 两个文件夹. 解决方法: ① : 在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre就可以了. (推荐使用这种) ② :手动创建 目录.切换视图采用Navigato

  • WebSocket整合SSM(Spring,Struts2,Maven)的实现示例

    一.WebSocket与HTTP长轮询 WebSocket属于HTML5 规范的一部分,提供的一种在单个 TCP 连接上进行全双工通讯的协议.允许服务端主动向客户端推送数据.在 WebSocket API 中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输.(Tomcat 8以上版本支持) HTTP 协议是一种无状态的.无连接的.单向的应用层协议.它采用了请求/响应模型.通信请求只能由客户端发起,服务端对请求做出应答处理.这种通信模型有一个弊端:HTTP

  • 使用IDEA配置Maven搭建开发框架ssm教程

    一.配置Maven环境 1.下载Maven 下载链接http://maven.apache.org/download.cgi 2.下载完成解压压缩包并创建本地仓库文件夹 3.打开解压缩文件,配置本地仓库路径 4.配置Maven环境变量 5.在cmd中查看maven是否配置正确 在cmd中输入mvn -v命令查看 二.在IntelliJ IDEA中配置Maven 打开-File-Settings 三.新建maven JAVAWEB项目 1.打开-File-New-Project Next Next

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

    项目完整搭建链接:https://gitee.com/DaNanHai04/ssm_parent.git 第一步 创建一个父工程: 导入父工程的pom坐标: <dependencies> <!--spring核心--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version

  • 通过IEAD+Maven快速搭建SSM项目的过程(Spring + Spring MVC + Mybatis)

    项目效果预览: 通过访问控制层的test/dologin方法进入index.jsp,展示jsp中的"hello,ssm"内容 一.新建项目: 1.打开IDEA,新建一个Maven项目 2.点击next,新建一个为ssmDemo的Maven项目(自己定义项目GroupId和ArtifactId) 3.配置本地Maven路径 4.点击Next,确认项目信息,配置项目名称和项目路径,点击Finish按钮即可创建项目 5.项目创建完成,目录结构如下: 注:如项目目录中未包含src,则项目还在初

  • 使用IntelliJ IDEA搭建SSM框架的图文教程

    1.使用IDEA新建项目 2.选择创建Maven工程 3.填写GroupId和ArtifactId 4.填写项目名称,与上一步的ArtifactId一致即可,然后点Finish 5.刚建好的目录只是一个Maven的目录结构,如下 6.完善目录结构,添加webapp.WEB-INF目录,以及web.xml文件 7.修改IDEA的Maven设置,Maven默认的本地仓库会保存在C盘,为了方便以后使用,尽量修改本地仓库的位置,因为这是框架整合,所以具体的修改方式在这里不多做说明.快捷键ctrl+alt

  • 使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)

    1 框架组成 Spring SpringMVC MyBatis 2 所需工具 Mysql 8.0.15 ​数据库管理系统,创建数据库 Tomcat 8.5.51 ​用于部署web项目 Maven 3.6.1 ​项目构建.项目依赖管理 lombok 1.18.10(可用可不用工具) ​用于类注解创建setter.getter.无参构造.全参构造.toString等函数 ​注:只导入依赖,不安装插件是不起作用的 3 搭建步骤 3.1 新建一个空Maven项目,填写项目相关信息,完成 3.2 添加we

  • 使用IDEA搭建ssm框架的详细图文教程

    ssm(spring springMVC mybatis) 1.创建项目 file->new->project 2.新建的maven项目目录结构 添加ssm需要的文件夹等 如果去掉java文件夹的蓝色标志,会发现这里new时不能创建java类或包 如果main/java前不是蓝色文件夹或test/java前不是绿色文件夹,可以这样添加 3.加入maven依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?

  • Win10 系统下快速搭建mxnet框架cpu版本

    Win10 系统下快速搭建mxnet框架cpu版本 一:安装Anaconda 1. 从官方网站下载 https://www.anaconda.com/download/ 建议下载python 3.7版本的Anaconda 2. 安装完成Anaconda后进行环境变量的测试(全程在cmd中完成) (1)检测anaconda环境是否安装成功: 运行cmd conda --version (2)安装一个内置的python版本解析器 conda search --full -name python #查

  • 教你快速搭建sona服务及idea使用sona的方法

    目录 Sonar概述 一. 搭建sona服务 二.idea配置 三. 配置maven的setting.xml文件 四.idea中 mvn sonar:sonar 执行命令 五 访问sona查看问题 六.汉化 Sonar概述 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)

  • SSM框架整合之Spring+SpringMVC+MyBatis实践步骤

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由RodJohnson在其著作ExpertOne-On-OneJ2EEDevelopmentandDesign中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从Spr

  • maven配置本地仓库的方法步骤

    目录 1.下载apache-maven-3.6.3-bin.zip 2.配置环境变量 3.测试 4.配置本地仓库 5.输入命令 本文主要介绍了maven配置本地仓库,分享给大家,具体如下: 官网http://maven.apache.org/download.cgi 1.下载apache-maven-3.6.3-bin.zip 然后解压放在本地盘(我放在了C盘,重命名为maven).然后再新建一个文件夹,命名为:maven-repository,作为本地仓库. 2.配置环境变量 在系统属性-环境

随机推荐