最近有空所以把以前做項目的框架做個簡單的整合
1.創建Maven項目,導入依賴
junit junit 4.12 test javax.servlet javax.servlet-api 4.0.0 provided javax.servlet jstl 1.2 mysql mysql-connector-java 5.1.38 com.mchange c3p0 0.9.5.2 org.springframework spring-webmvc 5.0.8.RELEASE org.springframework spring-jdbc 5.0.8.RELEASE org.mybatis mybatis 3.4.6 org.mybatis mybatis-spring 1.3.2 redis.clients jedis 2.9.0 com.alibaba fastjson 1.2.7 ssm_redis src/main/java **/*.xml maven-clean-plugin 3.0.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.7.0 maven-surefire-plugin 2.20.1 maven-war-plugin 3.2.0 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
2.根據個人習慣創建包
3.在resources中創建db.properties
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/Bloggingplatform?characterEncoding=utf-8&allowMultiQueries=true db.user=root db.password=@838061522@ly #定義初始連接數 initPoolSize=3 #定義最大連接數 maxPoolSizes=30
4.在resources中創建spring.xml
5.在pojo中創建實體類
public class Article implements Serializable{ private static final long serialVersionUID = 1L; private String aId; //文章id private String aTitle; //標題 private String aContent; //內容 private String aTitleImg; //標題圖片 private String aTime; //時間 private Integer aReadingQuantity; //閱讀量 private Integer aCommentnum; //評論數 private Programa programa;//欄目id 外鍵 private String label;//標籤 private Administrator administrator;//管理員id 外鍵 private Integer aStatusBar; //狀態 0不公開 1公開 private String aAbstract;//摘要 //此處省略setter和getter }
6.創建dao接口 ArticleDao
public interface ArticleDao { /* * @Author Uncle Liu * @Description //TODO 根據id查詢Article * @Date 18:36 2018/10/30 **/ Article getArticleById(String aId); }
7.創建service接口及實現類
7.1 創建ArticleService
public interface ArticleService { /* * @Author Uncle Liu * @Description //TODO 根據id查詢Article * @Date 18:36 2018/10/30 **/ Article getArticleById(String aId); }
7.2 創建ArticleImpl
@Service("articleImpl") public class ArticleImpl implements ArticleService { @Override public Article getArticleById(String aId) { return articleDao.getArticleById(aId); } }
8.在mappers中創建ArticleMapper.xml
9.配置web.xml
Archetype Created Web Application index.jsp contextConfigLocation classpath:spring.xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encodingFilter /* SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring.xml 1 SpringMVC *.do org.springframework.web.context.ContextLoaderListener
10.創建Controller
@Controller @RequestMapping("/index") public class indexController { @Resource private ArticleService articleService; @RequestMapping("/a") @ResponseBody public String index(){ //根據id查詢 Article article = articleService.getArticleById("91cf33d3-2b96-45e8-a66d-ffd7fabb8de8"); System.out.println(article); return "Hello"; } }
11.創建index.jsp
Title 你好
12.測試
12.1 運行結果:
12.2 控制檯輸出結果:
12.3 頁面顯示