首頁 > 軟體

Struts2+Spring3+Hibernate3+MySQL簡單登入實現

2020-06-16 17:21:47

使用Struts2+Spring3+Hibernate3+MySQL實現簡單登入,下面就開始說小編寫的程式碼。

1.匯入相關的jar包

2.建立資料庫

1 create table account(
2 id int(10),
3 user varchar(50),
4 paw varchar(50)
5 );
6 insert into account values(1,'admin','admin'); 

3.建立包結構

 

4.組態檔的設定及程式碼

4.1 資料庫組態檔:db.properties

1 #jdbc
2 jdbc.driver=com.mysql.jdbc.Driver
3 jdbc.url=jdbc:mysql://127.0.0.1:3306/test
4 jdbc.username=root
5 jdbc.password=

4.2 spring組態檔:applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
 5     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
10         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
11         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
13         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
14         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
15     <context:component-scan base-package="ssh.ft"></context:component-scan>
16 
17     <context:property-placeholder location="classpath:configs/db.properties" />
18     <!-- datasource -->
19     <bean id="dataSource"
20         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
21         <property name="driverClassName" value="${jdbc.driver}" />
22         <property name="url" value="${jdbc.url}" />
23         <property name="username" value="${jdbc.username}" />
24         <property name="password" value="${jdbc.password}" />
25     </bean>
26     <!-- spring與hibernate整合 spring來管理session的建立、開啟和關閉 -->
27     <bean id="sessionFactory"
28         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
29         <!-- 通過組態檔的方式獲取資料來源,出現異常,未解決 -->
30         <property name="hibernateProperties">
31             <props>
32                 <prop key="connection.useUnicode">true</prop>
33                 <prop key="connection.characterEncoding">utf-8</prop>
34                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
35                 <prop key="hibernate.show_sql">true</prop>
36                 <prop key="hibernate.hbm2ddl.auto">update</prop>
37             </props>
38         </property>
39         <property name="dataSource" ref="dataSource" />
40         <property name="mappingResources">
41             <list>
42                 <!-- 以下用來列出所有的PO對映檔案 -->
43                 <value>configs/account.hbm.xml</value>
44             </list>
45         </property>
46     </bean>
47     <!-- 定義事物管理器,並位事物管理器設定上述所定義的session -->
48     <!-- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
49         <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> 
50         </bean> <tx:annotation-driven transaction-manager="transactionManager"/> -->
51 
52     <!-- 對事物管理器進行設定 表示對save、del、update開頭的方法應用事物 -->
53     <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
54         <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method 
55         name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" 
56         /> </tx:attributes> </tx:advice> -->
57 
58     <!-- 注入dao -->
59     <bean class="ssh.ft.dao.impl.AccountDaoImpl" id="accountDao">
60         <property name="sessionFactory" ref="sessionFactory"></property>
61     </bean>
62 
63     <!--注入service -->
64     <bean class="ssh.ft.service.impl.AccountManagerImpl" id="accountManager">
65         <property name="dao" ref="accountDao"></property>
66     </bean>
67 
68     <!--寫action和業務邏輯層依賴注入 -->
69     <!--將scope設定成prototype,預防了執行緒安全問題 -->
70     <bean class="ssh.ft.action.LoginAction" id="loginAction" scope="prototype">
71         <property name="accountManager" ref="accountManager"></property>
72     </bean>
73 </beans>

註:上述設定中註釋掉的事物部分,因為小編未使用到,所以也沒有認證,在設定時可去掉

4.4 設定完spring,可以先測試下設定是否正確

 1 package ssh.ft.test;
 2 
 3 import Java.util.List;
 4 
 5 import org.hibernate.SessionFactory;
 6 import org.junit.Test;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 import ssh.ft.entity.Account;
