经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Maven » 查看文章
快速搭建SSM框架(Maven)五步曲的方法步骤
来源:jb51  时间:2019/10/9 8:35:25  对本文有异议

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

第一步 创建一个父工程:

导入父工程的pom坐标:

  1. <dependencies>
  2. <!--spring核心-->
  3. <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-context</artifactId>
  6. <version>5.0.2.RELEASE</version>
  7. </dependency>
  8. <!--spring-webmvc-->
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-webmvc</artifactId>
  12. <version>5.0.2.RELEASE</version>
  13. </dependency>
  14. <!--spring-jdbc支持、spring-tx事务支持、aspectj 支持-->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-jdbc</artifactId>
  18. <version>5.0.2.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-tx</artifactId>
  23. <version>5.0.2.RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.aspectj</groupId>
  27. <artifactId>aspectjweaver</artifactId>
  28. <version>1.8.7</version>
  29. </dependency>
  30. <!--spring-test-->
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-test</artifactId>
  34. <version>5.0.2.RELEASE</version>
  35. </dependency>
  36. <!--mybatis支持包-->
  37. <dependency>
  38. <groupId>org.mybatis</groupId>
  39. <artifactId>mybatis</artifactId>
  40. <version>3.4.5</version>
  41. </dependency>
  42. <!-- mybatis-spring整合包-->
  43. <dependency>
  44. <groupId>org.mybatis</groupId>
  45. <artifactId>mybatis-spring</artifactId>
  46. <version>1.3.1</version>
  47. </dependency>
  48. <!--数据库驱动包、连接池-->
  49. <dependency>
  50. <groupId>mysql</groupId>
  51. <artifactId>mysql-connector-java</artifactId>
  52. <version>5.1.30</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.alibaba</groupId>
  56. <artifactId>druid</artifactId>
  57. <version>1.1.10</version>
  58. </dependency>
  59. <!-- fastJSON -->
  60. <dependency>
  61. <groupId>com.alibaba</groupId>
  62. <artifactId>fastjson</artifactId>
  63. <version>1.2.56</version>
  64. </dependency>
  65. <!-- jstl 支持包-->
  66. <dependency>
  67. <groupId>jstl</groupId>
  68. <artifactId>jstl</artifactId>
  69. <version>1.2</version>
  70. </dependency>
  71. <!--log4j、junit测试支持-->
  72. <dependency>
  73. <groupId>log4j</groupId>
  74. <artifactId>log4j</artifactId>
  75. <version>1.2.17</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>junit</groupId>
  79. <artifactId>junit</artifactId>
  80. <version>4.12</version>
  81. </dependency>
  82. </dependencies>

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

创建实体类TbBrand实体类

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

