这篇文章主要是用来记录一下自己最近在整合一个简单的框架,虽然比较简单,但是自己也是花了不少时间去看文档和动手实现的。这里没有很详细的去介绍细节上的东西,大部分引用了官网上的内容,如果读者在搭建过程中遇到什么问题,或者文章中有什么不懂,欢迎一起讨论。
这里要介绍的包括三个组件,SpringMVC+datanucleus+mariadb,其中SpringMvc用来支持整个web项目的框架,datanucleus用来实现数据持久层,mariadb是数据库组件。下面是三个组件的简单介绍。如果读者想要学习这些组件,建议到相应的官网上查看相应的文档,官网上的介绍和文档已经很详细了。
(1)SpringMVC
Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。
SpringMVC官网 http://projects.spring.io/spring-framework/
(2)datanucleus
DataNucleus Access Platform 是一个兼容各种标准的 Java 数据持久化框架,兼容 JDO1, JDO2, JDO2.1, JDO2.2, JDO2.3, JPA1, JPA2 等 Java 标准。提供一个基于 REST 的API。通过它可以访问所有常见的数据库服务器,还包括 LDAP, NeoDatis, JSON, Excel/ODF spreadsheets, XML, BigTable, 和 HADOOP 数据库等。
DataNucleus官网 http://www.datanucleus.org/
(3)mariadb
MariaDB基于事务的Maria存储引擎,替换了MySQL的MyISAM存储引擎,它使用了Percona的 XtraDB,InnoDB的变体,分支的开发者希望提供访问即将到来的MySQL 5.4 InnoDB性能。这个版本还包括了 PrimeBase XT (PBXT) 和 FederatedX存储引擎。
mariadb官网 https://downloads.mariadb.org/
下面我们将整和一个demo,用来实现用户登录功能。
开发工具为MyEclipse2014,开发环境是windows,服务器是tomcat8.0。
(1)mariadb数据库安装
mariadb的安装很简单,只需要到官网下载相应的安装包或者解压包,安装或者解压到系统上,并设置一下环境变量就可以了,mariadb的使用和mysql几乎一致,所以这里就不介绍了。
首先我们建立一个用户数据库命名为chen,并建立一个数据表User用来记录用户的账号密码:
(2)插件安装
我们的开发环境为eclipse,需要支持javaee的最新版本,同时这里需要用到两个插件,分别是maven plugin和DataNucleus plugin。
maven在这里主要用来管理项目依赖,同时也是为了进行字节码增强,DataNucleus在这里是为了设置项目的字节码自动增强功能,所以这两个插件必不可少。
字节码增强:
Java 字节代码增强指的是在Java字节代码生成之后,对其进行修改,增强其功能。这种做法相当于对应用程序的二进制文件进行修改。在很多Java框架中都可以见到这种实现方式。在使用Datanucleus时我们需使用插件来对实体类进行增强。
关于这两个插件的安装,读者可以查阅相关的资料。这里是DataNucleus plugin的安装过程:http://www.datanucleus.org/products/datanucleus/jdo/guides/eclipse.html
(3)项目架构搭建
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
servlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>login.jspwelcome-file>
welcome-file-list>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
/huguaTicket/src/main/webapp/WEB-INF/SpringMvc-servlet.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="com.chen"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp"/>
beans>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="MyTest">
<class>com.chen.entity.Userclass>
<exclude-unlisted-classes />
persistence-unit>
persistence>
新建页面文件
index.jsp用来显示登陆窗口,login.jsp用来显示登陆信息
修改pom.xml
我们需要在pom.xml中完成SpringMVC+datanucleus的依赖包添加
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>lab.io.rushgroupId>
<artifactId>huguaTicktartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name>huguaTickt Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>com.sun.mailgroupId>
<artifactId>javax.mailartifactId>
<version>1.5.0version>
dependency>
<dependency>
<groupId>org.datanucleusgroupId>
<artifactId>datanucleus-coreartifactId>
<version>[5.0.0-m1, 5.9)version>
dependency>
<dependency>
<groupId>org.datanucleusgroupId>
<artifactId>datanucleus-api-jdoartifactId>
<version>[5.0.0-m1, 5.9)version>
dependency>
<dependency>
<groupId>org.datanucleusgroupId>
<artifactId>datanucleus-rdbmsartifactId>
<version>[5.0.0-m1, 5.9)version>
dependency>
<dependency>
<groupId>org.datanucleusgroupId>
<artifactId>javax.jdoartifactId>
<version>[3.2.0-m6, )version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.18version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjtoolsartifactId>
<version>1.8.8version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjrtartifactId>
<version>1.8.8version>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>3.2.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-asmartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-instrumentartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-instrument-tomcatartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.6.9version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-oxmartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvc-portletartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-strutsartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-aclartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-aspectsartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-casartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-configartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-coreartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-cryptoartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-ldapartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-openidartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-remotingartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-taglibsartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-webartifactId>
<version>3.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.webflowgroupId>
<artifactId>spring-webflowartifactId>
<version>2.3.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.webflowgroupId>
<artifactId>spring-bindingartifactId>
<version>2.3.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.webflowgroupId>
<artifactId>spring-jsartifactId>
<version>2.3.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.webflowgroupId>
<artifactId>spring-js-resourcesartifactId>
<version>2.3.1.RELEASEversion>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.datanucleusgroupId>
<artifactId>datanucleus-maven-pluginartifactId>
<version>5.0.1version>
<executions>
<execution>
<phase>test-compilephase>
<goals>
<goal>enhancegoal>
goals>
execution>
executions>
<configuration>
<persistenceUnitName>MyTestpersistenceUnitName>
<verbose>falseverbose>
configuration>
plugin>
plugins>
build>
project>
关于这里datanucleus enhance(字节码增强)的配置,可以参考下面网站:
http://www.datanucleus.org/products/datanucleus/jdo/enhancer.html
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://101.201.70.216:3306/chen"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="myPmf" class="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" destroy-method="close">
<property name="connectionFactory" ref="dataSource"/>
<property name="nontransactionalRead" value="true"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
我们新建一个controller
/huguaTickets/src/main/java/com/chen/controller/loginController.java
用来实现用户的登陆判断
package com.chen.controller;
import java.lang.annotation.Annotation;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ControllerSupport;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.chen.service.UserService;
@Component
@RequestMapping("login.do")
public class loginController implements Controller{
@Resource
private UserService service;
@RequestMapping
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String username = request.getParameter("username");
String password = request.getParameter("password");
String msg = null;
if(service.login(username,password)==1){
msg = "登陆成功";
}else{
msg = "登陆失败";
}
ModelAndView view = new ModelAndView("login");
view.addObject("msg", msg);
return view;
}
}
新建一个
/huguaTickets Maven Webapp/src/main/java/com/chen/service/UserService.java
package com.chen.service;
import com.chen.dao.UserDao;
@Component
public class UserService {
@Resource
private UserDao userdao;
public int login(String username, String password) {
return userdao.login(username,password);
}
}
我们需要先新建一个实体类User,用来映射数据表User
/**********************************************************************
Copyright (c) 2013 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package com.chen.entity;
import java.io.Serializable;
import javax.jdo.annotations.*;
/**
* Sample class that has a String field holding a Long.
*/
@PersistenceCapable
public class User
{
@PrimaryKey
int id;
String name;
String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在这里我直接修改了datanucleus dao的sample,上面用到的注解可以很容易理解,关于更多的注解可以查询官网。
然后我们可以实现数据库dao层
/huguaTickets Maven Webapp/src/main/java/com/chen/dao/UserDao.java
package com.chen.dao;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import com.chen.entity.User;
import com.chen.util.MyPmUtil;
@Component
public class UserDao {
@Resource(name="myPmf")
private PersistenceManagerFactory pmf;
public int login(String username, String password) {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = MyPmUtil.getTx();
tx.begin();
Query q = pm.newQuery("SELECT FROM " + User.class.getName()+" WHERE name == '"+username+"'"+"&& password =='" + password+"'");
List holders = (List) q.execute();
if(holders.size() > 0){
tx.commit();
return 1;
}
tx.commit();
return 0;
}
}
这样子,整个demo就搭建好了,下面我们将进行测试和运行。
我们需要先对项目添加 datanucleus support,并勾上enable auto-enhancement允许eclipse帮我们实现实体类的字节码增加。
然后我们就可以部署项目并运行项目了。