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

Spring 练习

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

Spring 练习

Spring 练习
  • 1. 环境搭建步骤分析
    • 1.1 项目目录
      • 1.1.1 pom.xml配置文件
      • 1.1.2 log4j.properties配置文件
      • 1.1.3 jdbc.properties配置文件
      • 1.1.4 web.xml配置文件
      • 1.1.5 spring-mvc.xml配置文件
      • 1.1.6 applicationContext.xml配置文件
  • 2. 角色列表展示
    • 2.1 角色列表的展示步骤分析
      • 2.1.1 controller 层实现
      • 2.1.2 service 层实现
    • 2.2 配置实现
    • 2.3 页面展示
  • 3. 角色添加
    • 3.1 角色添加的步骤分析
    • 3.2 代码实现
      • 3.2.1 role-add.jsp

1. 环境搭建步骤分析
  1. 创建工程(Project&Module)
  2. 导入静态页面(见资料jsp页面)
  3. 导入需要坐标(见资料中的pom.xml)
  4. 创建包结构(controller、service、dao、domain、utils)

controller:web层
service:业务层
domain:实体
utils:工具包

  1. 导入数据库脚本(见资料test.sql)
  2. 创建POJO类(见资料User.java和Role.java)
  3. 创建配置文件(applicationContext.xml、spring-mvc.xml、jdbc.properties、log4j.properties、web.xml)

applicationContext.xml:spring的核心配置文件
spring-mvc.xml:spring-mvc的核心配置文件
jdbc.properties:抽取数据库的连接信息
log4j.properties:关于日志的配置文件

1.1 项目目录


1.1.1 pom.xml配置文件



    4.0.0

    com.itheima
    itheima_spring_test
    1.0-SNAPSHOT
    war

    itheima_spring_test Maven Webapp
    
    http://www.example.com


    
        
            mysql
            mysql-connector-java
            8.0.27
        
        
            c3p0
            c3p0
            0.9.1.2
        
        
            com.alibaba
            druid
            1.1.10
        
        
            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
            org.springframework
            spring-web
            5.0.5.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.0.5.RELEASE
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
            provided
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.2.1
            provided
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.0
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.0
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.0
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.3
        
        
            commons-logging
            commons-logging
            1.2
        
        
            org.slf4j
            slf4j-log4j12
            1.7.7
        
        
            log4j
            log4j
            1.2.17
        
        
            org.springframework
            spring-jdbc
            5.0.5.RELEASE
        
        
            org.springframework
            spring-tx
            5.0.5.RELEASE
        
        
            jstl
            jstl
            1.2
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.1
                
                    13
                    13
                
            
        
    


1.1.2 log4j.properties配置文件
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout
1.1.3 jdbc.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
1.1.4 web.xml配置文件



    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        CharacterEncodingFilter
        /*
    

    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    


    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        2
    
    
        DispatcherServlet
        /
    


1.1.5 spring-mvc.xml配置文件



    
    

    
    
        
        
    

    
    

    
    


1.1.6 applicationContext.xml配置文件




    
    

    
    
        
        
        
        
    

    
    
        
    

    
    
        
    

    
    
        
    

    
    
        
        
    

    
    
        
    



2. 角色列表展示 2.1 角色列表的展示步骤分析
  1. 点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)
  2. 创建 RoleController 和 showList() 方法
  3. 创建 RoleService 和 showList() 方法
  4. 创建 RoleDao 和 findAll() 方法
  5. 使用 JdbcTemplate 完成查询操作
  6. 将查询数据存储到 Model 中
  7. 转发到 role-list.jsp 页面进行展示
2.1.1 controller 层实现
package com.itheima.controller;

import com.itheima.domain.Role;
import com.itheima.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 roleList = roleService.list();
        //设置模型
        modelAndView.addObject("roleList", roleList);
        //设置视图
        modelAndView.setViewName("role-list");
        return modelAndView;
    }
}
2.1.2 service 层实现

RoleService:

package com.itheima.service;

import com.itheima.domain.Role;

import java.util.List;

public interface RoleService {
    public List list() ;

    void save(Role role);
}

RoleServiceImpl:

package com.itheima.service.impl;

import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;

import java.util.List;

public class RoleServiceImpl implements RoleService {

    private RoleDao roleDao;
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    public List list() {
        List roleList = roleDao.findAll();
        return roleList;
    }

    public void save(Role role) {
        roleDao.save(role);
    }
}

RoleDao:

package com.itheima.dao;

import com.itheima.domain.Role;

import java.util.List;

public interface RoleDao {
    List findAll();

    void save(Role role);

    List findRoleByUserId(Long id);
}

RoleDaoImpl:

package com.itheima.dao.impl;

import com.itheima.dao.RoleDao;
import com.itheima.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 findAll() {
        List roleList = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper(Role.class));
        return roleList;
    }

}
2.2 配置实现

RoleController.java:

package com.itheima.controller;

import com.itheima.domain.Role;
import com.itheima.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.servlet.ModelAndView;

import java.util.List;

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

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        List roleList = roleService.list();
        //设置模型
        modelAndView.addObject("roleList", roleList);
        //设置视图
        modelAndView.setViewName("role-list");
        return modelAndView;
    }
}

其他配置见spring-mvc.xml配置文件:

加入:


applicationContext.xml配置文件:

加入:


	




	




	
	




	

2.3 页面展示

role-list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>







数据 - AdminLTE2定制版

































	

角色管理 全部角色

列表

ID 角色名称 角色描述 操作
${role.id} ${role.roleName} ${role.roleDesc} 删除
3. 角色添加 3.1 角色添加的步骤分析
  1. 点击列表页面新建按钮跳转到角色添加页面
  2. 输入角色信息,点击保存按钮,表单数据提交服务器
  3. 编写 RoleController 的 save() 方法
  4. 编写 RoleService 的 save() 方法
  5. 编写 RoleDao 的 save() 方法
  6. 使用 JdbcTemplate 保存 Role 数据到 sys_role
  7. 跳转回角色列表页面
3.2 代码实现 3.2.1 role-add.jsp
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425365.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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