大飞_dafei

导航

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);
}

***** 强烈建议使用下面这种方式 ****

package com.dafei.dao;

import com.dafei.model.User;
import org.apache.ibatis.annotations.Param;

public interface UserDao {
    User selectUser(@Param("id") 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:\selfweb\git_dev\java\java01\simplessm\src
└─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

posted on 2020-07-09 13:20  大飞_dafei  阅读(248)  评论(0编辑  收藏  举报