一.导入jar包
二.在WEB-INFO下配置web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 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_4_0.xsd">
<!-- 加载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>
<!-- SpringMVC前端控制器 -->
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三.在src下配置applicationContext.xml配置文件-----spring环境配置
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName">
<!-- 注解扫描 -->
<context:component-scan base-package="com.kennosaur.service.impl"></context:component-scan>
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- SqlSessionFactory -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.kennosaur.pojo"></property>
</bean>
<!-- 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kennosaur.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="txManage">
<tx:attributes>
<tx:method name="ins*"/>
<tx:method name="del*"/>
<tx:method name="upd*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<aop:pointcut expression="execution(* com.kennosaur.service.impl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
四.在src目录下添加db.properties数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
五.在src目录下添加springmvc.xml配置文件----配置springmvc环境
<?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.xsd">
<!-- 扫描注解,只扫描controller包 -->
<context:component-scan base-package="com.kennosaur.controller"></context:component-scan>
<!-- 注解驱动,注册HandlerMapping和HandlerAdapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 设置静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!-- 自定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
六.com.kennosaur.controller中添加MenuController 类
package com.kennosaur.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.kennosaur.pojo.Menu;
import com.kennosaur.service.MenuService;
@Controller
public class MenuController {
/**
* springmvc容器调用spring容器中内容
*/
@Resource
private MenuService menuServiceImpl;
@RequestMapping("show")
@ResponseBody
public List<Menu> show() {
return menuServiceImpl.show();
}
}
七.com.kennosaur.mapper中添加MenuMapper.java文件和MenuMapper.xml文件
MenuMapper.java
package com.kennosaur.mapper;
import java.util.List;
import com.kennosaur.pojo.Menu;
public interface MenuMapper {
List<Menu> selByPid(int pid);
}
MenuMapper.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.kennosaur.mapper.MenuMapper">
<resultMap type="menu" id="mymap">
<id property="id" column="id"/>
<collection property="children" select="com.kennosaur.mapper.MenuMapper.selByPid" column="id"></collection>
</resultMap>
<select id="selByPid" parameterType="int" resultMap="mymap">
select * from menu where pid=#{0}
</select>
</mapper>
八.com.kennosaur.pojo添加Menu.java文件
package com.kennosaur.pojo;
import java.util.List;
public class Menu {
private int id;
private String name;
private int pid;
private List<Menu> children;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public List<Menu> getChildren() {
return children;
}
public void setChildren(List<Menu> children) {
this.children = children;
}
}
九.com.kennosaur.service
MenuService.java
package com.kennosaur.service;
import java.util.List;
import com.kennosaur.pojo.Menu;
public interface MenuService {
List<Menu> show();
}
十.com.kennosaur.service.impl
MenuServiceImpl.java
package com.kennosaur.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.kennosaur.mapper.MenuMapper;
import com.kennosaur.pojo.Menu;
import com.kennosaur.service.MenuService;
@Service
public class MenuServiceImpl implements MenuService{
@Resource
private MenuMapper menuMapper;
@Override
public List<Menu> show() {
return menuMapper.selByPid(0);
}
}
十一.浏览器显示页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(function() {
$.post("show",function(data){
var result="";
for (var i = 0; i < data.length; i++) {
result+="<dl>";
result+="<dt style='cursor:pointer'>"+data[i].name+"</dt>";
for (var j = 0; j < data[i].children.length; j++) {
result+="<dd>"+data[i].children[j].name+"</dd>";
}
result+="</dl>";
}
$("body").html(result);
});
/* 对所有父菜单添加点击事件 */
// $("静态父元素").on("事件名 多个事件使用空格分隔", "需要点击的子元素" , function(){})
$("body").on("click", "dl dt", function(){
// slow normal fast 数值
$(this).siblings().slideToggle(500);
});
})
</script>
</head>
<body>
</body>
</html>