赞
踩
<dependencies> <!-- springboot web starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 如果在子工程/模块指定了 version,则以指定为准 --> </dependency> <!-- 1. starter-actuator 是 springboot 程序的监控系统,可以实现健康检查,info 信息 等 2. 访问 http://localhost:10000/actuator 可以看到相关链接, 还可以做相关设置. --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <!-- 这里我们重新指定一下 version 因为父项目中没有对这个依赖进行版本仲裁--> <version>1.1.13</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
server:
port: 10000 # 配置服务端口
spring:
application:
name: member-service-provider-10000 # 配置服务的名称,名字任意这里与项目名保持一致
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# 别忘记创建数据库之后修改数据库名称
url: jdbc:mysql://localhost:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml # 扫描所有Mapper.xml
type-aliases-package: com.sun.springcloud.entity # 扫描所有实体类,可以直接使用类名来标识
package com.sun.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Description: * * @Author sun * @Create 2024/3/22 12:49 * @Version 1.0 */ @SpringBootApplication public class MemberApplication { public static void main(String[] args) { SpringApplication.run(MemberApplication.class, args); } }
CREATE DATABASE e_commerce_center_db;
USE e_commerce_center_db;
CREATE TABLE member
(
id BIGINT NOT NULL AUTO_INCREMENT COMMENT 'id',
NAME VARCHAR(64) COMMENT '用户名',
pwd CHAR(32) COMMENT '密码',
mobile VARCHAR(20) COMMENT '手机号码',
email VARCHAR(64) COMMENT '邮箱',
gender TINYINT COMMENT '性别', PRIMARY KEY (id)
);
INSERT INTO member VALUES
(NULL, 'smith', MD5('123'), '123456789000', 'smith@sohu.com', 1);
SELECT * from member;
server:
port: 10001 # 配置服务端口
spring:
application:
name: member-service-provider-10001 # 配置服务的名称,名字任意这里与项目名保持一致
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# 别忘记创建数据库之后修改数据库名称
url: 你连接数据库的url
username: sdfsdfsd
password: 88888
mybatis:
mapper-locations: classpath:mapper/*.xml # 扫描所有Mapper.xml
type-aliases-package: com.sun.springcloud.entity # 扫描所有实体类,可以直接使用类名来标识
package com.sun.springcloud.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; /** * Description: * * @Author sun * @Create 2024/3/23 11:10 * @Version 1.0 */ @AllArgsConstructor @NoArgsConstructor @Data //Serializable 加上,后面可能使用 public class Member implements Serializable { private Long id; private String name; private String pwd; private String mobile; private String email; private Integer gender; }
package com.sun.springcloud.util; /** * Description: 用于返回结果, 利于 json 格式 * * @Author sun * @Create 2024/3/23 11:12 * @Version 1.0 */ public class Result<T> { private String code; private String msg; private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Result() { } public Result(T data) { this.data = data; } public static Result success() { Result result = new Result<>(); result.setCode("200"); result.setMsg("success"); return result; } public static <T> Result<T> success(T data) { Result<T> result = new Result<>(data); result.setCode("200"); result.setMsg("success"); return result; } public static <T> Result<T> success(String msg, T data) { Result<T> result = new Result<>(data); result.setCode("200"); result.setMsg(msg); return result; } public static Result error(String code, String msg) { Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } public static <T> Result<T> error(String code, String msg, T data) { Result<T> result = new Result<>(data); result.setCode(code); result.setMsg(msg); return result; } }
package com.sun.springcloud.dao; import com.sun.springcloud.entity.Member; import org.apache.ibatis.annotations.Mapper; /** * Description: 对member表进行操作 * * @Author sun * @Create 2024/3/23 21:06 * @Version 1.0 */ @Mapper // 注入容器 public interface MemberDao { /** * 根据id进行查询 * @param id * @return 查询成功返回Member对象,查询失败返回null */ public Member queryMemberById(Long id); /** * 保存一条记录 * @param member * @return 返回受影响的行数 */ public int save(Member member); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sun.springcloud.dao.MemberDao"> <resultMap id="BaseResultMap" type="Member"> <id column="id" property="id" jdbcType="BIGINT"></id> <id column="name" property="name" jdbcType="VARCHAR"></id> <id column="pwd" property="pwd" jdbcType="VARCHAR"></id> <id column="mobile" property="mobile" jdbcType="VARCHAR"></id> <id column="email" property="email" jdbcType="VARCHAR"></id> <id column="gender" property="gender" jdbcType="TINYINT"></id> </resultMap> <select id="queryMemberById" parameterType="Long" resultMap="BaseResultMap"> select * from `member` where `id` = #{id} </select> <!-- useGeneratedKeys="true" keyProperty="id" 表示插入之后将自增的主键id的值返回到Member对象中 --> <insert id="save" parameterType="Member" useGeneratedKeys="true" keyProperty="id"> INSERT INTO `member` VALUES (NULL, #{name}, MD5(#{pwd}), #{mobile}, #{email}, #{gender}); </insert> </mapper>
package com.sun.springcloud; import com.sun.springcloud.dao.MemberDao; import com.sun.springcloud.entity.Member; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; /** * Description: * * @Author sun * @Create 2024/3/23 21:46 * @Version 1.0 */ @SpringBootTest @Slf4j public class MemberApplicationTest { @Resource private MemberDao memberDao; @Test public void queryMemberById() { Member member = memberDao.queryMemberById(1L); log.info("member={}", member); } @Test public void save() { Member member = new Member(); member.setName("sun"); member.setPwd("12121"); member.setMobile("2342343"); member.setEmail("123@qq.com"); member.setGender(0); int save = memberDao.save(member); Assert.assertEquals(1, save); } }
package com.sun.springcloud.service; import com.sun.springcloud.entity.Member; /** * Description: 对member表进行操作的service * * @Author sun * @Create 2024/3/23 22:02 * @Version 1.0 */ public interface MemberService { /** * 根据id获取一条记录 * * @param id * @return 成功返回member对象,失败返回null */ public Member queryMemberById(Long id); /** * 保存一条记录 * * @param member * @return 成功返回1,失败返回0 */ public int save(Member member); }
package com.sun.springcloud.service.Impl; import com.sun.springcloud.dao.MemberDao; import com.sun.springcloud.entity.Member; import com.sun.springcloud.service.MemberService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Description: member表的service实现类 * * @Author sun * @Create 2024/3/23 22:05 * @Version 1.0 */ @Service // 注入容器 public class MemberServiceImpl implements MemberService { @Resource // 依赖注入memberdao private MemberDao memberDao; @Override public Member queryMemberById(Long id) { return memberDao.queryMemberById(id); } @Override public int save(Member member) { return memberDao.save(member); } }
@Test public void queryMemberById2() { Member member = memberService.queryMemberById(1L); log.info("member={}", member); } @Test public void save2() { Member member = new Member(); member.setName("sun"); member.setPwd("12121"); member.setMobile("2342343"); member.setEmail("123@qq.com"); member.setGender(0); int save = memberService.save(member); Assert.assertEquals(1, save); }
package com.sun.springcloud.controller; import com.sun.springcloud.entity.Member; import com.sun.springcloud.service.MemberService; import com.sun.springcloud.util.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * Description: member表的控制器 * * @Author sun * @Create 2024/3/23 22:24 * @Version 1.0 */ @RestController @Slf4j public class MemberController { @Resource // 注入service private MemberService memberService; /** * 根据前端传进来的json数据插入一条记录 * * @param member json类型的member对象数据 * @return 返回json类型的响应 */ @PostMapping("/member/save") public Result save(@RequestBody Member member) { int save = memberService.save(member); if (save > 0) { return Result.success("添加会员成功!", save); } else { return Result.error("401", "添加会员失败"); } } /** * 根据id来获取某个会员的信息 * * @param id 使用路径参数的形式传入参数 * @return 返回json格式的数据 */ @GetMapping("/member/get/{id}") // 这里使用的路径参数 public Result getMemberById(@PathVariable("id") Long id) { Member member = memberService.queryMemberById(id); if (member != null) { return Result.success("查询成功!", member); } else { return Result.error("402", "ID= " + id + "不存在"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。