IDEA整合SSM框架实现网页上显示数据

网友投稿 383 2023-01-14

IDEA整合SSM框架实现网页上显示数据

我们来整合SSM框架

第一步、

创建一个maven工程。配置Tomcat,并测试是否正常访问HelloWorld.

这一步就省略了。

不懂得看这个博客:

idea创建maven项目

创建出来是这样的:

我们从这里开始整合ssm。

第二步、

在pom.xml导入依赖,以下依赖是ssm常用的一些依赖,都导进去,没有坏处。

UTF-8

1.7

1.7

2.9.0

junit

junit

4.11

test

org.springframework

spring-webmvc

5.1.3.RELEASE

aopalliance

aopalliance

1.0

org.aspectj

aspectjweaver

1.9.2

org.apache.commons

commons-dbcp2

2.7.0

org.apache.commons

commons-pool2

2.8.0

org.springframework

spring-aspects

5.1.3.RELEASE

org.springframework

spring-test

5.1.3.RELEASE

mysql

mysql-connector-java

5.1.46

com.mchange

c3p0

0.9.5.2

org.mybatis

mybatis

3.4.5

org.springframework

spring-jdbc

5.1.3.RELEASE

org.mybatis

mybatis-spring

1.3.1

org.springframework

spring-tx

5.1.3.RELEASE

org.projectlombok

lombok

1.18.10

net.sf.json-lib

json-lib

2.4

jdk15

commons-beanutils

commons-beanutils

1.7.0

commons-collections

commons-collections

3.1

commons-lang

commons-lang

2.5

net.sf.ezmorph

ezmorph

1.0.3

commons-logging

commons-logging

1.2

com.alibaba

druid

1.2.6

javax.servlet.jsp.jstl

jstl-api

1.2-rev-1

taglibs

standard

1.1.2

org.apache.taglibs

taglibs-standard-spec

1.2.5

org.apache.taglibs

taglibs-standard-impl

1.2.5

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

com.fasterxml.jackson.core

jackson-core

${jackson.version}

com.fasterxml.jackson.core

jackson-annotations

${jackson.version}

com.alibaba

easyexcel

2.1.1

org.apache.poi

poi

&lmUCVCt;version>3.17

org.apache.poi

poi-ooxml

3.17

src/main/java

**/*.xml

第三步、

创建数据表。

创建一个名称为category_的数据表,只有两个字段,一个id, 一个name,id自增。随便插入点数据。

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', '违法收费');

INSERT INTO `category_` VALUES ('6', '发生的v');

INSERT INTO `category_` VALUES ('7', 'sdfsd');

INSERT INTO `category_` VALUES ('8', '34535');

第四步、

编写实体类;DAO层;Service层;Controller层。

像我这样,在java文件夹目录下,创建这几个包。

然后在包下创建对应的java文件或配置文件,最终的项目结构是这样的:

(那些打马赛克的东西你们用不到,小孩子别看那些。)

接下来开始写实体类:

实体类名称最好见名知意,跟数据表名称要对上。因为使用了lombok插件,所以使用@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层,这里写的多是接口,只写方法,不实现。

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 list();

}

然后在DAO层下创建对应的Mapper.xml文件。

注意namespace要对上,还有id对应上方法名。

CategoryMapper.xml

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into category_ ( name ) values (#{name})

delete from category_ where id= #{id}

select * from category_ where id= #{id}

update category_ set name=#{name} where id=#{id}

select * from category_

然后是Service层,这里才是你要实现业务的地方。这里也是接口,在impl实现类里才是写真正实现的。

这里只实现一个:查询所有数据。

CategoryService.java

package com.service;

import com.entity.Category;

import java.util.List;

public interface CategoryService {

List list();

}

然后写实现类。

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 list() {

return categoryDao.list();

}

}

然后写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 cs= categoryService.list();

// 放入转发参数

mav.addObject("cs", cs);

// 放入jsp路径

mav.setViewName("listCategory");

return mav;

}

}

OK,写到这里,你已经成功一半啦!给自己鼓鼓掌!

接下来,我们一鼓作气,写好配置文件并访问数据吧!

第五步、

编写配置文件。

在resources目录下,创建两个配置文件。

注意把数据库,密码,改成自己的,

applicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8

root

root

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8

root

root

springMVC.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView" />

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView" />

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView" />

value="org.springframework.web.servlet.view.JstlView" />

最后就是web.xml了。

web.xml

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

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

mvc-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springMVC.xml

1

mvc-dispatcher

/

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

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

mvc-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springMVC.xml

1

mvc-dispatcher

/

第六步、

创建listCategory.jsp文件。

在WEB-INF目录下创建jsp文件夹,将jsp文件都放在这里,因为我们在springMVC.xml中配置过的,就不过多解释了。

不知道在哪创建的可以回头看一下我的那个项目路径,根据那个来即可

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"%>

这里得到数据的思路我就不多说了吧,懂得都懂。就是jsp页面中通过jstl标签来获取到Controller中返回的数据。

OK,到这里,大功告成!启动Tomcat,测试一下。

没问题吧,注意这里的访问路径,http://localhost:8885/listCategory ,不是直接访问jsp页面,是访问controller中的请求路径。

ok,就到这吧,

这里有一个小小的改进,就是controller的改变,使用@ResponseBody注解来返回数据,然后前端通过ajax来异步请求数据。

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 findAll(){

List list = categoryService.list();

System.out.println(list.size());

return list;

}

@RequestMapping("/listall")

public String listAll(){

return "listall";

}

}

在jsp文件夹下创建listall.jsp页面。

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" %>

&mUCVClt;/body>

然后访问测试listall页面:

先访问list/allCategory请求路径,看看数据是否正常回显。

然后访问listall, 点击一下获取数据,OK,没问题吧,老铁们。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:中兴物流查询(中兴货运单号查询)
下一篇:springboot中如何使用自定义两级缓存
相关文章

 发表评论

暂时没有评论,来抢沙发吧~