菜单小项目
1.创建数据库表
create table menu(
id int(10) primary key auto_increment,
name varchar(20),
pid int(10)
);
insert into menu values(default,'系统设置',0);
insert into menu values(default,'销售管理',0);
insert into menu values(default,'修改密码',1);
insert into menu values(default,'添加用户',1);
insert into menu values(default,'销售人员新增',2);
insert into menu values(default,'删除销售人员',2);
2.新建项目Web Project,取名为menu
3.导入jar包
4.配置WEB-INF下的web.xml
有四个标签
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文参数 -->
<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>
5.配置src下的全局配置文件spring和springmvc
spring的配置文件applicationContext.xml
注:spring扫描service,而springmvc扫描控制器
property name=“typeAliasesPackage” value="com.youdian.pojo"加载类型别名
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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" default-autowire="byName">
<!-- 注解扫描 -->
<context:component-scan base-package="com.youdian.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.youdian.pojo"></property>
</bean>
<!-- 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.youdian.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.youdian.service.impl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
注:基于通知需要配置AOP
其中需要加载属性文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/sum
jdbc.username = root
jdbc.password =root
springmvc的配置文件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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.youdian.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>
至此环境搭建完成
6.新建实体类
com.youdian.pojo包下的菜单类中生成get和set方法
public class Menu {
private int id;
private String name;
private int pid;
private List<Menu> children;
}
7.新建mapper层
public interface MenuMapper {
List<Menu> selByPid(int pid);
}
ResultMap需要用注解或者配置文件方式
配置文件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.youdian.mapper.MenuMapper">
<resultMap type="menu" id="mymap">
<id property="id" column="id"/>
<collection property="children" select="com.youdian.mapper.MenuMapper.selByPid" column="id"></collection>
</resultMap>
<select id="selByPid" parameterType="int" resultMap="mymap">
select * from menu where pid=#{0}
</select>
</mapper>
8.新建service接口
public interface MenuService {
List<Menu> show();
}
9.新建service接口实现类
开发用注解开发,最上面加入@Service
@Service
public class MenuServiceImpl implements MenuService {
@Resource
private MenuMapper menuMapper;
@Override
public List<Menu> show() {
return menuMapper.selByPid(0);
}
}
10.新建controller类
@Controller表示把当前类交给springmvc管理
@Resource默认ByName进行注入
MenuServiceImpl和MenuController
springmvc容器调用spring容器中内容
@Controller
public class MenuController {
/**
* springmvc容器调用spring容器中内容
*/
@Resource
private MenuService menuServiceImpl;
@RequestMapping("show")
@ResponseBody
public List<Menu> show(){
return menuServiceImpl.show();
}
}
11.新建WebContent下的jsp
- 新建一个js文件夹,放入jquery库
- jquery1.7中有效果
- cursor有多种值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.7.2.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);
});
//对所有父菜单添加点击事件
//live("事件名,多个事件使用空格分割",function(){})
$("dt").live("click",function(){
//slow normal fast 数值
$(this).siblings().slideToggle(1000);
});
})
</script>
</head>
<body>
</body>
</html>