估计大家用的最多的就是ssh了.这次来个Struts2+Spring3+MyBatis的整合,实现完美的web开发结构,之所以将hibernate换成MyBatis,有以下好处:
(1)MyBatis框架比hibernate更简单轻量,上手快
(2)MyBatis的效率整体上比hibernate高,请不要拍砖,这是事实,毕竟MyBatis的sql是生成好的,而hibernate需要根据不同的方言生成sql,效率降低了一些
(3)使用官方提供的MyBatis代码生成器生成的代码大大降低了手写sql的复杂度.常用的增加删除修改操作和hibernate几乎相同,并且常用的查询操作都可以直接调用生成好的mapper里面的方法,只有少量复杂的查询需要手写sql;反观hibernate,除了根据id查询之外的大多数查询都需要写hql.
本例子数据库采用mysql,业务层的东西大部分才用了注解进行标注(dao除外,仍然采用xml配置,下面会说原因),简单,下面就走一遍开发流程:
一、建立数据库(mysql)
Sql代码
create database test;
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;
二、建立web工程
目录里面大致有vo,dao,service,service.impl,action这个几个包
为什么没有dao.impl呢?因为MyBatis官方代码生成器生成的代码里面只有XxxxMapper.java一个文件,这个文件是一个接口,在mybatis-spring.jar里面有一个工厂类MapperFactoryBean,在整合spring的时候需要将XxxxMapper接口注入到这个工厂类中然后动态地返回一个实现类,所以没有专门存放dao实现类的dao.impl包。
因为没有实现类,只有接口,注解不能标注在接口上,所以需要单独配置dao,因此只有dao要配置xml,action和service都直接采用注解就可以了。
前台就三个页面,一个登陆,一个登陆成功,一个登录失败,action的路径为test/login.action,页面的代码就不贴了。
configuration是源码文件夹,专门用于存放配置文件,下面是项目结构图:
以下是用到的所有jar包:
三、持久层的开发
持久层选用MyBatis,先编写一个配置文件,然后用自动化工具生成dao层以下的所有代码,配置文件generatorConfig.xml供生成器用,详细的内容请看我前面的几篇博客
generatorConfig.xml:
Xml代码 " quality="high" allowscriptaccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
connectionURL="jdbc:mysql://localhost:3306/abator_test"
userId="root" password="root" />
上面的路径请酌情修改,保存之后在cmd下面
进入生成器的jar包所在的文件夹,然后执行以下命令:
Cmd代码
java -jar mybatis-generator-core-1.3.1.jar -configfile ../src/generatorConfig.xml -overwrite
OK,刷新项目,vo和dao里面的代码就全都出来了。
下面再建立一个Configuration.xml文件,该文件是MyBatis的重要配置文件
Configuration.xml:
Xml代码 " quality="high" allowscriptaccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
现在dao层的东西我们已经开发完成了。
四、编写service层代码,实现类中采用注解标注,简化开发。
接口,PersonService:
Java代码
package org.qiuqiu.service;
import org.qiuqiu.vo.Person;
public interface PersonService {
public Person login(String name,String password);
}
实现类,PersonServiceImpl:
Java代码 @Service public class PersonServiceImpl implements
org.qiuqiu.service.PersonService {
//@Resource默认是按照名称装配,找不到对应名字的则按照类型装配 @Resource private PersonMapper pm; public
PersonServiceImpl(){ System.out.println("初始化PersonServiceImpl"); }
public PersonMapper getPm() { return pm; } public void
setPm(PersonMapper pm) { this.pm = pm; } public Person login(String
name, String password) { System.out.println(name+" "+password);
PersonExample pe = new PersonExample();
pe.createCriteria().andNameEqualTo(name).andPasswordEqualTo(password);
List list = pm.selectByExample(pe); if(list.size()>0){ return
list.get(0); }else{ return null; } } } " quality="high"
allowscriptaccess="always" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
package org.qiuqiu.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.qiuqiu.dao.PersonMapper;
import org.qiuqiu.vo.Person;
import org.qiuqiu.vo.PersonExample;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl implements org.qiuqiu.service.PersonService {
//@Resource默认是按照名称装配,找不到对应名字的则按照类型装配
@Resource
private PersonMapper pm;
public PersonServiceImpl(){
System.out.println("初始化PersonServiceImpl");
}
public PersonMapper getPm() {
return pm;
}
public void setPm(PersonMapper pm) {
this.pm = pm;
}
public Person login(String name, String password) {
System.out.println(name+" "+password);
PersonExample pe = new PersonExample();
pe.createCriteria().andNameEqualTo(name).andPasswordEqualTo(password);
List list = pm.selectByExample(pe);
if(list.size()>0){
return list.get(0);
}else{
return null;
}
}
}
五、添加struts2框架,编写action
废话就不多说了,这一步需要一个struts的配置文件和一个action类
struts.xml:
Java代码 /success.jsp /error.jsp " quality="high"
allowscriptaccess="always" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
/success.jsp
/error.jsp
处理登陆操作的action,LoginAction:
Java代码 //记得将该类标注为控制器bean,scope必须为prototype @Controller @Scope("prototype") public
class LoginAction extends ActionSupport { private static final long
serialVersionUID = -1006252987556326592L; //
@Resource默认是按照名称装配,找不到对应名字的则按照类型装配 @Resource private PersonService ps;
private String name; private String password; public PersonService
getPs() { return ps; } public void setPs(PersonService ps) {
this.ps = ps; } 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; } @Override public String
execute() throws Exception { System.out.println("收到信息-----------");
if (ps.login(name, password) != null) { return SUCCESS; } else {
return ERROR; } } } " quality="high" allowscriptaccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
package org.qiuqiu.action;
import javax.annotation.Resource;
import org.qiuqiu.service.PersonService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
//记得将该类标注为控制器bean,scope必须为prototype
@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = -1006252987556326592L;
// @Resource默认是按照名称装配,找不到对应名字的则按照类型装配
@Resource
private PersonService ps;
private String name;
private String password;
public PersonService getPs() {
return ps;
}
public void setPs(PersonService ps) {
this.ps = ps;
}
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;
}
@Override
public String execute() throws Exception {
System.out.println("收到信息-----------");
if (ps.login(name, password) != null) {
return SUCCESS;
} else {
return ERROR;
}
}
}
六、添加spring,整合struts和spring
这一步主要是一个spring的配置文件,该配置文件配置了数据源,连接池,sqlsession,以及非常重要的dao
applicationContext.xml:
Xml代码 class="org.mybatis.spring.mapper.MapperFactoryBean">
" quality="high" allowscriptaccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
class="org.mybatis.spring.mapper.MapperFactoryBean">
七、修改web.xml文件,加入spring和struts支持
web.xml:
Xml代码 struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp struts-cleanup
/* struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2 /* org.springframework.web.context.ContextLoaderListener
contextConfigLocation classpath:applicationContext.xml index.jsp "
quality="high" allowscriptaccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
index.jsp
整个例子的开发已经完成了,该过程还是比较简单的,主要是MyBatis官方为我们提供的代码生成器将底层的代码量大大地减少了