该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2011-02-17
最近复习一下spring和ibatis的相关知识,做个简单的项目实践一下
准备工作: myeclipse7.5+tomcat 6.0+MySQL 所需要的jar包: 下面是项目的实现: MySQL: 数据库名:xxx,表名:user 列名和字段类型: 项目结构: 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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 初始化参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- spring的启动 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts的启动 --> <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> </web-app> applicationContext.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" 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.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/xxx" /> <property name="username" value="root" /> <property name="password" value="kyo" /> </bean> <bean id="client" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="configLocation" value="classpath:com/kyo/resources/sqlmap-config.xml"> </property> </bean> <bean id="userDAO" class="com.kyo.DAO.IbatisUserDAO"> <property name="client" ref="client"></property> </bean> <bean id="userService" class="com.kyo.service.UserService"> <property name="userDAO" ref="userDAO"></property> </bean> <bean id="LoginAction" class="com.kyo.actions.Login"> <property name="userService" ref="userService"></property> </bean> </beans> sqlmap-config.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="com/kyo/resources/user.xml"/> </sqlMapConfig> action中Login类的设置: package com.kyo.actions; import java.util.List; import com.kyo.model.User; import com.kyo.service.IUserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; public class Login extends ActionSupport implements ModelDriven<User>, Preparable { private String id; private User user; private List<User> users; private IUserService userService; public String getId() { return id; } public void setId(String id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } public void setUserService(IUserService userService) { this.userService = userService; } public void prepare() throws Exception { if (id == null || id.length() == 0) user = new User(); else user = userService.getUserById(Integer.parseInt(id)); } public User getModel() { return user; } public String execute() { if (userService.isLogin(user)) return SUCCESS; return INPUT; } public String findAllUsers() { users = userService.getAllUsers(); if (users.size() != 0) { return SUCCESS; } else return ERROR; } } struts.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.objectFactory.spring.autoWire" value="type" /> <constant name="struts.objectFactory" value="spring" /> <include file="struts-default.xml"/> <package name="struts2" extends="struts-default"> <default-interceptor-ref name="paramsPrepareParamsStack" /> <!-- aciton的class为applicationContext.xml中的注册名 --> <action name="login" class="LoginAction" method="execute"> <result name="success">/success.jsp</result> <result name="input">/index.jsp</result> </action> <action name="show" class="LoginAction" method="findAllUsers"> <result name="success">/list.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts> 其它的诸如model层、DAO层、service层大同小异,不一一赘述。 页面很简单, 登录页面: 展示数据页面: 评论:这次实践中碰到很多异常,查阅资料中,也碰到很多相关性的问题。 比如: 1.Ibatis中的配置文件sqlmap-config.xml和user.xml头部很容易出错,需注意 sqlmap-config.xml <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> user.xml <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 如果出错,会报出 引用 Caused by: org.xml.sax.SAXParseException: Document root element "sqlMap", must match DOCTYPE root "sqlMapConfig". 2.Ibatis中client配置属性路径问题 <property name="configLocation" value="classpath:com/kyo/resources/sqlmap-config.xml"> </property> 3.Ibatis关键点,必须配置org.springframework.orm.ibatis.SqlMapClientFactoryBean,否则会出现空指针错误,ibatis不能起作用 4.另外很多问题都是由于jar包引用冲突,缺失jar包等问题,碰到这些问题需要小心排查,实在不行,百度知道,你就知道。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-02-18
使用Maven可以解决你最后一个问题。
|
|
返回顶楼 | |
发表时间:2011-02-18
这么点就完了
|
|
返回顶楼 | |
发表时间:2011-02-18
好久没搞J2EE,都快忘了.
|
|
返回顶楼 | |
发表时间:2011-02-18
lz是神马浏览器?看起来好高端..
|
|
返回顶楼 | |
发表时间:2011-02-18
都是火星人呀
|
|
返回顶楼 | |
发表时间:2011-02-18
ibatis怎么没用到啊。。我没用过它,就看见一个配置文件
|
|
返回顶楼 | |
发表时间:2011-02-18
wxq594808632 写道 lz是神马浏览器?看起来好高端..
很显然 Chrome |
|
返回顶楼 | |
发表时间:2011-02-18
求源码,学习编码经验
|
|
返回顶楼 | |
发表时间:2011-02-18
没必要加入那么多jar包的。
|
|
返回顶楼 | |