创建TbBrandExample条件类 

  1. package com.itcode.pojo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class TbBrandExample {
  5. protected String orderByClause;
  6. protected boolean distinct;
  7. protected List<Criteria> oredCriteria;
  8. public TbBrandExample() {
  9. oredCriteria = new ArrayList<Criteria>();
  10. }
  11. public void setOrderByClause(String orderByClause) {
  12. this.orderByClause = orderByClause;
  13. }
  14. public String getOrderByClause() {
  15. return orderByClause;
  16. }
  17. public void setDistinct(boolean distinct) {
  18. this.distinct = distinct;
  19. }
  20. public boolean isDistinct() {
  21. return distinct;
  22. }
  23. public List<Criteria> getOredCriteria() {
  24. return oredCriteria;
  25. }
  26. public void or(Criteria criteria) {
  27. oredCriteria.add(criteria);
  28. }
  29. public Criteria or() {
  30. Criteria criteria = createCriteriaInternal();
  31. oredCriteria.add(criteria);
  32. return criteria;
  33. }
  34. public Criteria createCriteria() {
  35. Criteria criteria = createCriteriaInternal();
  36. if (oredCriteria.size() == 0) {
  37. oredCriteria.add(criteria);
  38. }
  39. return criteria;
  40. }
  41. protected Criteria createCriteriaInternal() {
  42. Criteria criteria = new Criteria();
  43. return criteria;
  44. }
  45. public void clear() {
  46. oredCriteria.clear();
  47. orderByClause = null;
  48. distinct = false;
  49. }
  50. protected abstract static class GeneratedCriteria {
  51. protected List<Criterion> criteria;
  52. protected GeneratedCriteria() {
  53. super();
  54. criteria = new ArrayList<Criterion>();
  55. }
  56. public boolean isValid() {
  57. return criteria.size() > 0;
  58. }
  59. public List<Criterion> getAllCriteria() {
  60. return criteria;
  61. }
  62. public List<Criterion> getCriteria() {
  63. return criteria;
  64. }
  65. protected void addCriterion(String condition) {
  66. if (condition == null) {
  67. throw new RuntimeException("Value for condition cannot be null");
  68. }
  69. criteria.add(new Criterion(condition));
  70. }
  71. protected void addCriterion(String condition, Object value, String property) {
  72. if (value == null) {
  73. throw new RuntimeException("Value for " + property + " cannot be null");
  74. }
  75. criteria.add(new Criterion(condition, value));
  76. }
  77. protected void addCriterion(String condition, Object value1, Object value2, String property) {
  78. if (value1 == null || value2 == null) {
  79. throw new RuntimeException("Between values for " + property + " cannot be null");
  80. }
  81. criteria.add(new Criterion(condition, value1, value2));
  82. }
  83. public Criteria andIdIsNull() {
  84. addCriterion("id is null");
  85. return (Criteria) this;
  86. }
  87. public Criteria andIdIsNotNull() {
  88. addCriterion("id is not null");
  89. return (Criteria) this;
  90. }
  91. public Criteria andIdEqualTo(Long value) {
  92. addCriterion("id =", value, "id");
  93. return (Criteria) this;
  94. }
  95. public Criteria andIdNotEqualTo(Long value) {
  96. addCriterion("id <>", value, "id");
  97. return (Criteria) this;
  98. }
  99. public Criteria andIdGreaterThan(Long value) {
  100. addCriterion("id >", value, "id");
  101. return (Criteria) this;
  102. }
  103. public Criteria andIdGreaterThanOrEqualTo(Long value) {
  104. addCriterion("id >=", value, "id");
  105. return (Criteria) this;
  106. }
  107. public Criteria andIdLessThan(Long value) {
  108. addCriterion("id <", value, "id");
  109. return (Criteria) this;
  110. }
  111. public Criteria andIdLessThanOrEqualTo(Long value) {
  112. addCriterion("id <=", value, "id");
  113. return (Criteria) this;
  114. }
  115. public Criteria andIdIn(List<Long> values) {
  116. addCriterion("id in", values, "id");
  117. return (Criteria) this;
  118. }
  119. public Criteria andIdNotIn(List<Long> values) {
  120. addCriterion("id not in", values, "id");
  121. return (Criteria) this;
  122. }
  123. public Criteria andIdBetween(Long value1, Long value2) {
  124. addCriterion("id between", value1, value2, "id");
  125. return (Criteria) this;
  126. }
  127. public Criteria andIdNotBetween(Long value1, Long value2) {
  128. addCriterion("id not between", value1, value2, "id");
  129. return (Criteria) this;
  130. }
  131. public Criteria andNameIsNull() {
  132. addCriterion("name is null");
  133. return (Criteria) this;
  134. }
  135. public Criteria andNameIsNotNull() {
  136. addCriterion("name is not null");
  137. return (Criteria) this;
  138. }
  139. public Criteria andNameEqualTo(String value) {
  140. addCriterion("name =", value, "name");
  141. return (Criteria) this;
  142. }
  143. public Criteria andNameNotEqualTo(String value) {
  144. addCriterion("name <>", value, "name");
  145. return (Criteria) this;
  146. }
  147. public Criteria andNameGreaterThan(String value) {
  148. addCriterion("name >", value, "name");
  149. return (Criteria) this;
  150. }
  151. public Criteria andNameGreaterThanOrEqualTo(String value) {
  152. addCriterion("name >=", value, "name");
  153. return (Criteria) this;
  154. }
  155. public Criteria andNameLessThan(String value) {
  156. addCriterion("name <", value, "name");
  157. return (Criteria) this;
  158. }
  159. public Criteria andNameLessThanOrEqualTo(String value) {
  160. addCriterion("name <=", value, "name");
  161. return (Criteria) this;
  162. }
  163. public Criteria andNameLike(String value) {
  164. addCriterion("name like", value, "name");
  165. return (Criteria) this;
  166. }
  167. public Criteria andNameNotLike(String value) {
  168. addCriterion("name not like", value, "name");
  169. return (Criteria) this;
  170. }
  171. public Criteria andNameIn(List<String> values) {
  172. addCriterion("name in", values, "name");
  173. return (Criteria) this;
  174. }
  175. public Criteria andNameNotIn(List<String> values) {
  176. addCriterion("name not in", values, "name");
  177. return (Criteria) this;
  178. }
  179. public Criteria andNameBetween(String value1, String value2) {
  180. addCriterion("name between", value1, value2, "name");
  181. return (Criteria) this;
  182. }
  183. public Criteria andNameNotBetween(String value1, String value2) {
  184. addCriterion("name not between", value1, value2, "name");
  185. return (Criteria) this;
  186. }
  187. public Criteria andFirstCharIsNull() {
  188. addCriterion("first_char is null");
  189. return (Criteria) this;
  190. }
  191. public Criteria andFirstCharIsNotNull() {
  192. addCriterion("first_char is not null");
  193. return (Criteria) this;
  194. }
  195. public Criteria andFirstCharEqualTo(String value) {
  196. addCriterion("first_char =", value, "firstChar");
  197. return (Criteria) this;
  198. }
  199. public Criteria andFirstCharNotEqualTo(String value) {
  200. addCriterion("first_char <>", value, "firstChar");
  201. return (Criteria) this;
  202. }
  203. public Criteria andFirstCharGreaterThan(String value) {
  204. addCriterion("first_char >", value, "firstChar");
  205. return (Criteria) this;
  206. }
  207. public Criteria andFirstCharGreaterThanOrEqualTo(String value) {
  208. addCriterion("first_char >=", value, "firstChar");
  209. return (Criteria) this;
  210. }
  211. public Criteria andFirstCharLessThan(String value) {
  212. addCriterion("first_char <", value, "firstChar");
  213. return (Criteria) this;
  214. }
  215. public Criteria andFirstCharLessThanOrEqualTo(String value) {
  216. addCriterion("first_char <=", value, "firstChar");
  217. return (Criteria) this;
  218. }
  219. public Criteria andFirstCharLike(String value) {
  220. addCriterion("first_char like", value, "firstChar");
  221. return (Criteria) this;
  222. }
  223. public Criteria andFirstCharNotLike(String value) {
  224. addCriterion("first_char not like", value, "firstChar");
  225. return (Criteria) this;
  226. }
  227. public Criteria andFirstCharIn(List<String> values) {
  228. addCriterion("first_char in", values, "firstChar");
  229. return (Criteria) this;
  230. }
  231. public Criteria andFirstCharNotIn(List<String> values) {
  232. addCriterion("first_char not in", values, "firstChar");
  233. return (Criteria) this;
  234. }
  235. public Criteria andFirstCharBetween(String value1, String value2) {
  236. addCriterion("first_char between", value1, value2, "firstChar");
  237. return (Criteria) this;
  238. }
  239. public Criteria andFirstCharNotBetween(String value1, String value2) {
  240. addCriterion("first_char not between", value1, value2, "firstChar");
  241. return (Criteria) this;
  242. }
  243. }
  244. public static class Criteria extends GeneratedCriteria {
  245. protected Criteria() {
  246. super();
  247. }
  248. }
  249. public static class Criterion {
  250. private String condition;
  251. private Object value;
  252. private Object secondValue;
  253. private boolean noValue;
  254. private boolean singleValue;
  255. private boolean betweenValue;
  256. private boolean listValue;
  257. private String typeHandler;
  258. public String getCondition() {
  259. return condition;
  260. }
  261. public Object getValue() {
  262. return value;
  263. }
  264. public Object getSecondValue() {
  265. return secondValue;
  266. }
  267. public boolean isNoValue() {
  268. return noValue;
  269. }
  270. public boolean isSingleValue() {
  271. return singleValue;
  272. }
  273. public boolean isBetweenValue() {
  274. return betweenValue;
  275. }
  276. public boolean isListValue() {
  277. return listValue;
  278. }
  279. public String getTypeHandler() {
  280. return typeHandler;
  281. }
  282. protected Criterion(String condition) {
  283. super();
  284. this.condition = condition;
  285. this.typeHandler = null;
  286. this.noValue = true;
  287. }
  288. protected Criterion(String condition, Object value, String typeHandler) {
  289. super();
  290. this.condition = condition;
  291. this.value = value;
  292. this.typeHandler = typeHandler;
  293. if (value instanceof List<?>) {
  294. this.listValue = true;
  295. } else {
  296. this.singleValue = true;
  297. }
  298. }
  299. protected Criterion(String condition, Object value) {
  300. this(condition, value, null);
  301. }
  302. protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
  303. super();
  304. this.condition = condition;
  305. this.value = value;
  306. this.secondValue = secondValue;
  307. this.typeHandler = typeHandler;
  308. this.betweenValue = true;
  309. }
  310. protected Criterion(String condition, Object value, Object secondValue) {
  311. this(condition, value, secondValue, null);
  312. }
  313. }
  314. }

