Git Product home page Git Product logo

androidspringboot's Introduction

AndroidSpringBoot

SpringBoot Connect To MySql

自动生成表

@Entity 用Entity修饰实体类并同时根据类中的成员变数生成表与列 表名自动以类名显示

要达到以上操作还需要以下几点

  1. application.properties文件中的设置必须要有以下几点
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
  1. 启动类
@EntityScan(basePackages = ["com.example.DIM.model"])

Android Connect To SpringBoot Use RetrofitLibrary(レトロフィット)

実現に必要なClass Or Interface**

1 DataEntity Class (Backend側の@Entityもしくはテーブルを作るためのClass)
2 CallBackEndAPI Interface (BackEnd側のControllerクラスの@GetMapping、@PostMappingとかを呼び出すInterface)
3 Retrofit Class (上記のCallBackEndAPI Interfaceを .create(CallBackEndAPI)でCallBackEndAPIを実現する)

封装 カプセル化

dataのcreate update read delete を一つのinterfaceとして定義し、 レトロフィットのcreate関数でinterfaceを実装し、interfaceのエンティティが戻り値として、実際の機能を持つようにまりました。 このエンティティのエンキュー関数を使ってspringbootのcontrollerの中の関数を呼び出す

接続方:SpringBootと接続なので、まずアンドロイド側でもSpringBootと同じくデーターエンティティクラスとCURD interfaceを作る、 次はレトロフィット実体を作成し、ドットCreate関数にCURD interfaceを引数として渡して、interface型のエンティティがreturnされる、 このinterface型のエンティティを使ってエンキュー中のcallback{}を使ってsuccessful と failure を判断する

JAVA

public class WeatherRepositoryImplRetrofit2 implements WeatherRepository {
public static final String TAG = WeatherRepositoryImplRetrofit2.class.getSimpleName();
private final WeatherService service;

public WeatherRepositoryImplRetrofit2() {
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(new Uri.Builder().scheme(SCHEME).authority(AUTHORITY).build().toString())
            .client(new OkHttpClient())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    service = retrofit.create(WeatherService.class);
}

@Override
public void getWeather(final RequestCallback callback) {
    service.getWeather(130010).enqueue(new Callback<Weather>() {
        @Override
        public void onResponse(Call<Weather> call, Response<Weather> response) {
            Log.d(TAG, "result: " + response.body().toString());
            callback.success(response.body());
        }

        @Override
        public void onFailure(Call<Weather> call, Throwable error) {
            callback.error(error);
        }
    });
}

private interface WeatherService {
    @GET(PATH)
    Call<Weather> getWeather(@Query("city") int city);
    }
}

KOTLIN

 
~/src/main/java/com/example/myfirstandroidapp/callBackEndAPI/RetrofitEntity.kt
class RetrofitEntity {
        val retrofit: Retrofit by lazy {
        Retrofit.Builder().apply {
            baseUrl("http://192.168.2.101:8090")
            addConverterFactory(GsonConverterFactory.create(Gson()))
        }.build()
    }
}
 
~/src/main/java/com/example/myfirstandroidapp/callBackEndAPI/CallBackEndAPI.kt
interface CallBackEndAPI {
    @POST("/DIM/save")
    fun save(@Body dimModel: DIMModel):Call<Void>
}

~/src/main/java/com/example/myfirstandroidapp/dataEntity/DIMModel.kt
class DIMModel {
    val id = 0
    var name = ""
    var password = ""
}
 
~/src/main/java/com/example/myfirstandroidapp/viewListener/DAO.kt
val name = binding.editTextNumber2.text.toString()
val passWord = binding.editTextTextPassword.text.toString()
val userMessage = DIMModel()

userMessage.name = name
userMessage.password = passWord

val retrofitEntity = RetrofitEntity()
val callBackEndAPI = **retrofitEntity.retrofit.create(CallBackEndAPI::class.java)**

callBackEndAPI.save(userMessage)
              .enqueue(object: Callback<Void>{
               override fun onResponse(call: Call<Void>, response: Response<**Void**>) {
                   Toast.makeText(activity,"Save Successful!!",Toast.LENGTH_SHORT).show()
               }

               override fun onFailure(call: Call<Void>, t: Throwable) {
                   Toast.makeText(activity,"Save Failed!!",Toast.LENGTH_SHORT).show()
                   Logger.getLogger(CallBackEndAPI::class.java.name).log(Level.SEVERE,"Error",t)
               }
           })

注意

以上仅提供例子 实际code要具体情况具体分析

kotlin例子中由于不需要实际的response(从后段返回的值) 所以callBack<Void(这里填的Void)> 如果需要返回值 则需要填相应的Class

androidspringboot's People

Contributors

starrysky-eng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.