11 
12 public class DBTest {
13     @Test
14     public void test1() {
15         String config = "configs/applicationContext.xml";
16         ApplicationContext ac = new ClassPathXmlApplicationContext(config);
17         SessionFactory sf = ac.getBean(SessionFactory.class);
18         String sql = "from Account";
19         @SuppressWarnings("unchecked")
20         List<Account> list = sf.openSession().createQuery(sql).list();
21         System.out.println(list.size());
22     }
23 }

若設定正確則有如下結果:紅色框中的數位1表示資料庫中表account中有一條資料,若未出現正確結果,則需要檢查上述程式碼哪裡出錯,或者是jar的問題

因為小編也遇到過很多jar包不全之類的問題,務必正確之後再往下編寫,否則到後面錯誤或更多導致無法查詢

4.5 struts組態檔:struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <!-- 開發模式 -->
 7     <constant name="struts.devMode" value="true" />
 8     <!-- 將Action的建立交給spring來管理 -->
 9     <constant name="struts.objectFactory" value="spring" />
10 
11     <!-- 包含的組態檔 <include file="/configs/struts-user.xml"></include> -->
12     <package name="s2sh" extends="s2sh1">
13         <!-- Action的設定在這裡 -->
14         <action name="login" class="loginAction" method="login">
15             <result name="success">/WEB-INF/index.jsp</result>
16             <result name="error">/WEB-INF/login.jsp</result>
17         </action>
18     </package>
19     <package name="s2sh1" extends="struts-default">
20         <action name="tologin" >
21             <result >/WEB-INF/login.jsp</result>
22         </action>
23     </package>
24 </struts> 

4.6 hibernate組態檔:hibernate.cfg.xml 

 1 <!DOCTYPE hibernate-configuration PUBLIC
 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 3     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 4 <hibernate-configuration>
 5     <session-factory>
 6 
 7         <!-- 設定資料庫方言 -->
 8         <property name="dialect"> org.hibernate.dialect.MySQLDialect</property>
 9         <!-- 設定列印語句 -->
10         <property name="show_sql">true</property>
11         <property name="format_sql">true</property>
12         <!-- 設定執行緒安全的session -->
13         <property name="hibernate.current_session_context_class">thread</property>
14         <!--如果表不存在,則會幫你新建 -->
15         <property name="hbm2ddl.auto">create</property> 
16         <!-- 對映檔案 -->
17         <mapping resource="configs/account.hbm.xml" />
18     </session-factory>
19 </hibernate-configuration> 

hibernate組態檔還有一個實體對映檔案:account.hbm.xml

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="ssh.ft.entity.Account" table="account">
 6         <!-- 主鍵 -->
 7         <id name="id">
 8             <!-- 固定值:主鍵生成策略 -->
 9             <generator class="native"></generator>
10         </id>
11         <!-- 普通屬性 -->
12         <property name="user"></property>
13         <property name="paw"></property>
14     </class>
15 </hibernate-mapping> 

在hibernate.cfg.xml中的

  <mapping resource="configs/account.hbm.xml" />

這個語句裡面的account.hbm.xml就是指上述的account.hbm.xml組態檔

4.7 web.xml組態檔

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 3   <display-name>ssh</display-name>
 4     <!-- spring -->
 5     <listener>
 6         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 7     </listener>
 8     <context-param>
 9         <param-name>contextConfigLocation</param-name>
10         <param-value>classpath:configs/applicationContext.xml</param-value>
11     </context-param>
12   <filter>
13         <filter-name>struts2</filter-name>  
14         <filter-class>  
15           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
16         </filter-class>
17         <!-- 將struts.xml組態檔放置在src的其他位置無法存取,未解決
18         <init-param>  
19                <param-name>filterConfig</param-name>  
20             <param-value>classpath:configs/struts.xml</param-value>  
21         </init-param> 
22          -->
23     </filter>  
24     <filter-mapping>  
25         <filter-name>struts2</filter-name>  
26         <url-pattern>/*</url-pattern>  
27     </filter-mapping>
28 </web-app> 

IT145.com E-mail:sddin#qq.com