目录
一、实现功能:
二、实战部署:
2.1 Android项目详解:
2.1.1 Android定义返回实体类(如是返回基本类型如int String Boolean 等无需建类)
2.1.2 Android定义接口
2.1.3 Android定义配置文件,设置IDEA电脑主机IP(localhost)以及端口号
2.1.4 Android使用Retrofit框架进行网络请求,并使用工具类进行日志拦截
2.1.5 调用接口进行数据请求
2.2 IDEA项目详解
2.2.1 项目结构:
2.2.2 Controller层/DoctortestController.java
2.2.3 dao层/Doctortest.java
2.2.4 mapper层/DoctortestMapper.java
2.2.5 service层/impl/DoctortestServiceImpl.java
2.2.6 service层/IDoctortestService.java
2.2.7 代码生成器Code,修改参数
2.2.8 MybatisPlusConfig
2.2.9 DoctortestMapper.xml
2.2.10 application.properties
2.2.11 依赖pom.xml文件:
3.运行结果
4.数据库表结构
一、实现功能:
框架思路:A电脑部署Android项目,B电脑部署IDEA后台,Android向IDEA发送参数M,在IDEA中根据接收参数M对Mysql数据库进行查询处理,将结果(参数N)返回给Android
实战逻辑:Android输入手机号Phone,和密码Password,将两个参数的值传输到IDEA项目,IDEA项目根据手机号查询,如果查询到记录,则返回true表示已注册,如果未查询到记录,则返回false(Android项目code、IDEA项目code、数据库结构以及注意问题 都放进去了)
二、实战部署:
2.1 Android项目详解:
2.1.1 Android定义返回实体类(如是返回基本类型如int String Boolean 等无需建类)
注:由于下面案例返回的是Boolean值,没用上这个实体类,但是返回的是对象类型就需要建一个代码如下(示例):
2.1.2 Android定义接口
doctortest :IDEA项目中的Controller层定义的路径,对应下面红框部分
findDoctoByNumber :IDEA项目中的Controller类中的方法注解路径,如下图
代
package com.hncum.doctortest.dao;
import com.hncum.doctortest.entity.DoctorTest;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
//测试 医生端 接口集合
public interface API {
//医生端登录接口
@FormUrlEncoded
@POST("doctortest/findDoctoByNumber")
Call<Boolean> getDoctor(@Field("doctorPhone") String doctorPhone, @Field("doctorPassword") String doctorPassword);
}
2.1.3 Android定义配置文件,设置IDEA电脑主机IP(localhost)以及端口号
package com.hncum.doctortest;
public class AppDataConfig {
//使用公网 IP地址 处于统一局域网中
public static String localhost = " 公共网络IP地址 ";
//9000 端口号
public static String getURL(){
return "http://"+localhost+":9000/";
}
}
注:此处IP地址 需要两部电脑同时连接一个热点之后(最好别用校园网,可以自己开热点),然后在IDEA项目的电脑上打开
设置==》网络和Internet==》WLAN==》第二行点进去 翻到最下面 复制 IPv4地址 对应的IP 放到Android项目中 localhost,切记 需两台电脑连接同一个热点后再复制对应IP地址,而且每次重新连接网络都会更改此地址
2.1.4 Android使用Retrofit框架进行网络请求,并使用工具类进行日志拦截
Retrofit工具类进行日志拦截:
package com.hncum.doctortest;
import android.util.Log;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUtils {
public static Retrofit getRetrofit(String url) {
//日志显示级别
HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
//新建log拦截器
HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.d("RetrofitMessage","OkHttp====Message:"+message);
}
});
loggingInterceptor.setLevel(level);
//定制OkHttp
OkHttpClient.Builder httpClientBuilder = new OkHttpClient
.Builder();
//OkHttp进行添加拦截器loggingInterceptor
httpClientBuilder.addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client( httpClientBuilder.build())
.build();
return retrofit;
}
}
Android添加Retrofit+GSON依赖:
//retrofit+gson+网络请求
implementation("com.squareup.okhttp3:okhttp:4.9.3")
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
//retrofit+gson+网络请求
2.1.5 调用接口进行数据请求
package com.hncum.doctortest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.hncum.doctortest.dao.API;
import com.hncum.doctortest.entity.DoctorTest;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
/**
* 进行逻辑处理
* 使用Retrofit框架进行处理: Retrofit进行接口封装 OkHttp进行网络请求
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* AppDataConfig.getURL()) 网络请求网址
*/
API api = RetrofitUtils.getRetrofit(AppDataConfig.getURL()).create(API.class);
//Boolean 返回值类型
api.getDoctor("12345","1212").enqueue(new Callback<Boolean>() {
//请求成功
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
System.out.println("已成功请求");
System.out.println(response.body()+"请求成功");
}
//请求失败后
@Override
public void onFailure(Call<Boolean> call, Throwable t) {
System.out.println("请求失败");
//返回错误信息
System.out.println(t.getMessage());
}
});
}
}
2.2 IDEA项目详解
2.2.1 项目结构:
2.2.2 Controller层/DoctortestController.java
package com.example.doctortestmodel.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.doctortestmodel.dao.Doctortest;
import com.example.doctortestmodel.service.IDoctortestService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
* @since 2022-11-05
*/
@RestController
@CrossOrigin
@Api(tags = "DoctortestController医生登陆控制器")
@RequestMapping("/doctortest")
public class DoctortestController {
@Autowired
IDoctortestService iDoctortestService;
/**
*
* @param phone_number
* @param doctorPassword
* @return Boolean
*/
@PostMapping("findDoctoByNumber")
private Boolean findDoctoByNumber(@RequestParam(name = "doctorPhone") String phone_number,
@RequestParam(name = "doctorPassword") String doctorPassword) {
QueryWrapper<Doctortest> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("doctorPhone", phone_number);
List<Doctortest> list = new ArrayList<>();
System.out.println(111);
list = iDoctortestService.getBaseMapper().selectList(queryWrapper);
if(list.size()!=1)
return false;
if(list.get(0).getDoctorPassword().equals(doctorPassword))
return true;
return false;
}
}
2.2.3 dao层/Doctortest.java
package com.example.doctortestmodel.dao;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
/**
* <p>
*
* </p>
* @since 2022-11-05
*/
@Data
@ApiModel(value = "Doctortest对象", description = "")
@TableName("doctortest")
public class Doctortest implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "doctorPhone")
private String doctorPhone;
@TableField(value="doctorPassword")
private String doctorPassword;
public String getDoctorPhone() {
return doctorPhone;
}
public void setDoctorPhone(String doctorPhone) {
this.doctorPhone = doctorPhone;
}
public String getDoctorPassword() {
return doctorPassword;
}
public void setDoctorPassword(String doctorPassword) {
this.doctorPassword = doctorPassword;
}
@Override
public String toString() {
return "Doctortest{" +
"doctorPhone=" + doctorPhone +
", doctorPassword=" + doctorPassword +
"}";
}
}
2.2.4 mapper层/DoctortestMapper.java
package com.example.doctortestmodel.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.doctortestmodel.dao.Doctortest;
import org.apache.ibatis.annotations.Mapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author yqifei
* @since 2022-11-05
*/
@Mapper
public interface DoctortestMapper extends BaseMapper<Doctortest> {
}
2.2.5 service层/impl/DoctortestServiceImpl.java
package com.example.doctortestmodel.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.doctortestmodel.dao.Doctortest;
import com.example.doctortestmodel.mapper.DoctortestMapper;
import com.example.doctortestmodel.service.IDoctortestService;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author yqifei
* @since 2022-11-05
*/
@Service
public class DoctortestServiceImpl extends ServiceImpl<DoctortestMapper, Doctortest> implements IDoctortestService {
}
2.2.6 service层/IDoctortestService.java
package com.example.doctortestmodel.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.doctortestmodel.dao.Doctortest;
/**
* <p>
* 服务类
* </p>
*
* @author yqifei
* @since 2022-11-05
*/
public interface IDoctortestService extends IService<Doctortest> {
}
2.2.7 代码生成器Code,修改参数
package com.example.doctortestmodel.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.Collections;
public class GenerateCode {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://1.117.225.144:3306/renren_security?serverTimezone=UTC", "root", "83f67c3ec322f2b0")
.globalConfig(builder -> {
builder.author(" ") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D://data//"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.example_login.demo") // 设置父包名
// .moduleName("idcard") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://data//")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("doctortest") // 设置需要生成的表名
.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
2.2.8 MybatisPlusConfig
package com.example.doctortestmodel.utils;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInnerInterceptor(){
return new PaginationInterceptor();
}
}
DoctorTestModelApplication
package com.example.doctortestmodel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DoctorTestModelApplication {
public static void main(String[] args) {
SpringApplication.run(DoctorTestModelApplication.class, args);
}
}
2.2.9 DoctortestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.huncm.com.example_login.demo.mapper.DoctortestMapper">
</mapper>
2.2.10 application.properties
# åºç¨å称
spring.application.name=DoctorTestModel
# åºç¨æå¡ WEB 访é®ç«¯å£
server.port=9000
#ä¸é¢è¿äºå容æ¯ä¸ºäºè®©MyBatisæ å°
#æå®MybatisçMapperæ件
mybatis.mapper-locations=classpath:mappers/*xml
#æå®Mybatisçå®ä½ç®å½
mybatis.type-aliases-package=com.example.doctortestmodel.DoctorTestModelApplication
# æ°æ®åºé©±å¨ï¼
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# æ°æ®æºå称
spring.datasource.name=defaultDataSource
# æ°æ®åºè¿æ¥å°å
# localhost 对应连接热点后的IP地址 3306为端口号 renren_security 数据库名
spring.datasource.url=jdbc:mysql://localhost:3306/renren_security?serverTimezone=UTC
# æ°æ®åºç¨æ·å&å¯ç ï¼
spring.datasource.username=root
spring.datasource.password=83f67c3ec322f2b0
# å¤è·¯å¾éç½®
spring.resources.static-locations=file:/home/dachuang
# å¼å¯æ¥å¿
logging.level.com.hnucm.xiaotang = debug
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
spring.servlet.multipart.max-request-size=200MB
spring.servlet.multipart.max-file-size=200MB
2.2.11 依赖pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>DoctorTestModel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DoctorTestModel</name>
<description>DoctorTestModel</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- swagger start-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<!-- swagger end-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
<!-- 对应自己的数据库版本 -->
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.doctortestmodel.DoctorTestModelApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.运行结果
3.1 两个项目部署好之后即可运行,IDEA项目运行后,Android发起请求并打印输出结果
3.2 在写好IDEA项目后,可用APIFOX进行调试,测试接口是否正确返回数据(APIFOX在线调试工具,可看这一篇 APIFOX在线调试工具 有图有实例)
3.3 如若Android项目显示连接超时,请关掉两台电脑主机的公网防火墙
4.数据库表结构
文章来源:https://www.toymoban.com/news/detail-780592.html
文章来源地址https://www.toymoban.com/news/detail-780592.html
到了这里,关于Android+IDEA后台+MySQL数据库数据交互实战案例(A电脑:Android B电脑:IDEA后台)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!