Skip to content

Commit ce65e87

Browse files
committed
新增LitePalDemo
1 parent b233b9d commit ce65e87

File tree

16 files changed

+393
-93
lines changed

16 files changed

+393
-93
lines changed

AndroidDatabaseDemo.zip

195 KB
Binary file not shown.

AndroidDatabaseDemo/LoginBackEnd/pom.xml

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>mysql</groupId>
4343
<artifactId>mysql-connector-java</artifactId>
44-
<version>5.1.41</version>
44+
<version>8.0.21</version>
4545
</dependency>
4646
<!-- 4、引入的第四个:阿里的德鲁伊连接池,管理mysql连接的jar包 -->
4747
<dependency>
@@ -86,11 +86,6 @@
8686
<version>1.7.25</version>
8787
<!-- <scope>test</scope> -->
8888
</dependency>
89-
<dependency>
90-
<groupId>mysql</groupId>
91-
<artifactId>mysql-connector-java</artifactId>
92-
<version>8.0.21</version>
93-
</dependency>
9489
<dependency>
9590
<groupId>org.mybatis.generator</groupId>
9691
<artifactId>mybatis-generator-core</artifactId>
@@ -99,48 +94,8 @@
9994
</dependencies>
10095

10196
<build>
102-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
97+
<pluginManagement>
10398
<plugins>
104-
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
105-
<plugin>
106-
<artifactId>maven-clean-plugin</artifactId>
107-
<version>3.1.0</version>
108-
</plugin>
109-
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
110-
<plugin>
111-
<artifactId>maven-resources-plugin</artifactId>
112-
<version>3.0.2</version>
113-
</plugin>
114-
<plugin>
115-
<artifactId>maven-compiler-plugin</artifactId>
116-
<version>3.8.0</version>
117-
</plugin>
118-
<plugin>
119-
<artifactId>maven-surefire-plugin</artifactId>
120-
<version>2.22.1</version>
121-
</plugin>
122-
<plugin>
123-
<artifactId>maven-jar-plugin</artifactId>
124-
<version>3.0.2</version>
125-
</plugin>
126-
<plugin>
127-
<artifactId>maven-install-plugin</artifactId>
128-
<version>2.5.2</version>
129-
</plugin>
130-
<plugin>
131-
<artifactId>maven-deploy-plugin</artifactId>
132-
<version>2.8.2</version>
133-
</plugin>
134-
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
135-
<plugin>
136-
<artifactId>maven-site-plugin</artifactId>
137-
<version>3.7.1</version>
138-
</plugin>
139-
<plugin>
140-
<artifactId>maven-project-info-reports-plugin</artifactId>
141-
<version>3.0.0</version>
142-
</plugin>
143-
14499
<!-- 6、第六个引入的:mybatis的相关插件 -->
145100
<plugin>
146101
<groupId>org.mybatis.generator</groupId>
@@ -187,21 +142,16 @@
187142
<!-- 指定mybatis配置文件的路径 -->
188143
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
189144
</configuration>
190-
191145
</plugin>
192-
193-
146+
<plugin>
147+
<groupId>org.apache.maven.plugins</groupId>
148+
<artifactId>maven-compiler-plugin</artifactId>
149+
<configuration>
150+
<source>8</source>
151+
<target>8</target>
152+
</configuration>
153+
</plugin>
194154
</plugins>
195155
</pluginManagement>
196-
<plugins>
197-
<plugin>
198-
<groupId>org.apache.maven.plugins</groupId>
199-
<artifactId>maven-compiler-plugin</artifactId>
200-
<configuration>
201-
<source>8</source>
202-
<target>8</target>
203-
</configuration>
204-
</plugin>
205-
</plugins>
206156
</build>
207157
</project>

