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