创建一个Result返回通知类

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

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

创建TbBrandMapper接口类

  1. package com.itcode.dao;
  2. import com.itcode.pojo.TbBrand;
  3. import com.itcode.pojo.TbBrandExample;
  4. import org.apache.ibatis.annotations.Param;
  5. import java.util.List;
  6. import java.util.Map;
  7. public interface TbBrandMapper {
  8. int countByExample(TbBrandExample example);
  9. int deleteByExample(TbBrandExample example);
  10. int deleteByPrimaryKey(Long id);
  11. int insert(TbBrand record);
  12. int insertSelective(TbBrand record);
  13. List<TbBrand> selectByExample(TbBrandExample example);
  14. TbBrand selectByPrimaryKey(Long id);
  15. int updateByExampleSelective(@Param("record") TbBrand record, @Param("example") TbBrandExample example);
  16. int updateByExample(@Param("record") TbBrand record, @Param("example") TbBrandExample example);
  17. int updateByPrimaryKeySelective(TbBrand record);
  18. int updateByPrimaryKey(TbBrand record);
  19. List<Map> selectOptionList();
  20. }

创建TbBrandMapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.itcode.dao.TbBrandMapper" >
  4. <resultMap id="BaseResultMap" type="com.itcode.pojo.TbBrand" >
  5. <id column="id" property="id" jdbcType="BIGINT" />
  6. <result column="name" property="name" jdbcType="VARCHAR" />
  7. <result column="first_char" property="firstChar" jdbcType="VARCHAR" />
  8. </resultMap>
  9. <sql id="Example_Where_Clause" >
  10. <where >
  11. <foreach collection="oredCriteria" item="criteria" separator="or" >
  12. <if test="criteria.valid" >
  13. <trim prefix="(" suffix=")" prefixOverrides="and" >
  14. <foreach collection="criteria.criteria" item="criterion" >
  15. <choose >
  16. <when test="criterion.noValue" >
  17. and ${criterion.condition}
  18. </when>
  19. <when test="criterion.singleValue" >
  20. and ${criterion.condition} #{criterion.value}
  21. </when>
  22. <when test="criterion.betweenValue" >
  23. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  24. </when>
  25. <when test="criterion.listValue" >
  26. and ${criterion.condition}
  27. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  28. #{listItem}
  29. </foreach>
  30. </when>
  31. </choose>
  32. </foreach>
  33. </trim>
  34. </if>
  35. </foreach>
  36. </where>
  37. </sql>
  38. <sql id="Update_By_Example_Where_Clause" >
  39. <where >
  40. <foreach collection="example.oredCriteria" item="criteria" separator="or" >
  41. <if test="criteria.valid" >
  42. <trim prefix="(" suffix=")" prefixOverrides="and" >
  43. <foreach collection="criteria.criteria" item="criterion" >
  44. <choose >
  45. <when test="criterion.noValue" >
  46. and ${criterion.condition}
  47. </when>
  48. <when test="criterion.singleValue" >
  49. and ${criterion.condition} #{criterion.value}
  50. </when>
  51. <when test="criterion.betweenValue" >
  52. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  53. </when>
  54. <when test="criterion.listValue" >
  55. and ${criterion.condition}
  56. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  57. #{listItem}
  58. </foreach>
  59. </when>
  60. </choose>
  61. </foreach>
  62. </trim>
  63. </if>
  64. </foreach>
  65. </where>
  66. </sql>
  67. <sql id="Base_Column_List" >
  68. id, name, first_char
  69. </sql>
  70. <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.itcode.pojo.TbBrandExample" >
  71. select
  72. <if test="distinct" >
  73. distinct
  74. </if>
  75. <include refid="Base_Column_List" />
  76. from tb_brand
  77. <if test="_parameter != null" >
  78. <include refid="Example_Where_Clause" />
  79. </if>
  80. <if test="orderByClause != null" >
  81. order by ${orderByClause}
  82. </if>
  83. </select>
  84. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
  85. select
  86. <include refid="Base_Column_List" />
  87. from tb_brand
  88. where id = #{id,jdbcType=BIGINT}
  89. </select>
  90. <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
  91. delete from tb_brand
  92. where id = #{id,jdbcType=BIGINT}
  93. </delete>
  94. <delete id="deleteByExample" parameterType="com.itcode.pojo.TbBrandExample" >
  95. delete from tb_brand
  96. <if test="_parameter != null" >
  97. <include refid="Example_Where_Clause" />
  98. </if>
  99. </delete>
  100. <insert id="insert" parameterType="com.itcode.pojo.TbBrand" >
  101. insert into tb_brand (id, name, first_char
  102. )
  103. values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{firstChar,jdbcType=VARCHAR}
  104. )
  105. </insert>
  106. <insert id="insertSelective" parameterType="com.itcode.pojo.TbBrand" >
  107. insert into tb_brand
  108. <trim prefix="(" suffix=")" suffixOverrides="," >
  109. <if test="id != null" >
  110. id,
  111. </if>
  112. <if test="name != null" >
  113. name,
  114. </if>
  115. <if test="firstChar != null" >
  116. first_char,
  117. </if>
  118. </trim>
  119. <trim prefix="values (" suffix=")" suffixOverrides="," >
  120. <if test="id != null" >
  121. #{id,jdbcType=BIGINT},
  122. </if>
  123. <if test="name != null" >
  124. #{name,jdbcType=VARCHAR},
  125. </if>
  126. <if test="firstChar != null" >
  127. #{firstChar,jdbcType=VARCHAR},
  128. </if>
  129. </trim>
  130. </insert>
  131. <select id="countByExample" parameterType="com.itcode.pojo.TbBrandExample" resultType="java.lang.Integer" >
  132. select count(*) from tb_brand
  133. <if test="_parameter != null" >
  134. <include refid="Example_Where_Clause" />
  135. </if>
  136. </select>
  137. <update id="updateByExampleSelective" parameterType="map" >
  138. update tb_brand
  139. <set >
  140. <if test="record.id != null" >
  141. id = #{record.id,jdbcType=BIGINT},
  142. </if>
  143. <if test="record.name != null" >
  144. name = #{record.name,jdbcType=VARCHAR},
  145. </if>
  146. <if test="record.firstChar != null" >
  147. first_char = #{record.firstChar,jdbcType=VARCHAR},
  148. </if>
  149. </set>
  150. <if test="_parameter != null" >
  151. <include refid="Update_By_Example_Where_Clause" />
  152. </if>
  153. </update>
  154. <update id="updateByExample" parameterType="map" >
  155. update tb_brand
  156. set id = #{record.id,jdbcType=BIGINT},
  157. name = #{record.name,jdbcType=VARCHAR},
  158. first_char = #{record.firstChar,jdbcType=VARCHAR}
  159. <if test="_parameter != null" >
  160. <include refid="Update_By_Example_Where_Clause" />
  161. </if>
  162. </update>
  163. <update id="updateByPrimaryKeySelective" parameterType="com.itcode.pojo.TbBrand" >
  164. update tb_brand
  165. <set >
  166. <if test="name != null" >
  167. name = #{name,jdbcType=VARCHAR},
  168. </if>
  169. <if test="firstChar != null" >
  170. first_char = #{firstChar,jdbcType=VARCHAR},
  171. </if>
  172. </set>
  173. where id = #{id,jdbcType=BIGINT}
  174. </update>
  175. <update id="updateByPrimaryKey" parameterType="com.itcode.pojo.TbBrand" >
  176. update tb_brand
  177. set name = #{name,jdbcType=VARCHAR},
  178. first_char = #{firstChar,jdbcType=VARCHAR}
  179. where id = #{id,jdbcType=BIGINT}
  180. </update>
  181. <select id="selectOptionList" resultType="java.util.Map">
  182. select id,name as text from tb_brand
  183. </select>
  184. </mapper>

