struts2+ibatis+spring框架整合
lspengjiang
2011-09-05
近期公司要开发新的项目,要用struts2+ibatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!下
<?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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/configs/sqlServer.properties</value> </property> </bean> <!--配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driver}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.user}</value> </property> <property name="password"> <value>${jdbc.pwd}</value> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:com/test/sqlMapper/mybatis_config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="loginDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.test.dao.ILoginDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="loginAction" class="com.test.action.LoginAction"> <property name="loginDao" ref="loginDao"></property> </bean> </beans>
jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=login jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc.user=sa jdbc.pwd=
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="userinfo" type="com.test.entity.UserInfo"/> </typeAliases> <mappers> <mapper resource="com/test/sqlMapper/loginMapper.xml"/> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.dao.ILoginDao"> <resultMap type="userinfo" id="userMap"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> </resultMap> <select id="getUser" parameterType="String" resultMap="userMap"> select * from userinfo where username=#{userName} </select> </mapper>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <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>
package com.test.dao; import java.util.List; public interface ILoginDao { public List getUser(String userName); }
package com.test.entity; public class UserInfo { private int id; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
package com.test.action; import java.util.List; import com.test.dao.ILoginDao; public class LoginAction { private ILoginDao loginDao; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public ILoginDao getLoginDao() { return loginDao; } public void setLoginDao(ILoginDao loginDao) { this.loginDao = loginDao; } public String execute(){ String userName = getUsername(); String password = getPassword(); System.out.println("userName:"+userName+"\n"+"password:"+password); List list = loginDao.getUser(userName); if(list.size()>0){ return "success"; }else{ return "error"; } } }
<body> <s:form action="login" method="post"> <s:textfield name="username" label="用户名:"></s:textfield> <s:textfield name="password" label="密码:"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body> |
|
afro
2011-09-21
你都抽象出来了一个user类,为什么登陆action还要get,set username和password属性呢???直接 get set user 不就行了吗?有点多此一举的感觉。。。
|
|
李学力
2011-09-27
写得太简单了吧,至少自己封装点东西才好用啊,不如关联对象的保存....
|
|
lzeus
2011-11-02
spring用的2。5还是3?
可以支持myibatis3了? 我一直用的ibatis2.3 |
相关讨论
相关资源推荐
- 《自然语言处理实战入门》第二章:NLP 前置技术 ---- NLP开源工具包与云服务提供商
- Open-Xchange 修复 OX App Suite 中的RCE等漏洞
- 在Ubuntu 9.04/9.10 桌面版上安装 Open-Xchange 社区开源免费版--2009.12.23--北京守望小方翻译+实际操作...
- LLMs:《BLOOM: A 176B-Parameter Open-Access Multilingual Language Model》翻译与解读
- 开源地图引擎openlayers_Openlayer4 - 最好最强大的开源地图引擎
- android-open-project 安卓开源项目汇总
- 【C++】开源:跨平台Excel处理库-libxlsxwriter配置使用
- LLMs:OpenAI 官方文档发布提高 GPT 使用效果指南—GPT最佳实践(GPT best practices)翻译与解读
- 云栖大会开源重磅升级!PolarDB-X v2.2: 企业级和国产化适配
- open-falcon plus——安装