IDEA整合SSM框架實(shí)現(xiàn)網(wǎng)頁(yè)上顯示數(shù)據(jù)
我們來(lái)整合SSM框架
第一步、
創(chuàng)建一個(gè)maven工程。配置Tomcat,并測(cè)試是否正常訪問(wèn)HelloWorld.
這一步就省略了。
不懂得看這個(gè)博客:
創(chuàng)建出來(lái)是這樣的:
我們從這里開(kāi)始整合ssm。
第二步、
在pom.xml導(dǎo)入依賴,以下依賴是ssm常用的一些依賴,都導(dǎo)進(jìn)去,沒(méi)有壞處。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <jackson.version>2.9.0</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.3.RELEASE</version> </dependency> <!-- aop聯(lián)盟,提供了aop規(guī)范,Spring AOP就是實(shí)現(xiàn)了這個(gè)規(guī)范 --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!-- aspectj,實(shí)現(xiàn)了aop規(guī)范,比spring AOP實(shí)現(xiàn)的使用起來(lái)簡(jiǎn)單,所以引入它 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.8.0</version> </dependency> <!-- 集成aspectjweaver --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.3.RELEASE</version> </dependency> <!--spring集成測(cè)試 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.3.RELEASE</version> </dependency> <!-- mybatis --> <!-- mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng),以及使用的數(shù)據(jù)源 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- mybatis依賴,spring對(duì)jdbc的支持,spring集成mybatis以及對(duì)事務(wù)的支持 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> <!-- JSONObject的依賴 --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2-rev-1</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
第三步、
創(chuàng)建數(shù)據(jù)表。
創(chuàng)建一個(gè)名稱為category_的數(shù)據(jù)表,只有兩個(gè)字段,一個(gè)id, 一個(gè)name,id自增。隨便插入點(diǎn)數(shù)據(jù)。
DROP TABLE IF EXISTS `category_`; CREATE TABLE `category_` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `category_` VALUES ('1', 'sadf'); INSERT INTO `category_` VALUES ('2', 'safa'); INSERT INTO `category_` VALUES ('3', 'adfasdfas'); INSERT INTO `category_` VALUES ('4', '張阿道夫'); INSERT INTO `category_` VALUES ('5', '違法收費(fèi)'); INSERT INTO `category_` VALUES ('6', '發(fā)生的v'); INSERT INTO `category_` VALUES ('7', 'sdfsd'); INSERT INTO `category_` VALUES ('8', '34535');
第四步、
編寫(xiě)實(shí)體類;DAO層;Service層;Controller層。
像我這樣,在java文件夾目錄下,創(chuàng)建這幾個(gè)包。
然后在包下創(chuàng)建對(duì)應(yīng)的java文件或配置文件,最終的項(xiàng)目結(jié)構(gòu)是這樣的:
(那些打馬賽克的東西你們用不到,小孩子別看那些。)
接下來(lái)開(kāi)始寫(xiě)實(shí)體類:
實(shí)體類名稱最好見(jiàn)名知意,跟數(shù)據(jù)表名稱要對(duì)上。因?yàn)槭褂昧薼ombok插件,所以使用@Data注解,減少代碼量。
Category.java
package com.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Category { private int id; private String name; }
然后是DAO層,這里寫(xiě)的多是接口,只寫(xiě)方法,不實(shí)現(xiàn)。
CategoryDao.java
package com.dao; import com.entity.Category; import java.util.List; public interface CategoryDao { public int add(Category category); public void delete(int id); public Category get(int id); public int update(Category category); public List<Category> list(); }
然后在DAO層下創(chuàng)建對(duì)應(yīng)的Mapper.xml文件。
注意namespace要對(duì)上,還有id對(duì)應(yīng)上方法名。
CategoryMapper.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.dao.CategoryDao"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
然后是Service層,這里才是你要實(shí)現(xiàn)業(yè)務(wù)的地方。這里也是接口,在impl實(shí)現(xiàn)類里才是寫(xiě)真正實(shí)現(xiàn)的。
這里只實(shí)現(xiàn)一個(gè):查詢所有數(shù)據(jù)。
CategoryService.java
package com.service; import com.entity.Category; import java.util.List; public interface CategoryService { List<Category> list(); }
然后寫(xiě)實(shí)現(xiàn)類。
CategoryServiceImpl.java
package com.service.impl; import com.dao.CategoryDao; import com.entity.Category; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.service.CategoryService; import java.util.List; @Service public class CategoryServiceImpl implements CategoryService { @Autowired CategoryDao categoryDao; @Override public List<Category> list() { return categoryDao.list(); } }
然后寫(xiě)Controller層。
IndexController.java
package com.controller; import com.entity.Category; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.service.CategoryService; import java.util.ArrayList; import java.util.List; @Controller @RequestMapping("/") public class IndexController { @Autowired CategoryService categoryService; @RequestMapping("listCategory") public ModelAndView listCategory(){ ModelAndView mav = new ModelAndView(); List<Category> cs= categoryService.list(); // 放入轉(zhuǎn)發(fā)參數(shù) mav.addObject("cs", cs); // 放入jsp路徑 mav.setViewName("listCategory"); return mav; } }
OK,寫(xiě)到這里,你已經(jīng)成功一半啦!給自己鼓鼓掌!
接下來(lái),我們一鼓作氣,寫(xiě)好配置文件并訪問(wèn)數(shù)據(jù)吧!
第五步、
編寫(xiě)配置文件。
在resources目錄下,創(chuàng)建兩個(gè)配置文件。
注意把數(shù)據(jù)庫(kù),密碼,改成自己的,
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <context:component-scan base-package="com.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.entity" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/dao/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao"/> </bean> </beans>
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:annotation-config/> <context:component-scan base-package="com.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <mvc:default-servlet-handler /> <!-- 視圖定位 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
最后就是web.xml了。
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" xmlns:web="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"> <!-- spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc核心:分發(fā)servlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring mvc的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第六步、
創(chuàng)建listCategory.jsp文件。
在WEB-INF目錄下創(chuàng)建jsp文件夾,將jsp文件都放在這里,因?yàn)槲覀冊(cè)趕pringMVC.xml中配置過(guò)的,就不過(guò)多解釋了。
不知道在哪創(chuàng)建的可以回頭看一下我的那個(gè)項(xiàng)目路徑,根據(jù)那個(gè)來(lái)即可
listCategory.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/5/12 Time: 12:46 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${cs}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> </tr> </c:forEach> </table> </body> </html>
這里得到數(shù)據(jù)的思路我就不多說(shuō)了吧,懂得都懂。就是jsp頁(yè)面中通過(guò)jstl標(biāo)簽來(lái)獲取到Controller中返回的數(shù)據(jù)。
OK,到這里,大功告成!啟動(dòng)Tomcat,測(cè)試一下。
沒(méi)問(wèn)題吧,注意這里的訪問(wèn)路徑,http://localhost:8885/listCategory ,不是直接訪問(wèn)jsp頁(yè)面,是訪問(wèn)controller中的請(qǐng)求路徑。
ok,就到這吧,
這里有一個(gè)小小的改進(jìn),就是controller的改變,使用@ResponseBody注解來(lái)返回?cái)?shù)據(jù),然后前端通過(guò)ajax來(lái)異步請(qǐng)求數(shù)據(jù)。
ListController.java
package com.controller; import com.entity.Category; import com.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Controller @RequestMapping("/") public class ListController { @Autowired CategoryService categoryService; @RequestMapping("list/allCategory") @ResponseBody public List<Category> findAll(){ List<Category> list = categoryService.list(); System.out.println(list.size()); return list; } @RequestMapping("/listall") public String listAll(){ return "listall"; } }
在jsp文件夾下創(chuàng)建listall.jsp頁(yè)面。
listall.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/5/14 Time: 10:05 To change this template use File | Settings | File Templates. --%> <%@ page isELIgnored="false"%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>首頁(yè)</title> </head> <body> <input type="button" id="btn" value="獲取數(shù)據(jù)"/> <table width="80%" align="center"> <tr> <td>id</td> <td>姓名</td> </tr> <tbody id="content"> </tbody> </table> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script type="text/javascript"> $(function () { $('#btn').click(function () { $.post("list/allCategory",function (data) { console.log(data); var html = ""; for(var i = 0; i <data.length;i++){ html += "<tr>" + "<td>"+ data[i].id+"</td>"+ "<td>"+ data[i].name+"</td>"+ +"</tr>"; } $('#content').html(html); }) }) }) </script> </body> </html>
然后訪問(wèn)測(cè)試listall頁(yè)面:
先訪問(wèn)list/allCategory請(qǐng)求路徑,看看數(shù)據(jù)是否正常回顯。
然后訪問(wèn)listall, 點(diǎn)擊一下獲取數(shù)據(jù),OK,沒(méi)問(wèn)題吧,老鐵們。
到此這篇關(guān)于IDEA整合SSM框架實(shí)現(xiàn)網(wǎng)頁(yè)上顯示數(shù)據(jù)的文章就介紹到這了,更多相關(guān)IDEA整合SSM內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中將UUID存儲(chǔ)為Base64字符串的方法實(shí)現(xiàn)
使用Base64編碼來(lái)對(duì)UUID存儲(chǔ)在一些特定的場(chǎng)合被廣泛的使用,本文主要介紹了Java中將UUID存儲(chǔ)為Base64字符串的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04java基于C/S結(jié)構(gòu)實(shí)現(xiàn)多線程聊天室
這篇文章主要為大家詳細(xì)介紹了java基于C/S結(jié)構(gòu)實(shí)現(xiàn)多線程聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01mybatis 根據(jù)id批量刪除的實(shí)現(xiàn)操作
這篇文章主要介紹了mybatis 根據(jù)id批量刪除的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08深入了解Java核心類庫(kù)--BigDecimal和System類
這篇文章主要為大家詳細(xì)介紹了javaBigDecimal和System類定義與使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-07-07使用Idea maven創(chuàng)建Spring項(xiàng)目過(guò)程圖解
這篇文章主要介紹了使用Idea maven創(chuàng)建Spring項(xiàng)目過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02mybatis查詢數(shù)據(jù)賦值到model里面為空的解決
這篇文章主要介紹了mybatis查詢數(shù)據(jù)賦值到model里面為空的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01