JAVAWEB 一一 Sturts2+ibatis(框架,Sturts2,用action代替servlet)
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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <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> </web-app>
sqlMapConfig.xml
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled ="true"
lazyLoadingEnabled="true"
enhancementEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="true"/>
<transactionManager type ="JDBC">
<dataSource type ="JNDI">
<property name ="DataSource" value ="java:comp/env/jdbc/emp" />
</dataSource>
</transactionManager>
<sqlMap resource ="com/struts2/entity/userMap.xml" />
<sqlMap resource ="com/struts2/entity/bookMap.xml" />
</sqlMapConfig >
bookMap.xml
<?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 namespace="book"> <typeAlias alias="book" type="com.struts2.entity.Book"/>
<resultMap class="Book" id="bookResultMap">
<result property="bookId"/>
<result property="bname"/>
<result property="bprice"/>
<result property="bcount"/>
<result property="bds"/>
</resultMap> <select id="getBookList" resultMap="bookResultMap">
select bookId,bname,bprice,bcount,bds from(
select e.*,rownum rn from(
select * from Book where 1=1
<isNotNull property="bname" prepend="and">
bname like '%$bname$%'
</isNotNull>
<isNotEmpty property="bprice" prepend="and">
bprice <![CDATA[ <= $bprice$ ]]>
</isNotEmpty>
)e
)
where <![CDATA[ rn>=#startRow# and rn <=#endRow# ]]>
</select> <select id="getTotalCount" resultClass="java.lang.Integer">
select count(*)from Book where 1=1
<isNotNull property="bname" prepend="and">
bname like '%$bname$%'
</isNotNull>
<isNotEmpty property="bprice" prepend="and">
bprice <![CDATA[ <= $bprice$ ]]>
</isNotEmpty> </select> <insert id="addBook" parameterClass="Book">
insert into BOOK(bookId,bname,bprice,bcount,bds)values
(#bookId#,#bname#,#bprice#,#bcount#,#bds#)
</insert> <select id="getBookById" resultMap="bookResultMap">
select * from book where bookId =#bookId#
</select> <update id="updateBook" parameterClass="Book">
update book set bname=#bname#,bprice=#bprice#,bcount=#bcount#,bds=#bds#
where bookId=#bookId#
</update> <delete id="delBook" parameterClass="java.lang.Integer">
delete from book where bookId=#bookId#
</delete> <select id="getBookId" resultClass="java.lang.Integer">
select Seq_id.Nextval from dual
</select>
</sqlMap>
userMap.xml
<?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 namespace="user"> <typeAlias alias="User" type="com.struts2.entity.User"/>
<parameterMap id="userParameterMap" class="User" >
<parameter property="userid" />
<parameter property="username" />
<parameter property="userpassword" />
<parameter property="role" />
<parameter property="state" />
<parameter property="email" />
<parameter property="createdate" />
</parameterMap>
<resultMap id="userResultMap" class="User">
<result property="userid" column="USERID"/>
<result property="username" column="USERNAME"/>
<result property="userpassword" column="USERPASSWORD"/>
<result property="role" column="ROLE"/>
<result property="state" column="STATE"/>
<result property="email" column="EMAIL"/>
<result property="createdate" column="CREATEDATE"/>
</resultMap>
<select id="getUserByName" resultMap="userResultMap">
select * from User_New where username =#username#
</select>
</sqlMap>
sturt.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" /> <global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
</package>
<include file="user.xml"></include>
<include file="book.xml"></include>
</struts>
book.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> -->
<constant name="struts.devMode" value="true"/> <package name="book" namespace="/" extends="struts-default"> <action name="getBookList" class="com.struts2.action.BookAction">
<param name="list">list</param>
<result name="success">/jsp/list.jsp</result>
</action> <action name="addBook" class="com.struts2.action.BookAction"> <result name="success" >/jsp/list.jsp</result>
<result name="addError" >/jsp/error.jsp</result>
</action>
<action name="getbook" class="com.struts2.action.BookAction"> <result name="success" >/jsp/list.jsp</result>
<result name="updateError" >/jsp/error.jsp</result>
<result name="bookObj" >/jsp/updateBook.jsp</result>
</action> <action name="update" class="com.struts2.action.BookAction"> <result name="success" >/jsp/list.jsp</result>
<result name="updateError" >/jsp/error.jsp</result>
<result name="bookObj" >/jsp/updateBook.jsp</result>
</action>
<action name="delbook" class="com.struts2.action.BookAction"> <result name="success" >/jsp/list.jsp</result>
<result name="delError" >/jsp/error.jsp</result> </action> </package>
</struts>
user.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> -->
<constant name="struts.devMode" value="true"/> <package name="user" namespace="/" extends="struts-default">
<action name="login" class="com.struts2.action.UserAction">
<result name="success" type="redirectAction">getBookList</result>
<result name="input">/jsp/login.jsp</result>
</action>
</package> </struts>
User.action
package com.struts2.action; import com.opensymphony.xwork2.Action;
import com.struts2.service.UserService;
import com.struts2.service.impl.UserServiceImpl; public class UserAction implements Action { private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} UserService service = new UserServiceImpl() ; public String execute() throws Exception { boolean isLogin =service.isLogin(username, password);
if(isLogin){
return SUCCESS;
}else{
return INPUT;
} } }
Book.acton
package com.struts2.action; import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.StrutsStatics; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.struts2.entity.Book;
import com.struts2.service.BookService;
import com.struts2.service.impl.BookServiceImpl; public class BookAction implements Action {
private Book book;
private String flag;
private Integer bookId;
private Integer totalCount;
private Integer totalPage; public Book getBook() {
return book;
} public void setBook(Book book) {
this.book = book;
} public Integer getTotalPage() {
return totalPage;
} public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
} public Integer getTotalCount() {
return totalCount;
} public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
} public Integer getBookId() {
return bookId;
} public void setBookId(Integer bookId) {
this.bookId = bookId;
} /* public String getBookId() {
return bookId;
} public void setBookId(String bookId) {
this.bookId = bookId;
}*/ public String getFlag() {
return flag;
} public void setFlag(String flag) {
this.flag = flag;
} public static final int PAGE_SIZE=5; public List<Book> getList() {
return list;
} public void setList(List<Book> list) {
this.list = list;
} private Integer pageIndex; public Integer getPageIndex() {
return pageIndex;
} public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
} BookService service = new BookServiceImpl();
List<Book>list = new ArrayList<Book>(); public String execute() throws Exception {
if("2".equals(flag)){ int bookId=service.getBookId();
book.setBookId(bookId);
boolean isAdd=service.addBook(book); book=null; if(!isAdd){
return "addError";
}
}else if("4".equals(flag)){ Book book2=service.getBookById(bookId); HttpServletRequest request=(HttpServletRequest)
ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
request.setAttribute("book", book2); return "bookObj";
}else if("5".equals(flag)){
boolean ismodify = service.modifyBook(book);
if(!ismodify){
return "updateError";
}
}else if("3".equals(flag)){
boolean isdel=service.delBook(bookId);
if(!isdel){
return "delError";
}
} if(pageIndex==null){ pageIndex=1;
} totalCount =service.getTotalCount(book);
list = service.getBookList(pageIndex, PAGE_SIZE,book);
totalPage = totalCount%PAGE_SIZE==0 ? (totalCount/PAGE_SIZE) : (totalCount/PAGE_SIZE+1);
return SUCCESS;
} }
UserDao.java
package com.struts2.dao; import com.struts2.entity.User; public interface UserDao {
public User getUserByName(String username); }
UserDaoImpl.java
package com.struts2.dao.impl; import java.sql.SQLException;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.struts2.dao.UserDao;
import com.struts2.entity.User;
import com.struts2.util.SqlMapClientUtil; public class UserDaoImpl implements UserDao{
SqlMapClient sqlMapClient = SqlMapClientUtil.getSqlMapClient();
User user=null;
public User getUserByName(String username){
try {
//("user.getUserByName")命名空间 id username
user = (User)sqlMapClient.queryForObject("user.getUserByName", username);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
} }
BookDao.java
package com.struts2.dao; import java.util.List;
import com.struts2.entity.Book; public interface BookDao { public List<Book>getBookList(int pageIndex,int pageSize,Book book); public int getTotalCount(Book book); public Book getBookById(int bookId); public Book getBookById(String name); public boolean addBook(Book book); public boolean modifyBook(Book book); public boolean delBook(int bookId); public int getBookId();
}
BookDaoImpl.java
package com.struts2.dao.impl; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.ibatis.sqlmap.client.SqlMapClient;
import com.struts2.dao.BookDao;
import com.struts2.entity.Book;
import com.struts2.util.SqlMapClientUtil; public class BookDaoImpl implements BookDao {
SqlMapClient sqlMapClient =SqlMapClientUtil.getSqlMapClient(); @Override
public List<Book> getBookList(int pageIndex, int pageSize,Book book) {
Map<String,Object> map = new HashMap<String,Object>();
List<Book>list = new ArrayList<Book>(); try {
int startRow=(pageIndex-1)*pageSize+1;
int endRow=pageIndex*pageSize;
map.put("startRow", startRow);
map.put("endRow", endRow);
if(book!=null){
map.put("bname",book.getBname()); map.put("bprice", book.getBprice()); }
list = sqlMapClient.queryForList("book.getBookList", map);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
} @Override
public int getTotalCount(Book book) {
Map<String,Object> map = new HashMap<String,Object>(); int count = 0;
try {
if(book!=null){
map.put("bname",book.getBname());
map.put("bprice", book.getBprice()); }
count =(Integer)sqlMapClient.queryForObject("book.getTotalCount",map);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
} @Override
public Book getBookById(int bookId) { Book book=null;
try {
book = (Book)sqlMapClient.queryForObject("book.getBookById", bookId);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return book;
} public Book getBookById(String name) {
// TODO Auto-generated method stub
return null;
} @Override
public boolean addBook(Book book) {
SqlMapClient sqlMapClient=SqlMapClientUtil.getSqlMapClient();
try {
int count = sqlMapClient.update("book.addBook",book);
if(count>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} @Override
public boolean modifyBook(Book book) { try {
int count = sqlMapClient.update("book.updateBook",book);
if(count>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return false;
} @Override
public boolean delBook(int bookId) {
int count;
try {
count = sqlMapClient.delete("book.delBook", bookId);
if(count>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public int getBookId(){
int bookId=0;
try {
bookId = (Integer)sqlMapClient.queryForObject("book.getBookId");
return bookId;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookId;
} }
User.java
package com.struts2.entity; import java.util.Date; public class User {
private int userid;
private String username;
private String userpassword;
private int role;
private int state;
private String email;
private Date createdate; public User(String username, String userpassword, int role, int state,
String email, Date createdate) {
super();
this.username = username;
this.userpassword = userpassword;
this.role = role;
this.state = state;
this.email = email;
this.createdate = createdate;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
}
Book.java
package com.struts2.entity; public class Book {
private int bookId;
private String bname;
private String bprice;
private int bcount;
private String bds; public Book( String bname, String bprice, int bcount, String bds) {
super(); this.bname = bname;
this.bprice = bprice;
this.bcount = bcount;
this.bds = bds;
} public Book() {
super();
// TODO Auto-generated constructor stub
} public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBprice() {
return bprice;
}
public void setBprice(String bprice) {
this.bprice = bprice;
}
public int getBcount() {
return bcount;
}
public void setBcount(int bcount) {
this.bcount = bcount;
}
public String getBds() {
return bds;
}
public void setBds(String bds) {
this.bds = bds;
} }
UserService.java
package com.struts2.service; public interface UserService { public boolean isLogin(String username,String password); }
UserServiceImpl.java
package com.struts2.service.impl; import com.struts2.dao.UserDao;
import com.struts2.dao.impl.UserDaoImpl;
import com.struts2.entity.User;
import com.struts2.service.UserService; public class UserServiceImpl implements UserService { public boolean isLogin(String username, String password) { UserDao userDao = new UserDaoImpl();
User user = userDao.getUserByName(username); if (user != null) { String pwd = user.getUserpassword(); if (pwd.equals(password)) {
return true;
}
return false;
} else {
return false;
}
}
}
BookService.java
package com.struts2.service; import java.util.List;
import com.struts2.entity.Book; public interface BookService { public List<Book>getBookList(int pageIndex,int pageSize,Book book); public int getTotalCount(Book book); public Book getBookById(int bookId); public Book getBookById(String name); public boolean addBook(Book book); public boolean modifyBook(Book book); public boolean delBook(int bookId); public int getBookId(); }
BookServiceImpl.java
package com.struts2.service.impl; import java.util.List;
import com.struts2.dao.BookDao;
import com.struts2.dao.impl.BookDaoImpl;
import com.struts2.entity.Book;
import com.struts2.service.BookService; public class BookServiceImpl implements BookService {
BookDao dao = new BookDaoImpl();
public List<Book> getBookList(int pageIndex, int pageSize,Book book) {
//
return dao.getBookList(pageIndex, pageSize, book);
} public int getTotalCount(Book book) { return dao.getTotalCount(book);
} public Book getBookById(int bookId) { return dao.getBookById(bookId); } public Book getBookById(String name) {
// TODO Auto-generated method stub
return null;
} public boolean addBook(Book book) { return dao.addBook(book);
} public boolean modifyBook(Book book) {
//
return dao.modifyBook(book);
} public boolean delBook(int bookId) {
//
return dao.delBook(bookId);
} @Override
public int getBookId() {
// TODO Auto-generated method stub
return dao.getBookId();
}
}
SqlMapClientUtil.java
package com.struts2.util; import java.io.IOException;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class SqlMapClientUtil {
public static SqlMapClient getSqlMapClient() {
SqlMapClient sqlMapClient = null;
Reader reader = null; try {
reader = Resources.getResourceAsReader("sqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sqlMapClient; }
}
JAVAWEB 一一 Sturts2+ibatis(框架,Sturts2,用action代替servlet)的更多相关文章
- JAVAWEB 一一 Spirng(框架,IOC控制反转,DI依赖注入)
jar包 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...
- JAVAWEB 一一 Hibernate(框架)
实体类关联数据库字段,操作实体类,HQL语句对数据结构CRUD) 引入jar包 配置文件 hibernate.cfg.xml User.hbm.xml <?xml version="1 ...
- iBatis框架batch处理优化 (转)
为什么要做batch处理 这个问题我就不解释了,因为我想你们肯定能比我解释的更好!如果你真的不知道,那就到Google上去搜索一下吧☻Oracle回滚段 这个问题偶也不很明白,只是 ...
- spring+struts2+ibatis 框架整合以及解析
一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...
- iBatis框架基本使用
iBatis框架是Java持久层开发框架,说白了就是前人写了一部分代码(针对数据库操作),我们要做的就是再次开发,拿来框架直接使用. 我们自己开发时,dao层的sql语句都是写死在程序中的,如果查询条 ...
- iBatis框架简介
一.为啥使用iBatis? 在 Hibernate.JPA 这样的一站式对象 / 关系映射(O/R Mapping)解决方案盛行之前,iBaits 基本是持久层框架的不二选择.即使在持久层框架层出不穷 ...
- 【转】深入分析 iBATIS 框架之系统架构与映射原理
深入分析 iBATIS 框架之系统架构与映射原理 iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Ja ...
- 深入分析 iBATIS 框架之系统架构与映射原理--转载
http://www.ibm.com/developerworks/cn/java/j-lo-ibatis-principle/ iBATIS 通过 SQL Map 将 Java 对象映射成 SQL ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- spring struts2 ibatis 框架结构图
spring struts2 ibatis 框架结构图
随机推荐
- JavaScript类继承, 用什么方法好
JavaScript类继承, 用什么方法好 一个实例: 基类Car: function Car(color, year) { this.name = "car"; this.col ...
- 2-Zookeeper、HA安装
1.Zookeeper安装 1.解压 zookeeper 到安装目录中/opt/app/zookeeper 中. 2.在安装目录下创建data和logs两个目录用于存储数据和日志: cd /opt/a ...
- HDFS在web端无法访问文件
解决办法1: [root@djt002 hadoop]# vi /etc/selinux/config 改为 SELINUX=disabled 解决办法2: 查看你的$HADOOP_HOME/etc/ ...
- python变量存储和深浅拷贝
python的变量及其存储 在高级语言中,变量是对内存及其地址的抽象.对于python而言,python的一切变量都是对象,变量的存储,采用了引用语义的方式,存储的只是一个变量的值所在的内存地址,而不 ...
- Patrick Hughes - 错觉3D雕塑艺术
Pictures Patrick Hughes (artist) From Wikipedia, the free encyclopedia Patrick Hughes. Leaning on a ...
- 8.2.优化SQL语句
8.2.优化SQL语句 数据库应用程序核心操作逻辑都是通过执行SQL语句来执行,不管是直接通过解释器还是通过后台API提交. 调优手册里面的这一节内容帮助各种各样MySQL程序加快速度.手册包括SQL ...
- 《算法》第二章部分程序 part 1
▶ 书中第二章部分程序,加上自己补充的代码,包括插入排序,选择排序,Shell 排序 ● 插入排序 package package01; import java.util.Comparator; im ...
- web session 原理1
原理 我们都知道,浏览器无状态的.浏览器是操作不了session的,浏览器能够做的只是传递cookie,每次都传递. 把当前主机下的,和当前请求相同域下的cookie 传递到服务器去,只要cooki ...
- Centos7 搭建Gitlab服务器并配置项目全过程
https://blog.csdn.net/Abysscarry/article/details/79402695 gitlab与jenkins整合搭建 https://blog.csdn. ...
- eclipse gradle插件 org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradle-3.4-bin.zip'.
eclipse安装gradle后出现如下异常: org.gradle.tooling.GradleConnectionException: Could not install Gradle distr ...