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; } }
posted on 2017-11-10 20:25 PoeticalJustice 阅读(179) 评论(0) 编辑 收藏 举报