近期公司要開發新的專案,要用struts2+mybatis+spring框架,所以學習了下,來自己的部落格發表下,希望能給大家帶來幫助!下
邊我把我的myschool開發的原始碼以及資料庫貼出來!
開發環境 myeclips+tomcate+sql server
開發技術 struts2+mybatis+spring jsp+java
一.建立web專案匯入必需的jar檔案
邊我把我的myschool開發的原始碼以及資料庫貼出來!
開發環境 myeclips+tomcate+sql server
開發技術 struts2+mybatis+spring jsp+java
一.建立web專案匯入必需的jar檔案
見專案。
二.建立並編寫配置檔案,配置檔案比較多。可能有點繁瑣。
二.建立並編寫配置檔案,配置檔案比較多。可能有點繁瑣。
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看看成功了
- ibatis_spring整合文件.rar (649.2 KB)