环境搭建
1、创建工程
2、导入静态页面
3、导入需要的坐标
4、创建包结构
5、导入数据库脚本
6、创建POJO类
7、创建配置文件
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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1、mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--2、内部资源视图解析器,前后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--3、方行静态资源,静态资源权限开发-->
<mvc:default-servlet-handler/>
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--全局的初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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: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">
<!--1、加载jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2、配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.url}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3、配置JDBC模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
用户和角色的关系
角色列表的展示步骤分析
1、点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)
2、创建RoleController和List()方法
package com.zg.controller;
import com.zg.domain.Role;
import com.zg.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@RequestMapping("/role")
public class RoleController {
private RoleService roleService;
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView modelAndView = new ModelAndView();
List<Role> roleList = roleService.list();
//设置模型
modelAndView.addObject("roleList",roleList);
//设置视图
modelAndView.setViewName("role-list");
System.out.println(roleList);
return modelAndView;
}
}
3、创建RoleService和List()方法
package com.zg.service.impl;
import com.zg.dao.RoleDao;
import com.zg.domain.Role;
import com.zg.service.RoleService;
import java.util.List;
public class RoleServiceImpl implements RoleService {
private RoleDao roleDao;
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
public List<Role> list() {
List<Role> roleList = roleDao.findAll();
return roleList;
}
4、创建RoleDao和findAll()方法
package com.zg.dao.impl;
import com.zg.dao.RoleDao;
import com.zg.domain.Role;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class RoleDaoImpl implements RoleDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Role> findAll() {
List<Role> roleList = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
return roleList;
}
5、使用JdbcTemplate完成查询操作(见4)
6、将查询数据存储到Model中
7、转发到role-list.jsp页面进行展示
查用用户管理的时候不能只查user表,user表只有用户的信息,这里除了查user表还要查角色信息,我们可以先查用户表,在查每个用户对应的角色表,最终查的数据把每一个user封装好,而user也有多个,最终查询的是多个user,这些user都放在一个集合当中,每一个user中又具备一个集合,这个集合封装的是role