Android项目高速开发框架探索(Mysql + OrmLite + Hessian + Sql
Android项目快速开发框架探索(Mysql + OrmLite + Hessian + Sqlite) 前言 结合之前所用的ormlite和hessian,再加上SAE已经支持JAVA,把服务端切换到JAVA,也就有了本文。使用hessian来做数据传输,ormlite来实现客户端与服务端的数据存储,极大的减少了CRUD
Android项目快速开发框架探索(Mysql + OrmLite + Hessian + Sqlite)前言
结合之前所用的ormlite和hessian,再加上SAE已经支持JAVA,把服务端切换到JAVA,也就有了本文。使用hessian来做数据传输,ormlite来实现客户端与服务端的数据存储,极大的减少了CRUD工作。本文为探索贴,未正式用于大型项目,欢迎大家讨论使用!
?
声明
欢迎转载,但请保留文章原始出处:)
ITEYE:http://www.iteye.com/
农民伯伯: http://www.cnblogs.com/over140/?
?
正文
一、简介
1.1ormlite
Ormlite[Object Relational Mapping Lite (ORM Lite)]
对象关系映射精简版(精简版的ORM)提供了一些简单的,轻量级持久化Java对象到SQL数据库,同时也避免了复杂性和更多的标准的ORM包的开销的功能。
支持数据库的jdbc调用,当然,最重要的肯定是它支持android原生的数据库api调用sqlite。
——转载自这里。?
1.2hessian?
使用方法参照本博两篇文章:
[hessdroid]Android下使用Hessian与Java服务端通讯?
[hessdroid]Android下使用Hessian与Java服务端通讯的传值测试?
?
1.3Android快速开发框架说明
考虑如下几个特点:
a).客户端(Android)和服务端均使用Java语言?
b).客户端(Android)和服务端均支持Hessian和ormlite框架
c).完整的支持面向对象开发:存储和交互传输?
?
二、准备
2.1开发环境
为了便于同时开发Android和Java Web,这里下载的是Eclipse IDE for Java EE Developers版本,然后安装最新的ADT插件和TOMCAT插件。
2.2服务端
应用服务器使用Tomcat,采用Java(JSP/Servlet)来实现服务端的业务逻辑,数据库使用Mysql。快速框架搭建推荐大家使用XAMPP(集成Apache、MySQL、PHP等,支持绿色安装)。
2.3客户端
普通的Android环境
2.4通信与存储说明
服务端与客户端通过Hessian进行数据交换,通过Ormlite保存数据库(通过JDBC保存到服务端的MYSQL数据库,也可以直接保存到客户端的sqlite数据库);
?
三、代码
3.1项目工程截图(服务端)
?HOLib共用于客户端和服务端,保证接口和数据对象一致性。
?
3.2重点代码分析
3.2.1服务端
web.xml

web-app?xmlns="http://java.sun.com/xml/ns/j2ee"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?web-app_2_4.xsd"
????version="2.4">
????servlet>
????????servlet-name>userservlet-name>
????????servlet-class>com.nmbb.ho.server.servlet.UserServletservlet-class>
????servlet>
????servlet-mapping>
????????servlet-name>userservlet-name>
????????url-pattern>/user.dourl-pattern>
????servlet-mapping>
????
????listener>
????????listener-class>com.nmbb.ho.server.StartupInitlistener-class>
????listener>
web-app>

?
StartupInit.java

????@Override
????public?void?contextInitialized(ServletContextEvent?arg0)?{
????????try?{
????????????TableUtils.dropTable(OrmliteHelper.getConnection(),?POUser.class,
????????????????????true);
????????????//创建数据库
????????????TableUtils.createTable(OrmliteHelper.getConnection(),?POUser.class);
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}
????}
????@Override
????public?void?contextDestroyed(ServletContextEvent?arg0)?{
????}
}

??代码说明:
StartupInit可用于创建数据库表结构,这里用于测试,真实环境注意数据丢失问题。
POUser.java

