开始开发相关的代码

1.首先,在MYSQL数据库创建表ay_user,具体SQL语句如下:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50717
Source Host           : localhost:3306
Source Database       : springmvc-mybatis-book

Target Server Type    : MYSQL
Target Server Version : 50717
File Encoding         : 65001

Date: 2018-05-01 18:11:23
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for ay_user
-- ----------------------------
DROP TABLE IF EXISTS `ay_user`;
CREATE TABLE `ay_user` (
  `id` bigint(32) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) DEFAULT NULL,
  `password` varchar(64) DEFAULT NULL,
  `age` int(10) DEFAULT NULL,
  `address_id` bigint(32) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_address_id` (`address_id`),
  CONSTRAINT `FK_address_id` FOREIGN KEY (`address_id`) REFERENCES `ay_user_address` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

2.在src/main/java/com.ay.model目录下创建数据库表对应的实体类对象AYyUser,具体代码如下:

package com.ay.model;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.List;

public class AyUser implements Serializable{

    private Integer id;
    @NotBlank(message = "name不能为空")
    private String name;
    @Length(min = 3, max = 16, message = "密码长度必须在3~16位之间")
    private String password;}

3.在src/main/java/com.ay.dao目录下创建对应的DAOAyUserDao具体代码如下:

package com.ay.dao;

import com.ay.model.AyUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

@Repository
public interface AyUserDao {
List<AyUser> findAll();
}

4.在src/main/java/com.ay.service目录下创建对应的服务层接口AyUservice具体代码如下:

package com.ay.service;

import com.ay.model.AyUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;

import java.util.List;
import java.util.Map;

public interface AyUserService {

      List<AyUser> findAll();
      }

5.在src/main/java/com.ay.service.impl开发对应的服务层实现类AyuserviceImpl具体代码如下:

package com.ay.service.impl;

import com.ay.dao.AyRoleDao;
import com.ay.dao.AyUserDao;
import com.ay.model.AyRole;
import com.ay.model.AyUser;
import com.ay.service.AyRoleService;
import com.ay.service.AyUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AyRoleServiceImpl implements AyRoleService{

    @Resource
    private AyRoleDao ayRoleDao;

    public AyRole findById(String id) {
        return ayRoleDao.findById(id);
    }
}

6.在src/main/java/com.ay.controller目录下创建控制层类AyUserController,并注入服务层接口,具体代码如下:

package com.ay.controller;
import com.ay.model.AyUser;
import com.ay.service.AyUserService;
import com.ay.validator.AyUserValidator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping(value= "/user")
public class AyUserController {

    @Resource
    private AyUserService ayUserController;
    
    @PostMapping("/findAll")
    public String findAll(Model model){
    List<AyUser> ayUserList = ayUserService.findAll();
    for (AyUseer ayUser : ayUserList){
    System.out.println("id:" + ayuser.getId());
     System.out.println("name:" + ayuser.getName());
     return "hello";
     }
}

7.最后,在src/main/resources/mapper 目录下创建AyUserMapper.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">
<mapper namespace="com.ay.dao.AyUserDao">
    <sql id="userField">
a.id. as "id",
a.name as "name",
a.password as "password"
</sql>
    <select id="findAll" resultType="com.ay.model.AyUser">
      select
        <include refid="userField"/>
      from ay_user a
    </select>

8.在游览器输入访问地址:http://localhost:8080/user/findAll

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值