• IDEA搭建-SSM三步走之步骤03


    IDEA搭建-SSM三步走之步骤03

    使用 IDEA搭建SSM项目[ SSM 全名: Spring+SpringMVC+MyBatis ]
    环境: IDEA, JDK1.8, Maven3.6,Tomcat7.0
    步骤分为三步:
    01: 创建maven 工程;
    02: 创建spring工程
    03: 写个查询demo测试

    第三步,写个查询demo测试

    数据库SQL

    -- 创建表
    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(255) DEFAULT NULL COMMENT '用户名',
      `password` varchar(255) DEFAULT NULL COMMENT '密码',
      `email` varchar(255) DEFAULT NULL COMMENT '邮箱',
      `mobile` varchar(255) DEFAULT NULL COMMENT '手机号',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表';
    
    -- 插入三条数据
    INSERT INTO `feissm`.`user`(`id`, `username`, `password`, `email`, `mobile`) VALUES (1, 'dafei', '123456', 'dafei@qq.com', '11111111');
    INSERT INTO `feissm`.`user`(`id`, `username`, `password`, `email`, `mobile`) VALUES (2, 'dafei_02', '123456', 'dafei_02@qq.com', '1111111122');
    INSERT INTO `feissm`.`user`(`id`, `username`, `password`, `email`, `mobile`) VALUES (3, 'dafei_03', '123456', 'dafei_03@qq.com', '1111111133');
    View Code

    model 目录下 User.java文件

    User.java 文件内容

     public class User {
        private long id;
        private String username;
        private String password;
        private String email;
        private String mobile;
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getMobile() {
            return mobile;
        }
        public void setMobile(String mobile) {
            this.mobile = mobile;
        }
    }
    View Code

    dao目录下 UserDao.java文件

    UserDao.java文件内容

    package com.dafei.dao;
    
    import com.dafei.model.User;
    
    public interface UserDao {
        User selectUser(long id);
    }

    resources-->mapper目录下面 UserMapper.xml

    UserMapper.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">
    
    <!-- 设置为UserDao接口方法提供sql语句配置 -->
    <mapper namespace="com.dafei.dao.UserDao">
    
        <select id="selectUser" resultType="User" parameterType="long">
          SELECT * FROM user WHERE id = #{id}
        </select>
    
    </mapper>

    service目录下面UsrService.java文件

    UsrService.java 文件内容

    package com.dafei.service;
    
    import com.dafei.model.User;
    
    public interface UserService {
        public User selectUser(long userId);
    }

    service-->impl目录下面UsrServiceImpl.java文件

    UsrServiceImpl.java文件内容

    package com.dafei.service.impl;
    
    import com.dafei.dao.UserDao;
    import com.dafei.model.User;
    import com.dafei.service.UserService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    @Service("userService")
    public class UserServiceImpl implements UserService {
        @Resource
        private UserDao userDao;
    
        @Override
        public User selectUser(long userId) {
            return userDao.selectUser(userId);      
        }
    }

    controller目录下面UserController.java

    UserController.java文件内容

    package com.dafei.controller;
    
    import com.dafei.model.User;
    import com.dafei.service.UserService;
    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;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @RequestMapping("/select")
        public ModelAndView selectUser() {
            ModelAndView mv = new ModelAndView();
            User user = userService.selectUser(1);
            mv.addObject("user", user);
            mv.setViewName("user");
            return mv;
        }
    }

    启动项目测试

    访问地址 localhost:8080/user/select 查看数据

    整个项目的目录和文件结构

    E:selfwebgit_devjavajava01simplessmsrc
    └─main
        ├─java
        │  └─com
        │      └─dafei
        │          ├─controller
        │          │      UserController.java
        │          │
        │          ├─dao
        │          │      UserDao.java
        │          │
        │          ├─model
        │          │      User.java
        │          │
        │          └─service
        │              │  UserService.java
        │              │
        │              └─impl
        │                      UserServiceImpl.jav
        │
        ├─resources
        │  │  jdbc.properties
        │  │  log4j.properties
        │  │  spring-mvc.xml
        │  │  spring-mybatis.xml
        │  │
        │  └─mapper
        │          UserMapper.xml
        │
        └─webapp
            │  index.jsp
            │
            └─WEB-INF
                │  web.xml
                │
                └─jsp
                        user.jsp
                        userAll.jsp
    View Code

    IDEA搭建-SSM三步走之步骤01

    IDEA搭建-SSM三步走之步骤02

  • 相关阅读:
    生活所迫,夫妻俩回到家乡搞养殖,建立公司与村民一起致富
    农村也可以干大事业,小伙返乡种植葡萄,年收益达100万
    为让家人过得更好,她放弃稳定工作选择创业,竟连开四家店
    2年融资3.5亿,她用“网红+短视频”模式掘金6亿下沉人群市场
    从小就对生意耳濡目染的小伙,长大后创业资产过亿
    夫妇俩打工多年后返乡创业,年销售额高达四五十万元
    一道普通菜,却能炒出亿万家业,看她成功的秘诀是什么?
    警示2018:那些值得在年底彻查和回顾的数据库事件
    可扩展性数据库的架构设计
    可扩展性数据库的架构设计
  • 原文地址:https://www.cnblogs.com/dafei4/p/13273093.html
Copyright © 2020-2023  润新知