Struts2+Spring2.5+Ibatis2.3图书管理
看了Ibatis也有好长时间啦,一直也没有和Struts2+Spring2.5进行整合,今天也没有什么任务,写一个简单的例子和大家一起分享下。
首先建立一下简单的数据库表:sbook
drop table if exists sbook;create table if not exists sbook(id int primary key auto_increment,title varchar(50),author varchar(20),total int,price float,isbn varchar(20),publisher varchar(50));
antlr-2.7.2.jaraspectjweaver.jarcommons-collections.jarcommons-dbcp.jarcommons-logging-1.0.4.jarcommons-pool.jar//Ibatis整体Jar文件ibatis-2.3.0.677.jar//Mysql JDBC驱动mysql-connector.jar//Spring整体包spring.jar //struts2必须 freemarker-2.3.8.jar //struts2必须ognl-2.6.11.jar //struts2核心包struts2-core-2.0.11.2.jar//struts2整合Spring插件 struts2-spring-plugin-2.0.11.2.jar //struts2必须xwork-2.0.5.jar
<?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"><!-- 配置Spring --><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/com/us/jack/config/applicationContext.xmlWEB-INF/classes/com/us/jack/config/applicationContext-services.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置Struts2 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
package com.us.jack.pojo;/** * 图书类 * @author jack * */public class SBook {private int id;private String title;private String author;private int total;private float price;private String isbn;private String publisher; //下面是Getter和Setter方法 ……}<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="dataSource"value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql://localhost:3306/ibatisstudy" /><property name="username" value="root" /><property name="password" value="jack" /></bean><!-- SqlMap setup for iBATIS Database Layer --><bean id="sqlMapClient"/><property name="dataSource" ref="dataSource" /></bean><!-- 注入BookDAO层 --><bean id="sbookDAO" ref="sqlMapClient" /></bean></beans>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"><sqlMapConfig><sqlMap resource="com/us/jack/dao/impl/maps/SBook.xml"/></sqlMapConfig>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap><typeAlias alias="sbook" type="com.us.jack.pojo.SBook" /><!-- 添加一本新书 --><insert id="saveBook" parameterresultparameterparameterparameterresultparameterparameterresultname="code"><?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="sbookServices" ref="sbookDAO"/></bean><!-- 将BookAction交给Spring控制 --><bean id="sbookAction" ref="sbookServices"/></bean><!-- Transaction manager for a single JDBC DataSource --><bean id="transactionManager"ref="dataSource" /></bean><aop:config><!-- 管理事务操作 --><aop:pointcut id="servicesPointcut"expression="execution(* com.us.jack.services.*.*(..))" /><aop:advisor advice-ref="txAdvice"pointcut-ref="servicesPointcut" /></aop:config><!-- 事务控制 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="get*" read-only="true" /></tx:attributes></tx:advice></beans>