public?class?POUser?implements?Serializable?{
????/**?用户编号,6位数字?*/
????@DatabaseField(generatedId?=?true)
????public?int?suid;
????/**?用户名?*/
????@DatabaseField(width?=?30)
????public?String?username;
????/**?密码?*/
????@DatabaseField(width?=?30)
????public?String?password;
????/**?昵称?*/
????@DatabaseField(width?=?60)
????public?String?nickname;
????/**?200?正常?201?数据校验错误?202用户已经存在?*/
????public?int?status?=?200;
????/**?用于放错误信息?*/
????public?String?msg;
????public?POUser()?{
????}
}

?代码说明:
注意需要一个空的构造函数,其他请参考ormlite资料。?
?
UserServlet.java?

?*?用户Servlet
?*?
?*?@author?农民伯伯
?*?@see?http://www.cnblogs.com/over140/archive/2013/02/19/2917231.html
?*
?*/
public?class?UserServlet?extends?HessianServlet?implements?IUserService?{
????@Override
????public?POUser?register(String?username,?String?password)?{
????????POUser?result?=?new?POUser();
????????System.out.println("[UserServlet.register]...");
????????//?检测数据是否合法
????????if?(isEmpty(username)?||?isEmpty(password))?{
????????????result.status?=?201;
????????????result.msg?=?"用户名或密码不能为空";
????????}?else?{
????????????//?检测用户是否存在
????????????OrmliteHelper
????????????if?(db.exist(POUser.class,?"username",?username))?{
????????????????result.status?=?202;
????????????????result.msg?=?"用户名已经存在";
????????????}?else?{
????????????????result.username?=?username;
????????????????result.password?=?password;
????????????????db.create(result);//?入库
????????????????result.msg?=?"注册成功";
????????????????System.out.println("create?user?suid:"?+?result.suid);
????????????}
????????}
????????return?result;
????}
????@Override
????public?List
????????return?new?OrmliteHelper
????}
????/**
?????*?判断字符串是否为空
?????*?
?????*?@param?str
?????*?@return
?????*/
????public?static?boolean?isEmpty(String?str)?{
????????return?str?==?null?||?str.length()?==?0;
????}
}

?
3.2.2客户端(Android)??

????@Override
????protected?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????}
????public?void?OnClickRegiger(View?view)?{
????????new?AsyncTask
????????????@Override
????????????protected?POUser?doInBackground(Void...?params)?{
????????????????String?url?=?"http://192.168.68.23:8081/HOServer/user.do";
????????????????HessianProxyFactory?factory?=?new?HessianProxyFactory();
????????????????try?{
????????????????????factory.setDebug(true);
????????????????????factory.setReadTimeout(5000);
????????????????????//不设置会报?expected?hessian?reply?at?0x48?
????????????????????factory.setHessian2Reply(false);
????????????????????IUserService?basic?=?(IUserService)?factory.create(IUserService.class,?url,?getClassLoader());
????????????????????return?basic.register("admin",?"123456");
????????????????}?catch?(MalformedURLException?e)?{
????????????????????Log.e("UserActivity",?"OnClickRegiger",?e);
????????????????}?catch?(Exception?e)?{
????????????????????Log.e("UserActivity",?"OnClickRegiger",?e);
????????????????}
????????????????return?null;
????????????}
????????????@Override
????????????protected?void?onPostExecute(POUser?result)?{
????????????????if?(result?!=?null)?{
????????????????????if?(result.status?==?200)?{
????????????????????????//保存入库
????????????????????????new?DbHelper
????????????????????}
????????????????????Toast.makeText(UserActivity.this,?""?+?result.msg,?Toast.LENGTH_LONG).show();
????????????????}
????????????};
????????}.execute();
????}
}

?
代码说明:
1、DbHelper在源码里给出。?
2、如果项目无法编译通过,请注意设置项目的字符编码、JDK版本、Android的版本。?
?
三、总结
5.1优点
a).完全面向对象开发
b).降低项目的复杂度,减少引入其他框架所带来的复杂性?
c).非常适合一个开发服务端和客户端
充分的利用的框架的特点,提交开发效率,适合中小型项目快速开发。?
5.2缺点
a).注意服务端与客户端共用id的问题
5.3其他
a).ormlite支持标准的JPA助记符,这里。这样服务端采用Hibernate应该也是可以的,有时间可以做一个整合例子看看。
b).测试发现整个框架也适用于SAE,如果一个人负责客户端和服务端,那就太幸福了!
?
四、下载
?AndroidFramework2013-03-05.zip?

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
