0


10.SpringBoot实战演练

📝 个人主页:程序员阿红🔥
👑 生活平淡,用心就会发光,岁月沉闷,跑起来,就会有风
📣 推荐文章:
📚📚📚 【毕业季·进击的技术er】忆毕业一年有感
📚📚📚 Java 学习路线一条龙版
📚📚📚 一语详解SpringBoot全局配置文件

实战技能补充:lombok

<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><!--只在编译阶段生效--><scope>provided</scope></dependency>

需求:实现用户的CRUD功能

1. 创建springboot工程

image-20220629190538614

2. 导入pom.xml

<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.3</version></dependency>

3. User实体类编写

@DatapublicclassUserimplementsSerializable{privateInteger id;privateString username;privateString password;privateString birthday;privatestaticfinallong serialVersionUID =1L;}

4.UserMapper编写及Mapper文件

publicinterfaceUserMapper{intdeleteByPrimaryKey(Integer id);intinsert(Userrecord);intinsertSelective(Userrecord);UserselectByPrimaryKey(Integer id);intupdateByPrimaryKeySelective(Userrecord);intupdateByPrimaryKey(Userrecord);List<User>queryAll();}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.example.base.mapper.UserMapper"><resultMapid="BaseResultMap"type="com.example.base.pojo.User"><idcolumn="id"jdbcType="INTEGER"property="id"/><resultcolumn="username"jdbcType="VARCHAR"property="username"/><resultcolumn="password"jdbcType="VARCHAR"property="password"/><resultcolumn="birthday"jdbcType="VARCHAR"property="birthday"/></resultMap><sqlid="Base_Column_List">
 id, username, password, birthday
 </sql><selectid="selectByPrimaryKey"parameterType="java.lang.Integer"resultMap="BaseResultMap">
 select
  <includerefid="Base_Column_List"/>
 from user
 where id = #{id,jdbcType=INTEGER}
 </select><selectid="queryAll"resultType="com.example.base.pojo.User">
  select <includerefid="Base_Column_List"/>
  from user
  </select>
 .......
</mapper>

5. UserService接口及实现类编写

importcom.lagou.mapper.UserMapper;importcom.lagou.pojo.User;importcom.lagou.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassUserServiceImplimplementsUserService{@AutowiredprivateUserMapper userMapper;@OverridepublicList<User>queryAll(){return userMapper.queryAll();}@OverridepublicUserfindById(Integer id){return userMapper.selectByPrimaryKey(id);}@Overridepublicvoidinsert(User user){//userMapper.insert(user);    //将除id所有的列都拼SQL
    userMapper.insertSelective(user);//只是将不为空的列才拼SQL}@OverridepublicvoiddeleteById(Integer id){
    userMapper.deleteByPrimaryKey(id);}@Overridepublicvoidupdate(User user){
    userMapper.updateByPrimaryKeySelective(user);}}

6. UserController编写

importcom.lagou.pojo.User;importcom.lagou.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importorg.yaml.snakeyaml.events.Event;importjava.util.List;@RestController@RequestMapping("/user")publicclassUserController{@AutowiredprivateUserService userService;/**
  * restful格式进行访问
  * 查询:GET
  * 新增: POST
  * 更新:PUT
  * 删除: DELETE
  *//**
  * 查询所有
  * @return
  */@GetMapping("/query")publicList<User>queryAll(){return userService.queryAll();}/**
  * 通过ID查询
  */@GetMapping("/query/{id}")publicUserqueryById(@PathVariableInteger id){return userService.findById(id);}/**
  * 删除
  * @param id
  * @return
  */@DeleteMapping("/delete/{id}")publicStringdelete(@PathVariableInteger id){
    userService.deleteById(id);return"删除成功";}/**
  * 新增
  * @param user
  * @return
  */@PostMapping("/insert")publicStringinsert(User user){
    userService.insert(user);return"新增成功";}/**
  * 修改
  * @param user
  * @return
  */@PutMapping("/update")publicStringupdate(User user){
    userService.update(user);return"修改成功";}}

7. application.yml

##服务器配置
server:
port: 8090
servlet:
 context-path: /
##数据源配置
spring:
datasource:
 name: druid
 type: com.alibaba.druid.pool.DruidDataSource
 url: jdbc:mysql://localhost:3306/springbootdata?characterEncoding=utf-
8&serverTimezone=UTC
 username: root
 password: wu7787879
#整合mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
#声明Mybatis映射文件所在的位置

8. 启动类

@SpringBootApplication//使用的Mybatis,扫描com.lagou.mapper@MapperScan("com.lagou.mapper")publicclassSpringbootdemo5Application{publicstaticvoidmain(String[] args){SpringApplication.run(Springbootdemo5Application.class, args);}}

9. 使用Postman测试

image-20220629194938660

image-20220629194942994

10.Spring Boot项目部署

需求:将Spring Boot项目使用maven指令打成jar包并运行测试

分析:

  1. 需要添加打包组件将项目中的资源、配置、依赖包打到一个jar包中;可以使用maven的 package ;
  2. 部署:java -jar 包名

步骤实现:

  • 添加打包组件
<build><plugins><!-- 打jar包时如果不配置该插件,打出来的jar包没有清单文件 --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
  • 部署运行
java -jar 包名

💖💖💖 完结撒花

💖💖💖 如果命运是世上最烂的编剧。 那么你就要争取,做你人生最好的演员

💖💖💖 写作不易,如果您觉得写的不错,欢迎给博主点赞、收藏、评论来一波~让博主更有动力吧


本文转载自: https://blog.csdn.net/qq_41239465/article/details/125528466
版权归原作者 程序员阿红 所有, 如有侵权,请联系我们删除。

“10.SpringBoot实战演练”的评论:

还没有评论