Skip to content

Commit b233b9d

Browse files
committed
新增·Android操作数据库Demo
1 parent 77422fe commit b233b9d

File tree

193 files changed

+18232
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+18232
-1
lines changed

AndroidDatabaseDemo.zip

1.39 MB
Binary file not shown.

AndroidDatabaseDemo/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>edu.heuet.android.shaohua</groupId>
8+
<artifactId>shaohua-backend</artifactId>
9+
<version>1.0</version>
10+
11+
<name>shaohua-backend</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<!-- 1、引入的第一个:导入springmvc、spring-aop之类的jar包 -->
16+
<parent>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-parent</artifactId>
19+
<version>2.1.4.RELEASE</version>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<maven.compiler.source>1.8</maven.compiler.source>
25+
<maven.compiler.target>1.8</maven.compiler.target>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.11</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<!-- 2、 引入的第二个:导入一些web相关的jar包 -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<!-- 3、 引入的第三个:java连接mysql的connector的jar包 -->
41+
<dependency>
42+
<groupId>mysql</groupId>
43+
<artifactId>mysql-connector-java</artifactId>
44+
<version>5.1.41</version>
45+
</dependency>
46+
<!-- 4、引入的第四个:阿里的德鲁伊连接池,管理mysql连接的jar包 -->
47+
<dependency>
48+
<groupId>com.alibaba</groupId>
49+
<artifactId>druid</artifactId>
50+
<version>1.1.3</version>
51+
</dependency>
52+
<!-- 5、引入的第五个:Mybatis相关的jar包 -->
53+
<dependency>
54+
<groupId>org.mybatis.spring.boot</groupId>
55+
<artifactId>mybatis-spring-boot-starter</artifactId>
56+
<version>1.3.1</version>
57+
</dependency>
58+
59+
<!-- 11、Apache commons-->
60+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
61+
<dependency>
62+
<groupId>org.apache.commons</groupId>
63+
<artifactId>commons-lang3</artifactId>
64+
<version>3.4</version>
65+
</dependency>
66+
67+
<!-- 12、hibernate validator -->
68+
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
69+
<dependency>
70+
<groupId>org.hibernate.validator</groupId>
71+
<artifactId>hibernate-validator</artifactId>
72+
<version>6.0.13.Final</version>
73+
</dependency>
74+
75+
<!-- 13、Log -->
76+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
77+
<dependency>
78+
<groupId>org.slf4j</groupId>
79+
<artifactId>slf4j-api</artifactId>
80+
<version>1.7.25</version>
81+
</dependency>
82+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
83+
<dependency>
84+
<groupId>org.slf4j</groupId>
85+
<artifactId>slf4j-log4j12</artifactId>
86+
<version>1.7.25</version>
87+
<!-- <scope>test</scope> -->
88+
</dependency>
89+
<dependency>
90+
<groupId>mysql</groupId>
91+
<artifactId>mysql-connector-java</artifactId>
92+
<version>8.0.21</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.mybatis.generator</groupId>
96+
<artifactId>mybatis-generator-core</artifactId>
97+
<version>1.3.5</version>
98+
</dependency>
99+
</dependencies>
100+
101+
<build>
102+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
103+
<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+
144+
<!-- 6、第六个引入的:mybatis的相关插件 -->
145+
<plugin>
146+
<groupId>org.mybatis.generator</groupId>
147+
<artifactId>mybatis-generator-maven-plugin</artifactId>
148+
<version>1.3.5</version>
149+
<!-- 注意这里plugin下的dependencies,Maven是无法识别的 -->
150+
<!-- 比较骚的操作就是,先把下面的dependency放到外面project下面的dependencies -->
151+
<!-- 然后去右侧工具条Maven执行Reimport,最后再把这些dependency放回来,再进行Reimport,来导入mybatis的插件 -->
152+
<dependencies>
153+
<!-- 7、 第七个引入的:mybatis生成器的core相关jar包 -->
154+
<dependency>
155+
<groupId>org.mybatis.generator</groupId>
156+
<artifactId>mybatis-generator-core</artifactId>
157+
<version>1.3.5</version>
158+
</dependency>
159+
<!-- 8、 在mybatis的插件里再引入一遍mysql的连接jar包 -->
160+
<!-- 上面曾经引入过,是服务端java代码与mysql连接 -->
161+
<!-- 这里再次声明,是mybatis插件和mysql连接 -->
162+
<!-- 虽说老师这么写了, 但我个人觉得没必要重复引入jar包啊 -->
163+
<!-- 老师给出的解释是:要生成的mybatis插件,需要借助相关的依赖 -->
164+
<dependency>
165+
<groupId>mysql</groupId>
166+
<artifactId>mysql-connector-java</artifactId>
167+
<version>8.0.21</version>
168+
</dependency>
169+
</dependencies>
170+
<!-- executions : 执行-->
171+
<!-- 9、配置mybatis generator 插件的执行阶段phase以及执行目的goal -->
172+
<executions>
173+
<execution>
174+
<id>mybatis generator</id>
175+
<phase>package</phase>
176+
<goals>
177+
<goal>generate</goal>
178+
</goals>
179+
</execution>
180+
</executions>
181+
<!-- 10、mybatis generator 插件的相关属性配置 -->
182+
<configuration>
183+
<!-- verbose : 冗余的 , 允许移动生成的文件 -->
184+
<verbose>true</verbose>
185+
<!-- 新生成的覆盖旧生成的,实际工作中慎用 -->
186+
<overwrite>true</overwrite>
187+
<!-- 指定mybatis配置文件的路径 -->
188+
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
189+
</configuration>
190+
191+
</plugin>
192+
193+
194+
</plugins>
195+
</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>
206+
</build>
207+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package edu.heuet.shaohua;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@SpringBootApplication(scanBasePackages = {"edu.heuet.shaohua"})
10+
@RestController
11+
@MapperScan("edu.heuet.shaohua.dao")
12+
public class App {
13+
@GetMapping("/hello")
14+
public String hello(){
15+
return "Hello World!";
16+
}
17+
18+
public static void main( String[] args ) {
19+
System.out.println( "Hello World!" );
20+
SpringApplication.run(App.class);
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package edu.heuet.shaohua.controller;
2+
3+
import edu.heuet.shaohua.error.BusinessException;
4+
import edu.heuet.shaohua.error.EmBusinessError;
5+
import edu.heuet.shaohua.response.CommonReturnType;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
import org.springframework.web.bind.annotation.ResponseStatus;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
/**
14+
* @author littlecurl
15+
*/
16+
public class BaseController {
17+
18+
public static final String CONTENT_TYPE_FORMED = "application/x-www-form-urlencoded";
19+
20+
// 定义ExceptionHandler解决直接抛到Tomcat容器层而未被Controller吸收的Exception
21+
22+
/**
23+
* ******************* 对三个注解的解释:*******************
24+
* 1、@ExceptionHandler(arg) 参数指明要拦截的异常类型,这里直接拦截Exception父类
25+
* 2、@ResponseStatus(HttpStatus.OK) 如果不加这个注解,返回状态码是500,
26+
* 加上这个注解后就可以将自定义的错误信息呈现到前端了
27+
* 3、@ResponseBody 如果不加这个注解,那么返回值就只是一个Object ,
28+
* 呈现给页面的就是 404 ,
29+
* 加上这个注解后就会返回Object的JSON序列串
30+
* @return CommonReturnType 通用返回对象
31+
*/
32+
@ExceptionHandler(Exception.class)
33+
@ResponseStatus(HttpStatus.OK)
34+
@ResponseBody
35+
public Object handlerException(HttpServletRequest request, Exception ex){
36+
// Map<String, Object> responseData = new HashMap<>();
37+
if (ex instanceof BusinessException){
38+
BusinessException businessException = (BusinessException)ex;
39+
// responseData.put("errorCode",businessException.getCode());
40+
// responseData.put("errorMsg",businessException.getMsg());
41+
return CommonReturnType.create(businessException.getCode(),businessException.getMsg());
42+
} else {
43+
// responseData.put("errorCode", EmBusinessError.UNKNOWN_ERROR.getCode());
44+
// responseData.put("errorMsg",EmBusinessError.UNKNOWN_ERROR.getMsg());
45+
return CommonReturnType.create(EmBusinessError.UNKNOWN_ERROR.getCode(),EmBusinessError.UNKNOWN_ERROR.getMsg());
46+
}
47+
48+
}
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package edu.heuet.shaohua.controller;
2+
3+
import edu.heuet.shaohua.dataobject.NewsDO;
4+
import edu.heuet.shaohua.response.CommonReturnType;
5+
import edu.heuet.shaohua.service.NewsService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
@Controller
17+
@RequestMapping("/news")
18+
public class NewsController extends BaseController {
19+
@Autowired
20+
NewsService newsService;
21+
22+
@GetMapping("/detail/id")
23+
@ResponseBody
24+
public CommonReturnType detailById(@RequestParam(name = "id") long id) {
25+
NewsDO newsDO = newsService.selectDetailById(id);
26+
List<NewsDO> newsDOList = new ArrayList<>();
27+
newsDOList.add(newsDO);
28+
return CommonReturnType.create(newsDOList);
29+
}
30+
31+
@GetMapping("/detail/title")
32+
@ResponseBody
33+
public CommonReturnType detailByTitle(@RequestParam(name = "title") String title) {
34+
List<NewsDO> newsDOList = newsService.selectDetailByTitle(title);
35+
return CommonReturnType.create(newsDOList);
36+
}
37+
38+
@GetMapping("/list")
39+
@ResponseBody
40+
public CommonReturnType list() {
41+
List<NewsDO> newsDOList = newsService.selectAll();
42+
return CommonReturnType.create(newsDOList);
43+
}
44+
}

0 commit comments

Comments
 (0)