AndroidDatabaseDemo/LoginBackEnd/src/main/java/edu/heuet/shaohua/controller/StudentController.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,23 @@ public CommonReturnType query(
6262
@RequestParam(name = "name", required = false) String name) {
6363
if (!StringUtils.isEmpty(studentNo)) {
6464
StudentDO studentDO1 = studentDOMapper.selectByPrimaryKey(studentNo);
65-
return CommonReturnType.create(studentDO1.getName());
65+
66+
if (studentDO1 != null) {
67+
return CommonReturnType.create(studentDO1.getName());
68+
} else {
69+
return CommonReturnType.create("");
70+
}
6671
} else if (!StringUtils.isEmpty(name)) {
6772
StudentDO studentDO2 = studentDOMapper.selectByName(name);
68-
return CommonReturnType.create(studentDO2.getStudentNo());
73+
74+
if (studentDO2 != null) {
75+
return CommonReturnType.create(studentDO2.getStudentNo());
76+
} else {
77+
return CommonReturnType.create("");
78+
}
6979
} else {
7080
List<StudentDO> studentDOS = studentDOMapper.getAll();
71-
StringBuilder studentBuilder = new StringBuilder();
81+
StringBuilder studentBuilder = new StringBuilder("");
7282
for (StudentDO student : studentDOS) {
7383
studentBuilder.append("学号:").append(student.getStudentNo()).append("\n\n");
7484
studentBuilder.append("姓名:").append(student.getName()).append("\n\n");

AndroidDatabaseDemo/app/build.gradle

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ android {
3434

3535
dependencies {
3636

37+
// 基础依赖,以满足SQLite
3738
implementation 'androidx.appcompat:appcompat:1.2.0'
3839
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
3940
implementation "com.google.android.material:material:1.2.1"
4041

41-
// 架构
42-
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
43-
implementation "android.arch.lifecycle:runtime:1.1.1"
44-
45-
// Room
42+
// Room新增
4643
implementation "androidx.room:room-runtime:2.2.5"
4744
annotationProcessor "androidx.room:room-compiler:2.2.5"
4845

49-
// 网络请求
46+
// LitePal新增
47+
implementation 'org.litepal.guolindev:core:3.2.2'
48+
49+
// MySQL新增
5050
implementation project(':xhttp2-lib')
51+
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
52+
implementation "android.arch.lifecycle:runtime:1.1.1"
5153
}

AndroidDatabaseDemo/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
android:allowBackup="true"
88
android:icon="@mipmap/ic_launcher"
99
android:label="@string/app_name"
10+
android:networkSecurityConfig="@xml/network_security_config"
1011
android:roundIcon="@mipmap/ic_launcher_round"
1112
android:supportsRtl="true"
12-
android:networkSecurityConfig="@xml/network_security_config"
1313
android:theme="@style/Theme.AndroidDatabaseDemo">
1414
<activity android:name=".MainActivity">
1515
<intent-filter>
@@ -21,6 +21,7 @@
2121
<activity android:name=".sqlite.SQLiteActivity" />
2222
<activity android:name=".room.RoomActivity" />
2323
<activity android:name=".mysql.MySQLActivity" />
24+
<activity android:name=".litepal.LitePalActivity" />
2425
</application>
2526

2627
</manifest>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<litepal>
3+
<!--
4+
Define the database name of your application.
5+
By default each database name should be end with .db.
6+
If you didn't name your database end with .db,
7+
LitePal would plus the suffix automatically for you.
8+
For example:
9+
<dbname value="demo" />
10+
-->
11+
<dbname value="demo_litepal" />
12+
13+
<!--
14+
Define the version of your database. Each time you want
15+
to upgrade your database, the version tag would helps.
16+
Modify the models you defined in the mapping tag, and just
17+
make the version value plus one, the upgrade of database
18+
will be processed automatically without concern.
19+
For example:
20+
<version value="1" />
21+
-->
22+
<version value="1" />
23+
24+
<!--
25+
Define your models in the list with mapping tag, LitePal will
26+
create tables for each mapping class. The supported fields
27+
defined in models will be mapped into columns.
28+
For example:
29+
<list>
30+
<mapping class="com.test.model.Reader" />
31+
<mapping class="com.test.model.Magazine" />
32+
</list>
33+
-->
34+
<list>
35+
<mapping class="cn.edu.heuet.demo.litepal.model.Student" />
36+
<!-- <mapping class="org.litepal.litepalsample.model.Song" />-->
37+
<!-- <mapping class="org.litepal.litepalsample.model.Singer" />-->
38+
39+
40+
<!-- <mapping class="com.litepaltest.model.Classroom" />
41+
<mapping class="com.litepaltest.model.Teacher" />
42+
<mapping class="com.litepaltest.model.IdCard" />
43+
<mapping class="com.litepaltest.model.Student" />
44+
<mapping class="com.litepaltest.model.Cellphone" />
45+
<mapping class="com.litepaltest.model.Computer" />
46+
<mapping class="com.litepaltest.model.Book" />
47+
<mapping class="com.litepaltest.model.Product" />
48+
<mapping class="com.litepaltest.model.Headset" />
49+
<mapping class="com.litepaltest.model.WeChatMessage" />
50+
<mapping class="com.litepaltest.model.WeiboMessage" />-->
51+
</list>
52+
53+
<!--
54+
Define the cases of the tables and columns name. Java is a
55+
case sensitive language, while database is case insensitive.
56+
LitePal will turn all classes names and fields names into lowercase
57+
by default while creating or upgrading database. Developers can change
58+
this behavior into the styles their like. "keep" will keep the
59+
cases of classes and fields. "upper" will turn all classes names
60+
and fields names into uppercase. "lower" will act as default.
61+
Do not change the value after you run your app for the first time,
62+
or it might cause the exception that column can not be found.
63+
value options: keep lower upper
64+
For example:
65+
<cases value="lower" />
66+
-->
67+
68+
<!--
69+
Define where the .db file should be. "internal" means the .db file
70+
will be stored in the database folder of internal storage which no
71+
one can access. "external" means the .db file will be stored in the
72+
path to the directory on the primary external storage device where
73+
the application can place persistent files it owns which everyone
74+
can access. "internal" will act as default.
75+
For example:
76+
<storage value="external" />
77+
-->
78+
79+
<storage value="external" />
80+
81+
</litepal>

AndroidDatabaseDemo/app/src/main/java/cn/edu/heuet/demo/MainActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import android.content.Intent;
44
import android.os.Bundle;
5+
import android.view.View;
56

67
import androidx.appcompat.app.AppCompatActivity;
78
import androidx.databinding.DataBindingUtil;
89

910
import cn.edu.heuet.demo.databinding.ActivityMainBinding;
11+
import cn.edu.heuet.demo.litepal.LitePalActivity;
1012
import cn.edu.heuet.demo.mysql.MySQLActivity;
1113
import cn.edu.heuet.demo.room.RoomActivity;
1214
import cn.edu.heuet.demo.sqlite.SQLiteActivity;
@@ -22,6 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
2224

2325
binding.btSqlite.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, SQLiteActivity.class)));
2426
binding.btRoom.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, RoomActivity.class)));
27+
binding.btLitepal.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, LitePalActivity.class)));
2528
binding.btMysql.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, MySQLActivity.class)));
2629
}
2730

