第八章 示例代码(MyBatis)
Sample Code
JPetStore 6 is a full web application built on top of MyBatis 3, Spring 3 and Stripes. It is available for downloading in the downloads section of MyBatis project site. In this section we will walk through this sample to understand how is it built and learn how to run it.
Purpose
This new JPetStore comes with the same idea in mind than its predecessors: keep it simple. The purpose of JPetStore 6 is to demonstrate how to build a web application with very few classes and no advanced coding skills. You just need to know plain Java and SQL.
The 6th version of JPetStore is the smallest one in the family. It uses just 24 java classes while keeping a good design and program structure. As we will see later on, you will find no code for dealing with JDBC, for creating objects or bind them or to handle transactions. What is more impressive is that you will not find any call to the MyBatis API. Although this sounds magical, you will see that the combination of MyBatis mappers and dependency injection lets you build applications without dependencies.
Program Structure
JPetStore 6 follows the typical maven project structure
/jpetstore <-- Maven pom.xml goes here.
/src
/main/
/java <-- Java code goes here.
/org/
/mybatis
/jpetstore
/domain <-- Business domain objects go here.
/persistence <-- Mapper interfaces go here.
/service <-- Application logic goes here.
/web
/actions <-- Presentation logic (actions) goes here.
/resources <-- Non java files go here.
/org
/mybatis
/jpetstore
/persistence <-- Mapper XML files go here.
/database
/webapp
/css
/images
/WEB-INF <-- web.xml and applicationContext.xml go here.
/jsp <-- JSP files go here.
Configuration files
Configuration files are read during application startup. Their purpose is to configure the three frameworks composing the application: Stripes, Spring and MyBatis. We will just need to configure two files: web.xml and applicationContext.xml.
web.xml
First of all we need to start Stripes, so we follow the Stripes manual to do so. The manual says that we should set up a dispatcher servlet and a filter. So let's go.
<filter>
<display-name>Stripes Filter</display-name>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>StripesDispatcher</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StripesDispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Stripes is able to search for ActionBean classes, for that purpose we must set up the base package it should search in.
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.Packages</param-name>
<param-value>org.mybatis.jpetstore.web</param-value>
</init-param>
</filter>
We are done with Stripes. Let's move on to the Spring side. According to Spring's reference manual we should add a Context listener to start up Spring. So let's add it:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
By default Spring will use /WEB-INF/applicationContext.xml if we don't specify a different file. The default is fine for us.
Now we have to let Stripes know that it will be running with Spring. This way we will be able to inject Spring beans directly into Stripes ActionBeans. For that purpose, following once again the Stripes manual, we set up an interceptor as follows below:
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
...
<init-param>
<param-name>Interceptor.Classes</param-name>
<param-value>net.sourceforge.stripes.integration.spring.SpringInterceptor</param-value>
</init-param>
</filter>
We are done with web.xml. As you may have notice, we have not set up any MyBatis 3 configuration yet. That configuration goes into the Spring's applicationContext.xml that we will see in the following section.
applicationContext.xml
As you already know applicationContext.xml is the Spring's configuration file. Spring is a dependency injection framework and it has to know which beans it must create and how to bind them together and that is what applicationContext.xml file is for. Let's have a deeper look into it.
The first and easiest thing we have to do is let Spring know where are our service beans. We will let Spring search them in our classpath so we just need to provide it the base package to search in:
<context:component-scan base-package="org.mybatis.jpetstore.service" />
NOTE Spring's component scan feature is not able to find MyBatis mappers. A mapper is not a plain bean and Spring would not know how to instantiate it. We will see how to search for mappers soon.
We will also need a DataSource and a TransactionManager. Given that this is a demo application we will use a test Spring DataSource that will create an HSQL in-memory database and load our database scripts into it and the standard Spring's DataSourceTransactionManager to handle transactions.
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:database/jpetstore-hsqldb-schema.sql"/>
<jdbc:script location="classpath:database/jpetstore-hsqldb-dataload.sql"/>
</jdbc:embedded-database> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
So far, all we have done is standard Stripes and Spring configuration and now it is time to move on to the MyBatis part. As you have learned in this manual to set up MyBatis with Spring you need at least two things: an SqlSessionFactoryBean and a mapper class. So let's go hands on. First define a SqlSessionFactoryBean:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
And now we need to setup our mappers. For that purpose we will use the MapperScannerConfigurer that works similar to Spring standard component scan. It will search our classpath for mapper classes and register them to MyBatis. Similar to Spring's component scan we must configure the base package to search in.
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.jpetstore.persistence" />
</bean>
To save some writing when building our mapper xml files we would want to be able to use short aliases for beans. The SqlSessionFactoryBean has the capability to search for beans and register their short names as aliases if we setup the typeAliasPackage property like the following:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
</bean>
Our application is now fully configured and ready to run. But before running it lets have a tour through the code to see how it looks like.
Code tour
JPetStore 6 is a typical MVC application with three layers: presentation, logic and data access.
Presentation
The presentation layer is composed by JSP files and Stripes ActionBeans. JSPs just use plain HTML, JSTL tags and Stripes tags so there is nothing especial about them for the sake of this sample. Stripes ActionBeans are like Struts actions or Spring MVC controllers so there is nothing especial with them either.
Given that we have integrated Stripes with Spring, we can inject our services into our ActionsBeans so you can just use them without caring about its creation or lookup. Have a look at CatalogActionBean:
@SessionScope
public class CatalogActionBean extends AbstractActionBean {
...
@SpringBean
private transient CatalogService catalogService;
...
public ForwardResolution viewCategory() {
if (categoryId != null) {
productList = catalogService.getProductListByCategory(categoryId);
category = catalogService.getCategory(categoryId);
}
return new ForwardResolution(VIEW_CATEGORY);
}
...
Note the @SpringBean annotation, that is an Stripes annotation that tells Stripes to look for that bean in Spring and inject it into this ActionBean.
Logic
Application logic is composed by plain Java beans that act as services and plain Java beans that act as domain objects. This layer is in charge of filling domain objects with database data and updating database data with the content of the domain objects. For this purpose this layer must be transactional, that is, it must be able to perform atomic database updates.
Let's have a look at OrderService code to see how all this is achieved:
@Service
public class OrderService { @Autowired
private ItemMapper itemMapper;
@Autowired
private OrderMapper orderMapper;
@Autowired
private LineItemMapper lineItemMapper; @Transactional
public void insertOrder(Order order) {
order.setOrderId(getNextId("ordernum"));
for (int i = 0; i < order.getLineItems().size(); i++) {
LineItem lineItem = (LineItem) order.getLineItems().get(i);
String itemId = lineItem.getItemId();
Integer increment = new Integer(lineItem.getQuantity());
Map<String, Object> param = new HashMap<String, Object>(2);
param.put("itemId", itemId);
param.put("increment", increment);
itemMapper.updateInventoryQuantity(param);
} orderMapper.insertOrder(order);
orderMapper.insertOrderStatus(order);
for (int i = 0; i < order.getLineItems().size(); i++) {
LineItem lineItem = (LineItem) order.getLineItems().get(i);
lineItem.setOrderId(order.getOrderId());
lineItemMapper.insertLineItem(lineItem);
}
}
The first thing you will notice is that there is no JDBC code in the service, nor it is any MyBatis code in it. You may think that we used the DAO pattern and database access code is in the database layer, but as we will see later, the database layer is built with MyBatis mappers, that are plain java interfaces, and that is why you will not find any call to MyBatis API in the whole application. It is just not needed.
The second thing you may have noticed is that there are no commits or rollbacks. That is because it uses the declarative transaction demarcation feature of Spring that is fully supported by MyBatis-Spring. The Spring's @Transactional annotation indicates that this method is transactional, that means that all updateInventoryQuantity, insertOrder and insertLineItem mapper calls must succeed or none.
Persistence
The persistence layer is composed of MyBatis mappers. Mappers are just plain Java interfaces and mapper XML files containing the SQL statements. There is no custom Java code in this layer. When the getOrder method is called on the OrderMapper interface, MyBatis will execute the getOrder SQL statement in OrderMapper.xml file and will populate the Order domain bean with retrieved data.
public interface OrderMapper {
List<Order> getOrdersByUsername(String username);
Order getOrder(int orderId);
void insertOrder(Order order);
void insertOrderStatus(Order order);
}
<mapper namespace="org.mybatis.jpetstore.persistence.OrderMapper"> <cache /> <select id="getOrder" resultType="Order" parameterType="int">
SELECT
BILLADDR1 AS billAddress1,
BILLADDR2 AS billAddress2,
BILLCITY,
BILLCOUNTRY,
BILLSTATE,
BILLTOFIRSTNAME,
BILLTOLASTNAME,
BILLZIP,
SHIPADDR1 AS shipAddress1,
SHIPADDR2 AS shipAddress2,
SHIPCITY,
SHIPCOUNTRY,
SHIPSTATE,
SHIPTOFIRSTNAME,
SHIPTOLASTNAME,
SHIPZIP,
CARDTYPE,
COURIER,
CREDITCARD,
EXPRDATE AS expiryDate,
LOCALE,
ORDERDATE,
ORDERS.ORDERID,
TOTALPRICE,
USERID AS username,
STATUS
FROM ORDERS, ORDERSTATUS
WHERE ORDERS.ORDERID = #{value}
AND ORDERS.ORDERID = ORDERSTATUS.ORDERID
</select>
...
</mapper>
NOTE You can easily add caching to your queries by adding a <cache /> element to your mapper xml file. Or, if you prefer, Spring lets you cache at a higher level, caching the whole call to a mapper or service method.
Running JPetStore
You may ask. Does all this work? Yes it does! Let's run it.
Let's assume you have a clean computer. These are the steps you should follow to have the sample running on Tomcat with Eclipse:
- Download and install a JDK 6 or later
- Download and upzip Eclipse
- Download and unzip Tomcat
- Run Eclipse
- Go to Git tab
- Clone the repo https://github.com/mybatis/jpetstore-6.git
- Select working directory, right click and select Import Projects (general)
- Go to Java EE tab
- Right click on jpetstore project and select "Configure/Convert to Maven Project"
- Right click on jpetstore project and select "run on server"
- Select Tomcat Server and set your installation directory
- JPetStore home page should be shown!!
Now you are ready to play with it, experiment with your own changes or whatever you want.
And remember that if you find a bug or something that is missing or can be improved (for example the missing tests!), fork the repo, change it, and open a pull request. Thanks in advance!!!
NOTE JPetStore 6 should run in any Servlet 2.5 y JSP 2.1 compliant Java server. Eclipse is not needed either, you can run the sample from your favorite IDE or the command line.
第八章 示例代码(MyBatis)的更多相关文章
- 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token
1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- ActiveMQ笔记(1):编译、安装、示例代码
一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...
- C#微信公众平台接入示例代码
http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...
- 编译opengl编程指南第八版示例代码通过
最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...
- 股票数据调用示例代码php
<!--?php // +---------------------------------------------------------------------- // | JuhePHP ...
- php示例代码之类似于C#中的String.Format方法
php示例代码之类似于C#中的String.Format方法 原文来自于 http://stackoverflow.com/questions/1241177/c-string-format-equ ...
- redis 学习笔记(2)-client端示例代码
redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redisson 一.Jedis的使用 <dependency> <groupId>redi ...
- 正则表达式学习笔记(附:Java版示例代码)
具体学习推荐:正则表达式30分钟入门教程 . 除换行符以外的任意字符\w word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s space,空白符 ...
随机推荐
- DMA&PIO
DMA&PIO DMA的英文拼写是“Direct Memory Access”,汉语的意思就是直接内存访问,是一种不经过CPU而直接从内存存取数据的数据交换模式.在DMA模式下,CPU只须 ...
- 【Android】第21章 2D图形和动画
分类:C#.Android.VS2015: 创建日期:2016-03-19 一.简介 Android系统定义了一系列独立的图形处理类,其中,2D图形处理类分别位于以下命名空间: Android.Gra ...
- linux工具大全
Linux Performance hi-res: observability + static + perf-tools/bcc (svg)slides: observabilityslides: ...
- Error LNK2019:Unresolved External Symbol 的解决方案
当头文件中声明了一个函数,但是在相应的源文件中却没有对该函数进行定义,则会出现为“解决的外部符号”(unresolved external symbol )错误.另外,当一个函数调用了外部的一个库文件 ...
- 每日英语:Why Food Companies Are Fascinated by the Way We Eat
Are you a cruncher? Or a 'smoosher'? cruncher:咬嚼者,咬碎 Some people crave the perfectly crispy crunch o ...
- 每日英语:Apple's Latest iPhone Puts Focus Back on Fingerprint Security
Apple's latest product launch could breathe new life into a technology that failed to take hold the ...
- solr中时区处理
solr.in.sh中的最后 # By default the start script uses UTC; override the timezone if needed SOLR_TIMEZONE ...
- ucenter通信失败和不能登录的解决
对于ucenter真是让人不省心,修改一下url,就通信失败了. 1.通信失败 然后后来怎么也改不好了,后来一步一步打log,发现是uc_server和uc_client不一致. 检查uc_serve ...
- ServiceMetadataBehavior 的 HttpsGetEnabled 属性设置为 True,而 HttpsGetUrl 属性是相对地址,但没有 https 基址
WCF 发布,本机正常,服务器报错,信息如下: ServiceMetadataBehavior 的 HttpsGetEnabled 属性设置为 True,而 HttpsGetUrl 属性是相对地址,但 ...
- Cookie application session
•Application 对象是存储于服务器的全局变量 •Cookie 存储信息于客户端 •Session 对象用于在服务器端存储用户的信息,在用户结束会话时被清除 1.将信息写入Cookies 中/ ...