application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: 123456
server:
port: 8001
持久层
package com.example.demo.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private String address;
private Integer age;
private String sex;
private String phone;
}
这里我们引入了 lombok 不需要写get和set方法简化代码
org.projectlombok
lombok
1.16.10
provided
mapper层
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
public interface UserMapper {
@Select(“select * from user”)
List findAll();
@Update(“INSERT INTO user
(name
, address
, age
, sex
, phone
) VALUES (#{name},#{address},#{age},#{sex},#{phone});”)
@Transactional
void save(User user);
@Update(“update user set name=#{name} , address=#{address}, age=#{age}, sex=#{sex},phone=#{phone} where id =#{id}”)
@Transactional
void updateById(User user);
@Delete(“delete from user where id =#{id}”)
@Transactional
void deleteById(Long id);
@Select(“select * from user where id =#{id}”)
User findById(Long id);
@Select(“select * from user limit #{offset},#{pageSize}”)
List findByPage(Integer offset, Integer pageSize);
@Select(“select count(id) from user”)
Integer countUser();
}
controller
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.vo.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping(“/user”)
public class UserController {
@Resource
UserMapper userMapper;
@GetMapping
public List getUser() {
return userMapper.findAll();
}
@PostMapping
public String addUser(@RequestBody User user){
//把前端传过来的数据转化为user实体类的对象插入到数据库中
userMapper.save(user);
return “success”;
}
@PutMapping
public String updateUser(@RequestBody User user){
userMapper.updateById(user);
return “success”;
}
@DeleteMapping(“/{id}”) //一一对相应的关系
public String deleteUser(@PathVariable(“id”) Long id){
//注解是循序json回传带有id
userMapper.deleteById(id);
return “success”;
}
@GetMapping(“/{id}”) //把返回的结果 返回出来 包装成一个user对象
public User findById(@PathVariable(“id”) Long id){
//注解是循序json回传带有id
return userMapper.findById(id);
}
@GetMapping(“/page”)
public Page findByPage(@RequestParam(defaultValue = “1”) Integer pageNum,
@RequestParam(defaultValue = “10”) Integer pageSize) {
Integer offset = (pageNum - 1) * pageSize;
List userData = userMapper.findByPage(offset, pageSize);
Page page = new Page<>();
page.setData(userData);
Integer total = userMapper.countUser();
page.setTotal(total);
page.setPageNum(pageNum);
page.setPageSize(pageSize);
return page;
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
文末
我将这三次阿里面试的题目全部分专题整理出来,并附带上详细的答案解析,生成了一份PDF文档
- 第一个要分享给大家的就是算法和数据结构
- 第二个就是数据库的高频知识点与性能优化
- 第三个则是并发编程(72个知识点学习)
- 最后一个是各大JAVA架构专题的面试点+解析+我的一些学习的书籍资料
还有更多的Redis、MySQL、JVM、Kafka、微服务、Spring全家桶等学习笔记这里就不一一列举出来
详细的答案解析,生成了一份PDF文档
- 第一个要分享给大家的就是算法和数据结构
[外链图片转存中…(img-LCx92taE-1712048106415)]
- 第二个就是数据库的高频知识点与性能优化
[外链图片转存中…(img-P4w43QFI-1712048106415)]
- 第三个则是并发编程(72个知识点学习)
[外链图片转存中…(img-Az8hwRb1-1712048106415)]
- 最后一个是各大JAVA架构专题的面试点+解析+我的一些学习的书籍资料
[外链图片转存中…(img-u4uPMgST-1712048106416)]
还有更多的Redis、MySQL、JVM、Kafka、微服务、Spring全家桶等学习笔记这里就不一一列举出来