AndroidDatabaseDemo/app/src/main/java/cn/edu/heuet/demo/MyApp.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
package cn.edu.heuet.demo;
22

33
import android.app.Application;
4+
import android.database.sqlite.SQLiteDatabase;
45

56
import androidx.room.Room;
67

78
import com.xuexiang.xhttp2.XHttpSDK;
89

10+
import org.litepal.LitePal;
11+
912
import cn.edu.heuet.demo.mysql.network.NetworkConstant;
1013
import cn.edu.heuet.demo.room.database.DemoDatabase;
14+
import cn.edu.heuet.demo.sqlite.DatabaseHelper;
1115

1216
public class MyApp extends Application {
13-
static Application application;
17+
public static Application application;
1418
public static DemoDatabase roomDb;
19+
public static SQLiteDatabase sqliteDb;
20+
public static DatabaseHelper dbHelper;
1521

1622
@Override
1723
public void onCreate() {
1824
super.onCreate();
1925
application = this;
2026

27+
// SQLite
28+
// 初始化数据库,建库
29+
dbHelper = new DatabaseHelper(this);
30+
sqliteDb = dbHelper.getWritableDatabase();
31+
32+
// ROOM
2133
roomDb = Room.databaseBuilder(this, DemoDatabase.class, "demo_room").build();
2234

35+
// LitePal
36+
LitePal.initialize(this);
37+
38+
39+
// MYSQL
2340
initHttp();
2441
}
2542

0 commit comments

Comments
 (0)