栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring练习-CRUD操作[2]-角色列表的展示

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring练习-CRUD操作[2]-角色列表的展示

Spring练习-CRUD操作[2]-角色列表的展示

文章目录
  • Spring练习-CRUD操作[2]-角色列表的展示
    • 1.步骤分析
    • 2.测试打包发布空项目(只有静态页面)
    • 3.编写后台代码
      • 3.1 配置文件
      • 3.2 编写RoleController代码(返回ModelAndView)
      • 3.3编写RoleServiceImpl代码
      • 3.4 编写RoleDaoImpl代码
      • 3.5 修改 jsp文件
      • 3.5 启动服务器,测试

1.步骤分析

① 点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)
② 创建RoleController和showList()方法
③ 创建RoleService和showList()方法
④ 创建RoleDao和findAll()方法
⑤ 使用JdbcTemplate完成查询操作
⑦ 将查询数据存储到Model中 ,转发到role-list.jsp页面进行展示

2.测试打包发布空项目(只有静态页面)

1.打包并将项目部署到tomcat

(1)打包项目:

(2)将项目部署到tomcat:

(3)运行tomcat,测试:


3.编写后台代码 3.1 配置文件

1.配置applicationContext.xml



    
    
    
    
    
    
    
    
        
        
        
        
    
    
    
        
    



2.配置spring-mvc.xml



    
    
    
    
    
    
    
    
        
        
    


3.配置jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=root

4.配置web.xml



         
	
	
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        
        1
    
    
        DispatcherServlet
        
        /
    


3.2 编写RoleController代码(返回ModelAndView)
package com.itspring.controller;

import com.itspring.domain.Role;
import com.itspring.service.RoleService;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView showList() {
        List roleList = roleService.showList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roleList", roleList);
        modelAndView.setViewName("role-list");
        return modelAndView;
    }
}


3.3编写RoleServiceImpl代码
package com.itspring.service.impl;

import com.itspring.dao.RoleDao;
import com.itspring.domain.Role;
import com.itspring.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    public List showList() {
        return roleDao.findAll();
    }
}

3.4 编写RoleDaoImpl代码
package com.itspring.dao.impl;

import com.itspring.dao.RoleDao;
import com.itspring.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {

    //自动注入jdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List findAll() {
        String sql = "select * from sys_role";
        List roleList =
                jdbcTemplate.query(sql, new BeanPropertyRowMapper(Role.class));
        return roleList;
    }
}

//---------dao测试代码-----------
package com.itspring.dao.impl;

import com.itspring.dao.RoleDao;
import com.itspring.domain.Role;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class RoleDaoImplTest {

    @Autowired
    private RoleDao roleDao;

    
    @Test
    public void testFindAll() {
        List roleList = roleDao.findAll();
        roleList.forEach((s) -> {
            System.out.println(s);
        });
    }


}
3.5 修改 jsp文件
//-------aside.jsp-------------
  • 角色管理
  • //-------role-list.jsp-----------------
    ID 角色名称 角色描述 操作
    ${role.id} ${role.roleName} ${role.roleDesc} 删除
    3.5 启动服务器,测试

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/331662.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号