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?

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
