版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java架构师-技术专家练习笔记开发工具:idea1、Idea创建project项目(前提:Java环境已经配好Maven已经安装好)1、如果是已经打开的项目:File-New-Project如果是新打开的idea:直接选CreateNewProject创建新项目2、之后选择Maven:3给项目起名字,然后点击【finish】4:创建好的新项目:2、配置文件及添加相关工程1、打开【pom.xml】文件添加打包方式:<packaging>pom</packaging>添加子工程:选中工程-右击:New-Module选择:【Next】填写名称,点击【Finish】子模块添加成功!注意:子模块默认打包方式:jar子模块的层级结构:父类的【pom.xml】中多了<modules><module>common</module></modules>类似第2步,创建【pojo】、【mapper】、【service】【api(即controller)】模块子模块间的相互引用在【pojo】子模块的【pom.xml】中添加:<dependencies><dependency><groupId></groupId><artifactId>common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>在【mapper】模块的【pom.xml】中添加:<dependencies><dependency><groupId></groupId><artifactId>pojo</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>在【service】模块的【pom.xml】中添加:<dependencies><dependency><groupId></groupId><artifactId>mapper</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>在【api】模块的【pom.xml】中添加:<dependencies><dependency><groupId></groupId><artifactId>service</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>即:api(即controller)->service->mapper->pojo->commonMaven打包操作打开maven管理界面显示(root)的即为父项目,根节点。点击【install】进行安装。显示【BUILDSUCCESS】即为构建打包成功设置父项目pom基本配置-相关属性及jar包引用maven配置File->Settings->BuildExecutionDeployment->Maven修改maven地址:引入依赖parent<!--引入依赖parent--><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.1.6.RELEASE</version></parent>设置资源属性:properties文件编码格式File->Settings->Editor->Fileencodings-->设置编码引入依赖dependency<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies>创建启动类在【Java】目录下-右击-【new】-创建【package】包在新建的包下:右击【new】-【JavaClass】创建java文件起名【Application】packagecom.zyx;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.scheduling.annotation.EnableScheduling;importtk.mybatis.spring.annotation.MapperScan;/***@Description*@Date2023-01-06**/@SpringBootApplication@MapperScan(basePackages={"com.zyx.mapper",""})@EnableSchedulingpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}POM文件引入数据源的配置(使用hikariCP)引入mysql包<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency>引入mybatis包<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency>yml文件的相关配置在【api】子模块下-【src】-【Java】-【resourres】-【new】-【file】Application文件配置:#########################web访问端口号约定8088########################server:port:8088tomcat:uri-encoding:UTF-8max-http-header-size:80KB#########################配置数据源信息########################spring:datasource:#数据源的相关配置type:com.zaxxer.hikari.HikariDataSource#数据源类型HikariCPdriver-class-name:com.mysql.jdbc.Driver#mysql驱动url:jdbc:mysql://localhost:16033/zyx?characterEncoding=utf8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNullusername:rootpassword:B@ojia56127!hikari:connection-timeout:30000#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接,则发生SQLException,默认30秒minimum-idle:5#最小连接数maximum-pool-size:20#最大连接数auto-commit:true#自动提交idle-timeout:600000#连接超时的最长时长(毫秒)超时则被释放(retired)默认:10分钟pool-name:DateSourceHikariCP#连接池名字max-lifetime:1800000#连接到生命时长(毫秒)超时而且没被使用则被释放(retired)默认:30分钟1800000msconnection-test-query:SELECT1#连接测试查询语句#######################################mybatis配置######################################mybatis:type-aliases-package:com.zyx.pojo#所有POJO类所在包路径mapper-locations:classpath:mapper/*.xml#mapper映射文件Mybatis逆向生成目录结构:pom.xml配置<dependencies><!--Mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency><!--mapper--><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>4.2.2</version></dependency><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>4.2.0</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.7</version></dependency></dependencies>通用Mapper提供了一些通用的方法,这些通用方法是以接口的形式提供的。com.imooc.my.mapper路径下生成MyMapper接口文件:packagecom.imooc.my.mapper;importmon.Mapper;importmon.MySqlMapper;publicinterfaceMyMapper<T>extendsMapper<T>,MySqlMapper<T>{}注意:mymapper文件,不能被Application扫描到generatorConfig.xml文件例子:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEgeneratorConfigurationPUBLIC"-////DTDMyBatisGeneratorConfiguration1.0//EN""/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><contextid="MysqlContest"targetRuntime="MyBatis3Simple"defaultModelType="flat"><propertyname="javaFileEncoding"value="UTF-8"/><propertyname="beginningDelimiter"value="`"/><propertyname="endingDelimiter"value="`"/><!--通用mapper所在目录自己创建的MyMapper文件路径:com.zyx.mapper--><plugintype="tk.mybatis.mapper.generator.MapperPlugin"><propertyname="mappers"value="com.imooc.my.mapper.MyMapper"/></plugin><!--添加:useSSL=false解决:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障--><jdbcConnectiondriverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://localhost:16033/zyx-dev?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false"userId="root"password="B@ojia56127!"></jdbcConnection><!--对应生成的pojo所在包--><javaModelGeneratortargetPackage="com.imooc.pojo"targetProject="src/main/java"></javaModelGenerator><!--对应生成mapper所在目录--><sqlMapGeneratortargetPackage="mapper"targetProject="src/main/resources"></sqlMapGenerator><!--配置mapper对应的Java映射--><javaClientGeneratortargetPackage="com.imooc.mapper"targetProject="src/main/java"type="XMLMAPPER"></javaClientGenerator><!--修改表名:tableName/指定数据库下的表名:catalog="zyx-dev"(zyx-dev数据库)--><tablecatalog="zyx-dev"tableName="users"></table></context></generatorConfiguration>逆向生成的代码,代码启动package.imooc.mybatis.utils;importorg.mybatis.generator.api.MyBatisGenerator;importorg.mybatis.generator.config.Configuration;importorg.mybatis.generator.config.xml.ConfigurationParser;importernal.DefaultShellCallback;importjava.io.File;importjava.util.ArrayList;importjava.util.List;publicclassGeneratorDisplay{publicvoidgenerator()throwsException{List<String>warnings=newArrayList<>();booleanoverwrite=true;//FileconfigFile=newFile("generatorConfig.xml");ConfigurationParsercp=newConfigurationParser(warnings);Configurationconfig=cp.parseConfiguration(configFile);DefaultShellCallbackcallback=newDefaultShellCallback(overwrite);MyBatisGeneratormyBatisGenerator=newMyBatisGenerator(config,callback,warnings);myBatisGenerator.generate(null);}publicstaticvoidmain(String[]args){try{GeneratorDisplaygeneratorDisplay=newGeneratorDisplay();generatorDisplay.generator();}catch(Exceptione){e.printStackTrace();}}}Junit测试配置pom.xml配置:<!--添加junit测试包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><!--作用域--><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>测试类:TrabsTestimportcom.zyx.Application;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(classes=Application.class)publicclassTrabsTest{@TestpublicvoidmyTest(){System.out.println("测试类!");}}Springboot事务(Transactional)的传播PropagationREQUIRED使用当前的事务,如果当前没有事务,则自己新建一个事务,子方法是必须运行再一个事务中的;如果当前存在事务,则加入这个事务,成为一个整体。常用于:增删改。举例:领导没饭吃,我有钱,我会自己买了自己吃;领导有的吃,会分给我一起吃。SUPPORTS如果当前有事务,则使用事务;如果当前没有事务,则不使用事务。常用于查询。举例:领导没饭吃,我也没饭吃,领导有饭吃,我也有饭吃。MANDATORY该传播属性强制必须存在一个事务,如果不存在,则抛出异常。举例:领导必须管饭,不管饭就没饭吃,我就不乐意了,就不干了(抛出异常)REQUIRES_NEW如果当前有事务,则挂起该事务,并且自己创建一个新的事务,给自己用。如果当前没有事务,则同REQUIRED举例:领导有饭吃,我偏不要,我自己买饭吃。NOT_SUPPORTED如果当前有事务,则把事务挂起,自己不适用事务去运行数据库操作。举例:领导有饭吃,分了一点给我,但我太忙了,放一边,我不吃。NEVER如果当前有事务存在,则抛出异常。举例:领导有饭吃,我不想吃,我热爱工作。(抛出异常)NESTED如果当前有事务,则开启子事务(嵌套事务),嵌套事务是独立提交或者回滚;如果当前没有事务,则同REQUIRED但是如果主事务提交,则会携带子事务一起提交。如果主事务回滚,则子事务会一起回滚,相反,子事务异常,则父事务可以回滚或不回滚。(取决于父事务)举例:领导决策不对,领导怪罪,领导带着小弟一同受罪。小弟出了差错,领导可以推卸责任,或一起承担。注册登录流程方法:用户名是否存在自定义相应数据:IMOOCJSONResult类(common下的工具类)packagecom.imooc.utils;importcom.fasterxml.jackson.annotation.JsonIgnore;importcom.fasterxml.jackson.databind.ObjectMapper;/***@Description*@Authorzhangxin*@Date20230112**/publicclassIMOOCJSONResult{//定义jackson对象privatestaticfinalObjectMapperMAPPER=newObjectMapper();//相应业务状态privateIntegerstatus;//相应消息privateStringmsg;//相应中的数据privateObjectdata;@JsonIgnoreprivateStringok;//不使用publicIMOOCJSONResult(){}publicIMOOCJSONResult(Integerstatus,Stringmsg,Objectdata){this.status=status;this.msg=msg;this.data=data;}publicIMOOCJSONResult(Integerstatus,Stringmsg,Objectdata,Stringok){this.status=status;this.msg=msg;this.data=data;this.ok=ok;}publicIMOOCJSONResult(Objectdata){this.status=200;this.msg="OK";this.data=data;}publicstaticIMOOCJSONResultbuild(Integerstatus,Stringmsg,Objectdata){returnnewIMOOCJSONResult(status,msg,data);}publicstaticIMOOCJSONResultbuild(Integerstatus,Stringmsg,Objectdata,Stringok){returnnewIMOOCJSONResult(status,msg,data,ok);}publicstaticIMOOCJSONResultok(){returnnewIMOOCJSONResult(null);}publicstaticIMOOCJSONResulterrorMsg(Stringmsg){returnnewIMOOCJSONResult(500,msg,null);}//=====================get/set方法publicstaticObjectMappergetMAPPER(){returnMAPPER;}publicIntegergetStatus(){returnstatus;}publicvoidsetStatus(Integerstatus){this.status=status;}publicStringgetMsg(){returnmsg;}publicvoidsetMsg(Stringmsg){this.msg=msg;}publicObjectgetData(){returndata;}publicvoidsetData(Objectdata){this.data=data;}publicStringgetOk(){returnok;}publicvoidsetOk(Stringok){this.ok=ok;}}实体类:Users类(自动生成的,注意@Table(name="users")注解)packagecom.imooc.pojo;importjavax.persistence.Column;importjavax.persistence.Id;importjavax.persistence.Table;importjava.util.Date;/***表名:zyx-dev..users*表注释:用户表*/@Table(name="users")publicclassUsers{/***主键id用户id*/@IdprivateStringid;/***用户名用户名*/privateStringusername;/***密码密码*/privateStringpassword;/***昵称昵称*/privateStringnickname;/***真实姓名真实姓名*/privateStringrealname;/***头像头像*/privateStringface;/***手机号手机号*/privateStringmobile;/***邮箱地址邮箱地址*/privateStringemail;/***性别性别*/privateIntegersex;/***生日生日*/privateDatebirthday;/***创建时间创建时间*/@Column(name="created_time")privateDatecreatedTime;/***更新时间更新时间*/@Column(name="updated_time")privateDateupdatedTime;/***获取主键id用户id**@returnid-主键id用户id*/publicStringgetId(){returnid;}/***设置主键id用户id**@paramid主键id用户id*/publicvoidsetId(Stringid){this.id=id;}/***获取用户名用户名**@returnusername-用户名用户名*/publicStringgetUsername(){returnusername;}/***设置用户名用户名**@paramusername用户名用户名*/publicvoidsetUsername(Stringusername){this.username=username;}/***获取密码密码**@returnpassword-密码密码*/publicStringgetPassword(){returnpassword;}/***设置密码密码**@parampassword密码密码*/publicvoidsetPassword(Stringpassword){this.password=password;}/***获取昵称昵称**@returnnickname-昵称昵称*/publicStringgetNickname(){returnnickname;}/***设置昵称昵称**@paramnickname昵称昵称*/publicvoidsetNickname(Stringnickname){this.nickname=nickname;}/***获取真实姓名真实姓名**@returnrealname-真实姓名真实姓名*/publicStringgetRealname(){returnrealname;}/***设置真实姓名真实姓名**@paramrealname真实姓名真实姓名*/publicvoidsetRealname(Stringrealname){this.realname=realname;}/***获取头像头像**@returnface-头像头像*/publicStringgetFace(){returnface;}/***设置头像头像**@paramface头像头像*/publicvoidsetFace(Stringface){this.face=face;}/***获取手机号手机号**@returnmobile-手机号手机号*/publicStringgetMobile(){returnmobile;}/***设置手机号手机号**@parammobile手机号手机号*/publicvoidsetMobile(Stringmobile){this.mobile=mobile;}/***获取邮箱地址邮箱地址**@returnemail-邮箱地址邮箱地址*/publicStringgetEmail(){returnemail;}/***设置邮箱地址邮箱地址**@paramemail邮箱地址邮箱地址*/publicvoidsetEmail(Stringemail){this.email=email;}/***获取性别性别**@returnsex-性别性别*/publicIntegergetSex(){returnsex;}/***设置性别性别**@paramsex性别性别*/publicvoidsetSex(Integersex){this.sex=sex;}/***获取生日生日**@returnbirthday-生日生日*/publicDategetBirthday(){returnbirthday;}/***设置生日生日**@parambirthday生日生日*/publicvoidsetBirthday(Datebirthday){this.birthday=birthday;}/***获取创建时间创建时间**@returncreatedTime-创建时间创建时间*/publicDategetCreatedTime(){returncreatedTime;}/***设置创建时间创建时间**@paramcreatedTime创建时间创建时间*/publicvoidsetCreatedTime(DatecreatedTime){this.createdTime=createdTime;}/***获取更新时间更新时间**@returnupdatedTime-更新时间更新时间*/publicDategetUpdatedTime(){returnupdatedTime;}/***设置更新时间更新时间**@paramupdatedTime更新时间更新时间*/publicvoidsetUpdatedTime(DateupdatedTime){this.updatedTime=updatedTime;}}Mapper类(自动生成的)packagecom.imooc.mapper;importcom.imooc.my.mapper.MyMapper;importcom.imooc.pojo.Users;publicinterfaceUsersMapperextendsMyMapper<Users>{}Mapper对应的XML(自动生成的)<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN""/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.imooc.mapper.UsersMapper"><resultMapid="BaseResultMap"type="com.imooc.pojo.Users"><!--WARNING-@mbg.generated--><idcolumn="id"jdbcType="VARCHAR"property="id"/><resultcolumn="username"jdbcType="VARCHAR"property="username"/><resultcolumn="password"jdbcType="VARCHAR"property="password"/><resultcolumn="nickname"jdbcType="VARCHAR"property="nickname"/><resultcolumn="realname"jdbcType="VARCHAR"property="realname"/><resultcolumn="face"jdbcType="VARCHAR"property="face"/><resultcolumn="mobile"jdbcType="VARCHAR"property="mobile"/><resultcolumn="email"jdbcType="VARCHAR"property="email"/><resultcolumn="sex"jdbcType="INTEGER"property="sex"/><resultcolumn="birthday"jdbcType="DATE"property="birthday"/><resultcolumn="created_time"jdbcType="TIMESTAMP"property="createdTime"/><resultcolumn="updated_time"jdbcType="TIMESTAMP"property="updatedTime"/></resultMap></mapper>Service接口:packagecom.imooc.service;/***@Description*@Authorzhangxin*@Date20230112**/publicinterfaceUserService{/***功能描述:判断用户名是否存在**@returnvoid*@date2023/1/12*@params*/publicbooleanqueryUserNameIsExist(StringuserName);}Service实现类packagecom.imooc.service.impl;importcom.imooc.mapper.UsersMapper;importcom.imooc.pojo.Users;importcom.imooc.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;importtk.mybatis.mapper.entity.Example;/***@Description*@Authorzhangxin*@Date20230112**/@ServicepublicclassUserServiceImplimplementsUserService{@AutowiredprivateUsersMapperusersMapper;@Transactional(propagation=Propagation.SUPPORTS)@OverridepublicbooleanqueryUserNameIsExist(StringuserName){ExampleuserExample=newExample(Users.class);Example.CriteriauserCriteria=userExample.createCriteria();userCriteria.andEqualTo("username",userName);Usersresult=usersMapper.selectOneByExample(userExample);returnresult==null?false:true;}}Api接口packagecom.imooc.controller;importcom.imooc.service.UserService;importcom.imooc.utils.IMOOCJSONResult;importmons.lang3.StringUtils;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/***@Description*@Authorzhangxin*@Date20230112**/@RestController@RequestMapping("passport")publicclassPassportController{@AutowiredprivateUserServiceuserService;@GetMapping("/usernameIsExist")publicIMOOCJSONResultusernameIsExist(@RequestParamStringuserName){//1、判断用户名不能为空if(StringUtils.isBlank(userName)){returnIMOOCJSONResult.errorMsg("用户名不能为空!");}//2、查询用户名是否存在booleanisExist=userService.queryUserNameIsExist(userName);if(isExist){returnIMOOCJSONResult.errorMsg("查询用户名已经存在!");}returnIMOOCJSONResult.ok();}}PostMan测试:localhost:8088/passport/usernameIsExist?userName=imooc方法:添加用户公共工具:common层自定义相应数据结构IMOOCJSONResult类packagecom.imooc.utils;importcom.fasterxml.jackson.annotation.JsonIgnore;importcom.fasterxml.jackson.databind.ObjectMapper;/***@Description*@Authorzhangxin*@Date20230112**/publicclassIMOOCJSONResult{//定义jackson对象privatestaticfinalObjectMapperMAPPER=newObjectMapper();//相应业务状态privateIntegerstatus;//相应消息privateStringmsg;//相应中的数据privateObjectdata;@JsonIgnoreprivateStringok;//不使用publicIMOOCJSONResult(){}publicIMOOCJSONResult(Integerstatus,Stringmsg,Objectdata){this.status=status;this.msg=msg;this.data=data;}publicIMOOCJSONResult(Integerstatus,Stringmsg,Objectdata,Stringok){this.status=status;this.msg=msg;this.data=data;this.ok=ok;}publicIMOOCJSONResult(Objectdata){this.status=200;this.msg="OK";this.data=data;}publicstaticIMOOCJSONResultbuild(Integerstatus,Stringmsg,Objectdata){returnnewIMOOCJSONResult(status,msg,data);}publicstaticIMOOCJSONResultbuild(Integerstatus,Stringmsg,Objectdata,Stringok){returnnewIMOOCJSONResult(status,msg,data,ok);}publicstaticIMOOCJSONResultok(){returnnewIMOOCJSONResult(null);}publicstaticIMOOCJSONResulterrorMsg(Stringmsg){returnnewIMOOCJSONResult(500,msg,null);}//=====================get/set方法publicstaticObjectMappergetMAPPER(){returnMAPPER;}publicIntegergetStatus(){returnstatus;}publicvoidsetStatus(Integerstatus){this.status=status;}publicStringgetMsg(){returnmsg;}publicvoidsetMsg(Stringmsg){this.msg=msg;}publicObjectgetData(){returndata;}publicvoidsetData(Objectdata){this.data=data;}publicStringgetOk(){returnok;}publicvoidsetOk(Stringok){this.ok=ok;}}调用方法:IMOOCJSONResult.errorMsg("XXX");MD5加密类:MD5Utils类packagecom.imooc.utils;importmons.codec.binary.Base64;importjava.nio.charset.StandardCharsets;importjava.security.MessageDigest;/***@DescriptionMD5加密*@Authorzhangxin*@Date20230112**/publicclassMD5Utils{/***功能描述:对字符串进行MD5加密*@authorzhangxin*@date2023/1/12*@params*@returnjava.lang.String*/publicstaticStringgetMD5Str(StringstrValue)throwsException{MessageDigestmd5=MessageDigest.getInstance("MD5");Stringnewstr=Base64.encodeBase64String(md5.digest(strValue.getBytes(StandardCharsets.UTF_8)));returnnewstr;}publicstaticvoidmain(String[]args){try{Stringmd5=getMD5Str("aaa");System.out.println(md5);}catch(Exceptione){e.printStackTrace();}}}调用方法:MD5Utils.getMD5Str(XXX)枚举:Sex类packagecom.imooc.enums;/***@Description性别枚举*@Authorzhangxin*@Date20230113**/publicenumSex{woman(0,"女"),man(1,"男"),secret(2,"保密");publicfinalIntegertype;publicfinalStringvalue;Sex(Integertype,Stringvalue){this.type=type;this.value=value;}}调用方法:Sex.secret.type添加用户:校验的实体类:UsersBOpackagecom.imooc.pojo.bo;/***@Description接收前端传入的json*@Authorzhangxin*@Date20230112**/publicclassUsersBO{publicStringusername;privateStringpassword;privateStringconfirmPassword;publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetConfirmPassword(){returnconfirmPassword;}publicvoidsetConfirmPassword(StringconfirmPassword){this.confirmPassword=confirmPassword;}}Service接口packagecom.imooc.service;importcom.imooc.pojo.Users;importcom.imooc.pojo.bo.UsersBO;/***@Description*@Authorzhangxin*@Date20230112**/publicinterfaceUserService{/***功能描述:判断用户名是否存在**@returnvoid*@date2023/1/12*@params*/publicbooleanqueryUserNameIsExist(StringuserName);/***功能描述:创建用户*@authorzhangxin*@date2023/1/12*@paramsUsersBO传入的json对象*@returncom.imooc.pojo.Users*/publicUserscreateUser(UsersBOusersBO);}Service实现类:packagecom.imooc.service.impl;importcom.imooc.enums.Sex;importcom.imooc.mapper.UsersMapper;importcom.imooc.pojo.Users;importcom.imooc.pojo.bo.UsersBO;importcom.imooc.service.UserService;importcom.imooc.utils.MD5Utils;importorg.n3r.idworker.Sid;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;importtk.mybatis.mapper.entity.Example;importjava.util.Date;importjava.util.UUID;/***@Description*@Authorzhangxin*@Date20230112**/@ServicepublicclassUserServiceImplimplementsUserService{@AutowiredprivateUsersMapperusersMapper;/*@AutowiredprivateSidsid;*///创建用户,默认图片privatestaticfinalStringUSER_FACE="";@Transactional(propagation=Propagation.SUPPORTS)@OverridepublicbooleanqueryUserNameIsExist(StringuserName){ExampleuserExample=newExample(Users.class);Example.CriteriauserCriteria=userExample.createCriteria();userCriteria.andEqualTo("username",userName);Usersresult=usersMapper.selectOneByExample(userExample);returnresult==null?false:true;}@Transactional(propagation=Propagation.REQUIRED)@OverridepublicUserscreateUser(UsersBOusersBO){Sidsid=newSid();Usersuser=newUsers();//user.setId(sid.nextShort());user.setId(UUID.randomUUID().toString().replace("-",""));user.setUsername(usersBO.getUsername());try{user.setPassword(MD5Utils.getMD5Str(usersBO.getPassword()));}catch(Exceptione){e.printStackTrace();}user.setNickname(usersBO.getUsername());user.setFace(USER_FACE);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年度临时仓库租赁与装修合同
- 化工厂合伙经营合同范本
- 2024年度版权购买合同:游戏软件
- 境外快递运输合同范本
- 2024年包装袋购销合同
- 买卖锅炉合同范本
- 二零二四年度新材料批量生产与销售合同
- 跨国并购合同范本
- 饼干厂课程设计
- 餐馆设计面食方案
- 2024年统编版七年级上册道德与法治 第三单元 珍爱我们的生命 第八课 生命可贵 第2课时 敬畏生命 教学课件
- IATF16949组织环境因素识别表
- 2024年共青团入团积极分子考试试卷题库及答案
- 2024年健康管理师实操
- 2024智能变电站新一代集控站设备监控系统技术规范部分
- 职业素质养成(吉林交通职业技术学院)智慧树知到答案2024年吉林交通职业技术学院
- 企业反恐专项经费保障制度
- 5.5《方程的意义》(课件)-2024-2025学年人教版数学五年级上册
- 普通话训练手册读书札记
- 部编版二年级语文上册看拼音写词语含答案
- 2024年浙江省应急管理行政执法竞赛题库-上(单选、多选题)
评论
0/150
提交评论