第四步 创建ssm_service子模块

创建一个service接口

  1. package com.itcode.service;
  2. import com.itcode.pojo.TbBrand;
  3. import java.util.List;
  4. public interface BrandService {
  5. List<TbBrand> findAll();
  6. void insert(TbBrand brand);
  7. }

创建一个service的实现类

  1. package com.itcode.service.impl;
  2. import com.itcode.dao.TbBrandMapper;
  3. import com.itcode.pojo.TbBrand;
  4. import com.itcode.service.BrandService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import java.util.List;
  9. @Service
  10. @Transactional
  11. public class BrandServiceImpl implements BrandService {
  12. @Autowired
  13. private TbBrandMapper tbBrandDao;
  14. @Override
  15. public List<TbBrand> findAll() {
  16. return tbBrandDao.selectByExample(null);
  17. }
  18. @Override
  19. public void insert(TbBrand brand) {
  20. tbBrandDao.insert(brand);
  21. }
  22. }

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

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

applicationContext.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd">
  12. <!--1.注解扫描: 只扫描service包。-->
  13. <context:component-scan base-package="com.itcode.service"/>
  14. <!--2. 加载properties-->
  15. <context:property-placeholder location="classpath:jdbc.properties"/>
  16. <!--3. 创建连接池-->
  17. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  18. <property name="driverClassName" value="${jdbc.driver}"/>
  19. <property name="url" value="${jdbc.url}"/>
  20. <property name="username" value="${jdbc.username}"/>
  21. <property name="password" value="${jdbc.password}"/>
  22. </bean>
  23. <!--4. Spring整合Mybatis:把SqlSessionFactory对象的创建交给Spring完成-->
  24. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  25. <property name="dataSource" ref="dataSource"/>
  26. <!--<property name="mapperLocations" value="classpath:com/itcode/dao/TbBrandMapper.xml"/>-->
  27. </bean>
  28. <!--5. 扫描dao的接口所在包,自动对该包下所有的dao接口自动生成代理对象-->
  29. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  30. <property name="basePackage" value="com.itcode.dao"/>
  31. </bean>
  32. <!--6. 事务管理器 -->
  33. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  34. <property name="dataSource" ref="dataSource" />
  35. </bean>
  36. <!--7. 开启事务控制的注解支持 -->
  37. <tx:annotation-driven transaction-manager="transactionManager"/>
  38. <!--以下配置请忽略只是为了回顾用-->
  39. <!--此处是第二种方式 Spring声明式事务管理配置-->
  40. <!--6.1 事务管理器
  41. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  42. <property name="dataSource" ref="dataSource"/>
  43. </bean>
  44. 6.2 事务通知规则
  45. <tx:advice id="txAdvice" transaction-manager="txManager">
  46. <tx:attributes>
  47. <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
  48. <tx:method name="*" propagation="REQUIRED" read-only="false"/>
  49. </tx:attributes>
  50. </tx:advice>
  51. 6.3 Aop配置 = 切入点表达式 + 通知规则的引用
  52. <aop:config>
  53. <aop:pointcut id="pt" expression="execution(* com..*ServiceImpl.*(..))"/>
  54. <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
  55. </aop:config> -->
  56. </beans>

