Spring环境搭建

阅读 81 收藏 0 点赞 0 评论 0

本文内容纲要:

- Spring环境搭建

- 角色列表的展示步骤分析

Spring环境搭建

Spring环境搭建步骤

  1. 创建工程(Project&Module)
  2. 导入静态页面(见资料jsp页面)
  3. 导入需要坐标(controller、service、dao、domian、utils)
  4. 创建包结构(controller、service、dao、domian、utils)
  5. 导入数据库脚本
  6. 创建POJO类
  7. 创建配置文件(applicationContext.xml、spring-mvc.xml、jdbc.properties、log4j.properties)
  • applicationContext.xml

    <!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--dataSource数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pwd}"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
  • jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.user=root jdbc.pwd=1121

  • spring-mvc.xlm

    mvc:annotation-driven/ mvc:default-servlet-handler/ <context:component-scan base-package="com.jotian.controller"/>

  • 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 /

  • pom.xml大概

    org.springframework spring-context 5.2.1.RELEASE org.springframework spring-test 5.2.12.RElEASE org.springframework spring-core 5.2.1.RELEASE org.springframework spring-beans 5.2.1.RELEASE org.springframework spring-aop 5.2.1.RELEASE org.springframework spring-jdbc 5.2.12.RELEASE org.springframework spring-orm 5.1.5.RELEASE org.springframework spring-web 5.2.12.RELEASE org.springframework spring-webmvc 5.2.12.RELEASE junit junit 4.13.1 c3p0 c3p0 0.9.1.2

角色列表的展示步骤分析

  1. 点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)
  2. 创建RoleController和showList()方法
  3. 创建RoleServic和showList()方法
  4. 创建RoleDao和showList()方法
  5. 使用JdbcTemplate完成查询操作
  6. 将查询数据存储到Model中
  7. 转发到role-list.jsp页面进行展示
  • pojo

    @Data public class User{ private Long id; private String username; private String email; private String password; private String phoneNum; //当前用户具备哪些角色 private List roles; //set() get() 有参无参构造 toStinrg() }

    @Data public class Role{ private Long id; private String roleName; private String roleDesc; }

  • dao

    public interfice RoleDao { List showList(); List findRolebyUserId(Long id); }

    public class RoleDaoImpl implements RoleDao{ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate=jdbcTemplate; } piblic List showList(){ List rolelist = jdbcTemplate.query("select * from 表",new BeanPropertyRowMapping(Role.class)); return roleList; } public List findRolebyUserId(Long id){ return jdbcTemplate.query("select * from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=?",new BeanPropertyRowMapper(Role.class),id) } }

  • service

    public interface RoleService{ List showList(); }

    public class RoleServiceImpl implements RoleService{ private RoleDao roleDao; public void setRoleDao(RoleDao roleDao){ this.roleDao=roleDao; } public List showList(){ return roleDao.showList(); } }

  • controller

    @RequestMapping("/跳转地址") @Controller //注解需要进行扫描 public class RoleController{ @Autowired private RoleService roleService; @RequestMapping("/showlist") public ModelAndView showList(){ ModelAndView modelAndView = new ModelAndVicw; List roleList=roleService.showList(); //设置模型 modelAndView.addObject("showList",showList); //设置视图 modelAndView.setViewName("地址-showlist") return modelAndView; } }

  • 配置Bean xml文件

角色添加的步骤分析

  • 点击列表页面新建按钮跳转到角色添加页面
  • 输入角色信息,点击保存按钮,表单数据提交服务器
  • 编写RoleController的save()方法
  • 编写RoleService的save()方法
  • 编写RoleDao的save()方法
  • 使用JdbcTemplate保存Role数据到表(sys_role)
  • 跳转回角色列表页面

post存在乱码问题 get put delete

<!--解决乱码的过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  • dao

    public interface UserDao{ List list(); void save(User user); void saveUserRoleRel(user.getId,roleIds); }

    public class UserDaoImpl implements UserDao{ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate=jdbcTemplate; } public List list(){ return jdbcTemplate.query("select * from sys_user",new BeanPropertyRowmapper(User.class));
    } public void save(User user){ jdbcTemplate.update("insert into sys_user value(?,?,?,?,?)",null,user.getUsername,user.getEmail().user.getPassword(),user.getPhoneNum()) } public void saveUserRoleRel(user.getId,roleIds){

    }
    

    }

  • service

    public interface UserService{ public void list(); }

    public class UserServiceImpl implements UserService{ private UserDao userDao; private RoleDao roleDao; public void setUserDao(UserDao userDao){ this.userDao=userDao } public void setRoleDao(RoleDao roleDao){ tihs.roleDao=roleDao; } public List list(){ List userlist = userDao.list(); for(User user:userList){ //获得user的id Long id = user.getId(); //将id作为参数查询当前userId对应的role的信息 List roles = roleDao.findRolebyUserId(id); user.setRoles(roles);

        }
        return userDao.list();
    }
    public void save(User user,Long[] roleIds){
        //第一步 向sys_user表中存储数据
        userDao.save(user);
        //第二步 向sus_user_role关系表中存储多条数据
        userDao.saveUserRoleRel(user.getId,roleIds);
    
    }
    

    }

  • Controller

    @Controller @RequestMapping("/") public class UserController{ @Autowired private UserService userservice; @Autowired private RoleService roleService; @RequestMapping("/save") public Stirng save(User user,Long[] roleIds){ userService.save(user,roleIds); return "redirect:/user/list" }

    @RequestMapping("/saveUI")
    public String save(User user.Long roleIds){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList = roleService.List();
        modelAndView.addObject("roleList",roleList);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }
    @RequsetMapping("/")
    public ModelAndView list(){
        List<User> userlist = userService.list();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList",userList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }
    

    }

  • 配置Bean

本文内容总结:Spring环境搭建,角色列表的展示步骤分析,

原文链接:https://www.cnblogs.com/jotian/p/15727163.html
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号