文章目录 1.环境配置 1.创建会员中心模块 2.检查父子模块的pom.xml 1.父模块 注意:如果父模块中的依赖显示not found,原因是子模块并没有引用,不用在意 2.子模块 3.pom.xml 引入相关依赖(别忘记刷新maven) 4.application.yml 进行基本配置 5.com/sun/springcloud/MemberApplication.java 编写主程序测试 6.出现两个问题 1.端口冲突,所以这里换一个端口为10001,将子模块名也一并修改! 2.数据库没创建 7.使用云数据库 1.数据库表设计 2.修改application.yml 的port,name以及数据库配置 3.执行主程序测试 2.代码编写 1.com/sun/springcloud/entity/Member.java 创建实体类 2.com/sun/springcloud/util/Result.java 封装响应结果的工具类 3.编写dao层 1.com/sun/springcloud/dao/MemberDao.java 编写Mapper接口并注入容器 2.mapper/MemberMapper.xml 编写Mapper实现类 3.com/sun/springcloud/MemberApplicationTest.java 测试dao层 4.编写service层 1.com/sun/springcloud/service/MemberService.java 编写service接口 2.com/sun/springcloud/service/Impl/MemberServiceImpl.java 编写service实现类并注入容器 3.测试service层 5.编写controller层 1.com/sun/springcloud/controller/MemberController.java 编写controller 2.postman测试
1.环境配置
1.创建会员中心模块
2.检查父子模块的pom.xml
1.父模块
注意:如果父模块中的依赖显示not found,原因是子模块并没有引用,不用在意
2.子模块
3.pom.xml 引入相关依赖(别忘记刷新maven)
< dependencies> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-web</ artifactId> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-actuator</ artifactId> </ dependency> < dependency> < groupId> org.mybatis.spring.boot</ groupId> < artifactId> mybatis-spring-boot-starter</ artifactId> </ dependency> < dependency> < groupId> com.alibaba</ groupId> < artifactId> druid-spring-boot-starter</ artifactId> < version> 1.1.13</ version> </ dependency> < dependency> < groupId> mysql</ groupId> < artifactId> mysql-connector-java</ artifactId> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-jdbc</ artifactId> </ dependency> < dependency> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId> < optional> true</ optional> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-test</ artifactId> < scope> test</ scope> </ dependency> </ dependencies>
4.application.yml 进行基本配置
server : port : 10000
spring : application : name : member- service- provider- 10000 datasource : type : com.alibaba.druid.pool.DruidDataSourceurl : jdbc: mysql: //localhost: 3306/xxx? useSSL=false&useUnicode=true&characterEncoding=UTF- 8 username : rootpassword : root
mybatis : mapper-locations : classpath: mapper/*.xml type-aliases-package : com.sun.springcloud.entity
5.com/sun/springcloud/MemberApplication.java 编写主程序测试
package com. sun. springcloud ; import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@SpringBootApplication
public class MemberApplication { public static void main ( String [ ] args) { SpringApplication . run ( MemberApplication . class , args) ; }
}
6.出现两个问题
1.端口冲突,所以这里换一个端口为10001,将子模块名也一并修改!
2.数据库没创建
7.使用云数据库
1.数据库表设计
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;
2.修改application.yml 的port,name以及数据库配置
server:port: 10001
spring:application:name: member- service- provider- 10001 datasource:type : com. alibaba. druid. pool. DruidDataSourceurl: 你连接数据库的urlusername: sdfsdfsdpassword: 88888
mybatis:mapper- locations: classpath:mapper/ * . xml type - aliases- package: com. sun. springcloud. entity
3.执行主程序测试
2.代码编写
1.com/sun/springcloud/entity/Member.java 创建实体类
package com. sun. springcloud. entity ; import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ; import java. io. Serializable ; @AllArgsConstructor
@NoArgsConstructor
@Data
public class Member implements Serializable { private Long id; private String name; private String pwd; private String mobile; private String email; private Integer gender;
}
2.com/sun/springcloud/util/Result.java 封装响应结果的工具类
package com. sun. springcloud. util ; 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; }
}
3.编写dao层
1.com/sun/springcloud/dao/MemberDao.java 编写Mapper接口并注入容器
package com. sun. springcloud. dao ; import com. sun. springcloud. entity. Member ;
import org. apache. ibatis. annotations. Mapper ;
@Mapper
public interface MemberDao { public Member queryMemberById ( Long id) ; public int save ( Member member) ;
}
2.mapper/MemberMapper.xml 编写Mapper实现类
<?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> < insert id = " save" parameterType = " Member" useGeneratedKeys = " true" keyProperty = " id" > INSERT INTO `member` VALUES(NULL, #{name}, MD5(#{pwd}), #{mobile}, #{email}, #{gender});</ insert>
</ mapper>
3.com/sun/springcloud/MemberApplicationTest.java 测试dao层
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 ;
@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) ; }
}
4.编写service层
1.com/sun/springcloud/service/MemberService.java 编写service接口
package com. sun. springcloud. service ; import com. sun. springcloud. entity. Member ;
public interface MemberService { public Member queryMemberById ( Long id) ; public int save ( Member member) ; }
2.com/sun/springcloud/service/Impl/MemberServiceImpl.java 编写service实现类并注入容器
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 ;
@Service
public class MemberServiceImpl implements MemberService { @Resource private MemberDao memberDao; @Override public Member queryMemberById ( Long id) { return memberDao. queryMemberById ( id) ; } @Override public int save ( Member member) { return memberDao. save ( member) ; }
}
3.测试service层
@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) ; }
5.编写controller层
1.com/sun/springcloud/controller/MemberController.java 编写controller
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 ;
@RestController
@Slf4j
public class MemberController { @Resource private MemberService memberService; @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" , "添加会员失败" ) ; } } @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 + "不存在" ) ; } }
}
2.postman测试