第五步 创建一个ssm_web子模块

web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6. version="2.5">
  7. <!-- 解决post乱码 -->
  8. <filter>
  9. <filter-name>CharacterEncodingFilter</filter-name>
  10. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  11. <init-param>
  12. <param-name>encoding</param-name>
  13. <param-value>utf-8</param-value>
  14. </init-param>
  15. <init-param>
  16. <param-name>forceEncoding</param-name>
  17. <param-value>true</param-value>
  18. </init-param>
  19. </filter>
  20. <filter-mapping>
  21. <filter-name>CharacterEncodingFilter</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. <!--SpringMVC前端控制器-->
  25. <servlet>
  26. <servlet-name>dispatcherServlet</servlet-name>
  27. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  28. <init-param>
  29. <param-name>contextConfigLocation</param-name>
  30. <param-value>classpath:springMVC.xml</param-value>
  31. </init-param>
  32. </servlet>
  33. <servlet-mapping>
  34. <servlet-name>dispatcherServlet</servlet-name>
  35. <url-pattern>*.do</url-pattern>
  36. </servlet-mapping>
  37. <!--配置监听器,加载applicationContext.xml配置文件-->
  38. <listener>
  39. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  40. </listener>
  41. <context-param>
  42. <param-name>contextConfigLocation</param-name>
  43. <param-value>classpath:applicationContext.xml</param-value>
  44. </context-param>
  45. </web-app>

