mybatis+spring+struts2框架整合
2012-08-07 22:18 java线程例子 阅读(370) 评论(0) 编辑 收藏 举报 近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!下
边我把我的myschool开发的源代码以及数据库贴出来!
开发环境 myeclips+tomcate+sql server
开发技术 struts2+mybatis+spring jsp+java
一.创建web项目导入必需的jar文件
![](http://dl.iteye.com/upload/picture/pic/97408/b99a917b-2387-336a-be1d-248533a5c06b.jpg)
二.创建并编写配置文件,配置文件比较多。可能有点繁琐。
1.创建并编写ApplicationContext.xml
边我把我的myschool开发的源代码以及数据库贴出来!
开发环境 myeclips+tomcate+sql server
开发技术 struts2+mybatis+spring jsp+java
一.创建web项目导入必需的jar文件
![](http://dl.iteye.com/upload/picture/pic/97408/b99a917b-2387-336a-be1d-248533a5c06b.jpg)
二.创建并编写配置文件,配置文件比较多。可能有点繁琐。
1.创建并编写ApplicationContext.xml
<?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>
2.配置数据源当然要有个数据源 属性文件了sqlServer.properties
jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=login jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc.user=sa jdbc.pwd=
我的数据库密码为空pwd当然也要空了。
3.mybatis的配置文件mybatis_config.xml
<?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>
4.在mybatis_config.xml中的中有个映射文件loginMapper.xml
<?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>
5.接下来就是web.xml
<?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>
好了所有的配置文件都已经配置好了
三.该写JAVA文件了 接口,实体辅助类,还有action
1.写接口 ILoginDao.java
package com.test.dao; import java.util.List; public interface ILoginDao { public List getUser(String userName); }
2.实体辅助类UserInfo.java
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; } }
3.action 类 LoginAction.java
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"; } } }
四.该写页面了 页面有三个如下
1. login.jsp
<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>
2.success.jsp 就几个字 登陆成功
3.error,jsp 就几个字 登录失败
五.创建数据库 login 创建表 userinfo 里面就三个字段 id username password 数据库我已经传到这文章里边了,下载后可以直接用
六.好了快启动tomcat看看成功了