估计大家用的最多的就是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)
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 version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration > <!-- 设置mysql驱动路径 --> <classPathEntry location="./mysql-connector-java-5.0.8-bin.jar" /> <!-- 此处指定生成针对MyBatis3的DAO --> <context id="context1" targetRuntime="MyBatis3"> <!-- jdbc连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/abator_test" userId="root" password="root" /> <!-- 生成vo对象 --> <javaModelGenerator targetPackage="org.qiuqiu.vo" targetProject="../src" /> <!-- 生成用于查询的Example对象 --> <sqlMapGenerator targetPackage="org.qiuqiu.vo" targetProject="../src" /> <!-- 生成DAO的类文件以及配置文件 --> <javaClientGenerator targetPackage="org.qiuqiu.dao" targetProject="../src" type="XMLMAPPER" /> <!-- 想要生成的数据库表,自动化工具会根据该表的结构生成相应的vo对象 --> <table schema="" tableName="person" > </table> </context> </generatorConfiguration>
上面的路径请酌情修改,保存之后在cmd下面 进入生成器的jar包所在的文件夹,然后执行以下命令:
java -jar mybatis-generator-core-1.3.1.jar -configfile ../src/generatorConfig.xml -overwrite
OK,刷新项目,vo和dao里面的代码就全都出来了。
下面再建立一个Configuration.xml文件,该文件是MyBatis的重要配置文件
Configuration.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="myexample">
<environment id="myexample">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/qiuqiu/dao/PersonMapper.xml"/>
</mappers>
</configuration>
现在dao层的东西我们已经开发完成了。
四、编写service层代码,实现类中采用注解标注,简化开发。
接口,PersonService:
package org.qiuqiu.service; import org.qiuqiu.vo.Person; public interface PersonService { public Person login(String name,String password); }
实现类,PersonServiceImpl:
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<Person> list = pm.selectByExample(pe); if(list.size()>0){ return list.get(0); }else{ return null; } } }
五、添加struts2框架,编写action
废话就不多说了,这一步需要一个struts的配置文件和一个action类
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="json" namespace="/test" extends="struts-default"> <action name="login" class="loginAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
处理登陆操作的action,LoginAction:
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 version="1.0" encoding="UTF-8"?> <beans 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"> <!-- 数据库连接池 --> <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="initialPoolSize" value="10" /> <property name="minPoolSize" value="5" /> <property name="maxPoolSize" value="30" /> <property name="acquireIncrement" value="10" /> <property name="maxIdleTime" value="10" /> <property name="maxStatements" value="0" /> </bean> <!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="c3p0DataSource"/> </bean> <!-- 声明式事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置注解搜索的范围,该例子中spring会自动搜索org.qiuqiu下面的注解 --> <context:component-scan base-package="org.qiuqiu"/> <!-- 此处配置dao,class为一工厂类,需要注入对应的dao接口和sqlsessionfactory --> <bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.qiuqiu.dao.PersonMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
七、修改web.xml文件,加入spring和struts支持
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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"> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml </param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
整个例子的开发已经完成了,该过程还是比较简单的,主要是MyBatis官方为我们提供的代码生成器将底层的代码量大大地减少了,附件在后面,有问题请留言,欢迎大家指出不足。