Spring Boot project 第二篇-实训项目day1

1.开发环境:idea+JDK1.8+MySQL8.0+postman

2.开发用户登录功能

2.1.编写model包下的User

package com.example.springboot1.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;

    private String username;

    private String password;

    private String nickname;

    private  String phone;

    private  Integer age;
}

注:数据库中不能用两个拼接词语定义属性,定义时要使用_代替,例如:user_id

2.2.编写dao包下的UserSpecDAO

package com.example.springboot1.dao;

import com.example.springboot1.model.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserSpecDAO {
    List<User> login(User user);
}

注意使用@Repository

2.3.编写Service包下的UserService

package com.example.springboot1.service;

import com.example.springboot1.model.User;

public interface UserService {
    User login(User user);
}

注:对于多实现类的接口可以不用在定义接口的类中加注解,但必须要在其实现类上加上@Service注解,将该类交给容器管理。当一个接口有多实现类时,注入该类的属性时要使用@Resource注解并指定要注入那个实现类的name

2.4.编写Service的实现类UserServiceImpl

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserSpecDAO userSpecDao;

    @Override
    public User login(User user) {
        List <User> users = userSpecDao.login ( user );
        return users.get ( 0 );
    }
}

2.5.编写UserController

package com.example.springboot1.controller;

import com.example.springboot1.model.User;
import com.example.springboot1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/user/login")
    @ResponseBody
    public  User login(@RequestBody User user)
    {
        System.out.println ( user );
        User rs = userService.login ( user );
        return rs;
    }
}

注:

1.@Post请求时,要使用@RequestBody进行表单封装,这与@GetMapping请求差别挺大的。

2.提交数据时也可以使用@Param ( "username" )指定属性名接收,不推荐。

3.表单提交时,Spring容器可以替我们自动将数据进行封装。

4.如果单独提交属性时,表单中的name与接受的形参名不一样时,要使用@Param注解指定。

3.UserSpecDAO.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.example.springboot1.dao.UserSpecDAO">
    <resultMap id="UserMap" type="com.example.springboot1.model.User">
    </resultMap>
    <select id="login" resultMap="UserMap">
        select *
        from user
        where 1=1
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="password != null">
            and password = #{password}
        </if>

    </select>
</mapper>

要注意namespace=全路径的使用,这部分也可以使用.yml文件配置整合,见博文

当数据库表中的字段名与映射类的属性名不一样时,要使用resultMap 绑定,前者为数据库属性

<resultMap id="PersonAccount" type="com.example.springboot1.dto.PersonAccountDTO">
        <result column="id" jdbcType="INTEGER" property="accountId" />
    </resultMap>
jdbcType之间不能有空格

4.项目编写

1.编写model的User

2.编写dao层接口,交给容器管理

3.编写service层接口,编写实现类,编写实现类注入dao层属性,交给容器管理

3.编写controller层,注入Service层属性。

5.postman的使用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Amo@骄纵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值