springMVC.xml配置说明

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. 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">
  7. <!--1. 注解扫描: 之扫描controller包下的注解-->
  8. <context:component-scan base-package="com.itcode.controller"/>
  9. <!--2. 注解驱动,controller返回值支持json-->
  10. <mvc:annotation-driven>
  11. <mvc:message-converters register-defaults="true">
  12. <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  13. <property name="supportedMediaTypes" value="application/json"/>
  14. <property name="features">
  15. <array>
  16. <value>WriteMapNullValue</value>
  17. <value>WriteDateUseDateFormat</value>
  18. </array>
  19. </property>
  20. </bean>
  21. </mvc:message-converters>
  22. </mvc:annotation-driven>
  23. </beans>

创建一个BrandController类

  1. package com.itcode.controller;
  2. import com.itcode.pojo.Result;
  3. import com.itcode.pojo.TbBrand;
  4. import com.itcode.service.BrandService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import java.util.List;
  10. @RestController
  11. @RequestMapping("/brand")
  12. public class BrandController {
  13. @Autowired
  14. private BrandService brandService;
  15. @RequestMapping("/findAll")
  16. public List<TbBrand> findAll(){
  17. return brandService.findAll();
  18. }
  19. @RequestMapping("/insert")
  20. public Result insert(@RequestBody TbBrand brand){
  21. try {
  22. brandService.insert(brand);
  23. return new Result(true, "新增成功");
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. return new Result(false, "新增失败");
  27. }
  28. }
  29. }

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号