Spring+SpringMVC+Mybatis(开发必备技能)01、基础idea环境配置
Spring+SpringMVC+Mybatis(开发必备技能)
01、基础idea环境配置
环境:
数据库:MySQL
开发工具:IntelliJ IDEA 2020.1.3 x64
框架:SSM(Spring+SpringMVC+Mybatis)
目录
5、创建数据库链接在资源文件【jdbc.properties】
6、在【resources】下创建【mapper】文件夹与【UsersMapper.xml】测试类文件
7、Spring配置文件【applicationContext.xml】的创建
8、spring-mvc配置文件spring-mvc.xml配置
1、创建idea的maven项目
写上项目名称,点击【Next】
选择maven路径
2、引入maven所需要的所有包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!--mybatis所需要的包-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--数据库事务管理-->
<!-- Spring提供了两种使用JDBC API的最佳实践,
一种是以JdbcTemplate为核心的基于Template的JDBC的使用方式,
另一种则是在JdbcTemplate基础之上的构建的基于操作对象的JDBC的使用方式。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- Spring-tx模块负责在spring框架中实现事务管理功能。以aop切面的方式将事务注入到业务代码中,并实现不同类型的事务管理器。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--由于为了方便api接口与本地服务的jsp页面所以也要引入jsp包(用作考试的也一样,方便)-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 这里是servlet的api服务Tomcat要用到 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- DBCP是Java的数据库连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!--json-->
<!-- fastjson是一个性能很好的Java语言实现的json解析器和生成器,来自阿里巴巴的工程师开发。是谁就不知道了。 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.2</version>
</dependency>
<!-- JDBC组件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能。
spring boot starter自动装配组件,简化组件引入的开发工作量,
所以Druid推出了druid-spring-boot-starter-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 分页会用到 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 处理sql的 -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.0</version>
</dependency>
粘贴位置:
3、包解释:
Spring需要的包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
Mybatis需要的包:
<!--mybatis所需要的包-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--数据库事务管理-->
<!-- Spring提供了两种使用JDBC API的最佳实践,
一种是以JdbcTemplate为核心的基于Template的JDBC的使用方式,
另一种则是在JdbcTemplate基础之上的构建的基于操作对象的JDBC的使用方式。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- Spring-tx模块负责在spring框架中实现事务管理功能。以aop切面的方式将事务注入到业务代码中,并实现不同类型的事务管理器。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
jsp与servlet需要的包:
<!--由于为了方便api接口与本地服务的jsp页面所以也要引入jsp包(用作考试的也一样,方便)-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 这里是servlet的api服务Tomcat要用到 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
数据库连接池DBCP:
<!-- DBCP是Java的数据库连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
Json:
<!--json-->
<!-- fastjson是一个性能很好的Java语言实现的json解析器和生成器,来自阿里巴巴的工程师开发。是谁就不知道了。 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.2</version>
</dependency>
JDBC组件:
<!-- JDBC组件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能。
spring boot starter自动装配组件,简化组件引入的开发工作量,
所以Druid推出了druid-spring-boot-starter-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
分页:
<!-- 分页会用到 -->
<dependency>
<groupId>com.github.pagehelper</groupI
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
复杂sql处理:
<!-- 处理sql的 -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.0</version>
</dependency>
4、创建资源文件夹:
打开项目层级,看到包已经下载好了,并引入成功,但是【src】下只有webapp,所以需要鼠标右键创建需要的4个文件夹。
在【src】上点击鼠标右键,创建四个文件夹:
全选并回车创建
项目层级:
5、创建数据库链接在资源文件【jdbc.properties】
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://rm-bp1zq3879r28p726lco.mysql.rds.aliyuncs.com:3306/test?characterEncoding=utf8
username=qwe8403000
password=Qwe8403000
#下面在有需要配置就行,没有需要不用记那么多,用的话过来复制
#初始化链接数量
initialSize=0
#最大连接数,一般20够测试了
maxActive=20
#最多多少空闲在
maxIdle=20
#最少多少空闲在
minIdle=1
#最大等待时间
maxWait=60000
6、在【resources】下创建【mapper】文件夹与【UsersMapper.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.item.dao.UsersMapper">
<!-- 用作基础查询测试 -->
<select id="GetInfo" resultType="Users">
select * from users
</select>
<!-- 用做传递参数测试 -->
<select id="SelectName" resultType="Users">
select * from users where nickName like "%${nickName}%"
</select>
</mapper>
7、Spring配置文件【applicationContext.xml】的创建
配置文件: (有详细的注解)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 加载properties文件·就是刚才写数据库连接信息的文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 配置数据源·你要连接的数据库,为了方便大家,我弄了个一年的数据库,免费使用,但是连接数只有20,别太拥挤 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- spring包里封装了有mybatis的SqlSessionFactoryBean工厂,直接用就行 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 获取model包下的模型,这里可能命名不同,entity或者pojo都是,也可能叫javabean -->
<property name="typeAliasesPackage" value="com.item.model"/>
<!-- 获取映射的xml文件,其中*代表所有 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 数据库工厂直接获取dao层的mapper,也就是将mapper的接口内容拿到 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这里可以理解成依赖注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- dao层的位置 -->
<property name="basePackage" value="com.item.dao"/>
</bean>
<!-- 事务管理,这就不废话了 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
8、spring-mvc配置文件spring-mvc.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-3.0.xsd">
<!-- 获取item下在所有上下文内容,自己些项目在时候注意改名 -->
<context:component-scan base-package="com.item"/>
<!-- 开注解 -->
<mvc:annotation-driven/>
<!-- 用于views层处理静态资源 -->
<mvc:default-servlet-handler/>
<!-- 处理返回试图的后缀,自己看好路径啊。我自己创建了一个【views】文件夹,里面放所有的视图文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
9、创建各层级【模型层model】、【数据层dao】、【控制器controller】、【业务逻辑接口层service】、【业务逻辑实现层serviceimpl】、【视图层resources/webapp/views】
当前所有的层级以及包名,还差一个webapp下【WEB-INF/web.xml】没配置。
10、webapp下【WEB-INF/web.xml】配置
替换配置
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>demo_515</display-name>
<!--初始访问页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- ServletContext监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring核心 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 格式化编码格式的,免得req和resp配置 -->
<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>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
替换效果:(注释写在的相对好理解,不理解的留言)
11、创建测试数据库(也可以直接用我的,连接不变即可)
/*
Navicat Premium Data Transfer
Source Server : myALi
Source Server Type : MySQL
Source Server Version : 50732
Source Host : rm-bp1zq3879r28p726lco.mysql.rds.aliyuncs.com:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50732
File Encoding : 65001
Date: 24/05/2022 21:59:06
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
`userName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户账号',
`pwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户密码',
`nickName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户姓名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户信息表\r\n\r\n这个表是用来记录用户的基础信息\r\n\r\n' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, '文日红', '123456', '有一姑娘,清扬婉兮。');
INSERT INTO `users` VALUES (2, '杨春红', '1234657', '灿如春华,皎若明月');
INSERT INTO `users` VALUES (3, '顾容菲', '123', '二九年华,娉婷姌嫋。');
INSERT INTO `users` VALUES (4, '韦文静', '1234', '明眸善睐,靥辅承权,瑰姿艳逸,仪静体闲。');
INSERT INTO `users` VALUES (5, '卢雪', '12345', '落落大方,英姿飒爽。');
SET FOREIGN_KEY_CHECKS = 1;
12、完成各层级编码(具体注解与逻辑下篇文章具体讲解)
需要5个文件,依次是【模型层Users】、【数据层UsersMapper】、【业务逻辑接口层UsersService】、【业务逻辑实现层UsersServiceImpl】、【控制层UsersController】、【视图层GetInfo.jsp】顺序如果错了回看到层层报错,将所有文件编写完才能不报红,所以建议按照顺序来。
【模型层Users】
package com.item.model;
public class Users {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", userName='" + userName + '\'' +
", pwd='" + pwd + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
private int id;
private String userName;
private String pwd;
private String nickName;
}
【数据层UsersMapper】
package com.item.dao;
import com.item.model.Users;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UsersMapper {
List<Users> GetInfo();
List<Users> SelectName(@Param("nickName") String nickName);
}
【业务逻辑接口层UsersService】
package com.item.service;
import com.item.model.Users;
import java.util.List;
public interface UsersService {
List<Users> GetInfo();
List<Users> SelectName(String nickName);
}
【业务逻辑实现层UsersServiceImpl】
package com.item.serviceimpl;
import com.item.dao.UsersMapper;
import com.item.model.Users;
import com.item.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public List<Users> GetInfo() {
return usersMapper.GetInfo();
}
@Override
public List<Users> SelectName(String nickName) {
return usersMapper.SelectName(nickName);
}
}
【控制层UsersController】
package com.item.controller;
import com.item.model.Users;
import com.item.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@CrossOrigin
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping("/GetInfoApi")
@ResponseBody
public Object GetInfoApi(){
List<Users> list=usersService.GetInfo();
System.out.println(list);
return list;
}
@RequestMapping("/GetInfo")
public String GetInfo(HttpServletRequest request,Model model){
List<Users> list=usersService.GetInfo();
model.addAttribute("lists",list);
return "GetInfo";
}
@RequestMapping("/GetName")
public String GetName(HttpServletRequest request,Model model){
String nickName = request.getParameter("nickName");
List<Users> list=usersService.SelectName(nickName);
model.addAttribute("lists",list);
return "GetInfo";
}
}
【视图层GetInfo.jsp】
<%@ page import="java.util.List" %>
<%@ page import="com.item.model.Users" %><%--
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/5/24 0024
Time: 22:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<form action="/GetName" method="get">
<input type="text" placeholder="输入昵称查询" name="nickName"/>
<input type="submit" value="提交" class="btn btn-primary"/>
</form>
<% List<Users> lists = (List<Users>) request.getAttribute("lists");%>
<table class="table table-bordered table-hover table-striped">
<tr class="info">
<th>编号</th>
<th>账号</th>
<th>密码</th>
<th>昵称</th>
</tr>
<%
for (Users u : lists) {
%>
<tr>
<td><%=u.getId()%></td>
<td><%=u.getUserName()%></td>
<td><%=u.getPwd()%></td>
<td><%=u.getNickName()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
13、配置Tomcat
去掉麻烦的路径:
14、启动测试
看到successfully,并且启动中没有错误提示代表启动成功。
15、接口访问测试
基本路径:【http://localhost:8088/】
api接口路径:【http://localhost:8088/GetInfoApi】
视图路径:【http://localhost:8088/GetInfo】
跨域测试(编码如下):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
</head>
<body>
<button onclick="ontest()">测试</button>
<script>
function ontest() {
$.ajax({
url: "http://127.0.0.1:8088/GetInfoApi",
type: "get",
success: function(data) {
console.log(data);
}
});
}
</script>
</body>
</html>
自行创建一个【index.html】用作测试:
打开后,F12,看控制台【Console】,点击一下【测试】按钮即可看到数据。
希望能给大家带来帮助,欢迎一键三连哦。
华为开发者空间发布
让每位开发者拥有一台云主机
- 点赞
- 收藏
- 关注作者
评论(0)