spring+springmvc+hibernate template禮品管理系統html
1.簡單介紹java
如標題所示,這篇文章簡單寫了一個基於spring+springmvc+hibernate template的禮品管理系統,適合初學者,這是個人第三篇文章,但願能夠經過這篇文章讓同窗瞭解這個系統基本流程,雖然逐漸退出主流,可是還有不少地方思路值得咱們學習。若有興趣,能夠隨時聯繫我:1763907618.mysql
2.環境搭建web
個人項目測試成功,前臺jsp,沒有美化,主要是後臺功能都能實現。spring
jdk:1.6;sql
數據庫:mysql5.6;數據庫
基於hibernate的jar包,cglib.jar可能還會有衝突,每次發佈項目須要刪除。session
基於springmvc的jar包,mvc
3.成果基本樣式app
4.詳細內容
HibernateTemplate的經常使用方法。
delete(Object entity): 刪除指定持久化實例。
find(String queryString): 根據 HQL 查詢字符串來返回實例集合。
save(Object entity): 保存新的實例。
update(Object entity): 更新實例的狀態,要求entity 是持久狀態。
主要就是一個增刪改查的過程。上面這四個是咱們經常使用方法。
由於個人項目用到了hibernate的逆向工程,因此數據庫是自動生成。
下面就寫一個登陸實例,由於登陸是一個查詢的過程,因此咱們將用find方法。
a.建立項目,導入jar包,這些我就不介紹了。
b.建立
com.controller,
com.dao,
com.model,
com.service
包,在裏面分別創建java
usercontroller,
userdao,
user,
userservice
注意開頭大寫。
c.編寫hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost:3306/bgift?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.username">root</property> <property name="connection.password">1478</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="myeclipse.connection.profile">Bgift</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.connection.autocommit">true</property> <mapping class="com.model.User" />
/////其中 mapping class="com.model.User"是要注意的。路徑問題。
</session-factory> </hibernate-configuration>
d.編寫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"> <!-- log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- 字mvc --> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符編碼過濾器,結覺亂碼問題 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
e.編寫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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.*"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="" p:suffix=".jsp"> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>
f.編寫com.model.user.java
package com.model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="user") public class User { int id; String username; String password; @Id @Column @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
g.編寫com.model.userdao.java
package com.dao; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.model.User; @Repository public class UserDao extends HibernateDaoSupport{ @Resource public void setSessionFactoryOverride(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } /////////////*登陸*//////////////////////////////*登陸*////////////////////////*登陸*/ @SuppressWarnings("unchecked") public List<User> userlogin(User user){ String hql= "from User where username=? and password=?"; List<User> t=this.getHibernateTemplate().find(hql,new Object[]{user.getUsername(),user.getPassword()}); return t; } }
h.編寫com.model.userservice.java
package com.service; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.model.User; @Component public class UserService { @Resource UserDao userdao; public UserDao getUserdao() { return userdao; } public void setUserdao(UserDao userdao) { this.userdao = userdao; } /////////////*登陸*//////////////////////////////*登陸*//////////*登陸*/ public List<User> userlogin(User user){ return userdao.userlogin(user); } }
i.編寫com.model.usercontroller.java
package com.Controller; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.model.User; import com.service.UserService; @Controller public class UserController { @Resource UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } /////////////*登陸*//////////////////////////////*登陸*/ @RequestMapping("userlogin.do") public String findAll(HttpSession session,User user,HttpServletResponse response){ List<User> listuser = userService.userlogin(user); if(listuser.size()>0){ session.setAttribute("listuser", listuser); return "loginsuccess"; }else{ response.setContentType("text/html; charset=UTF-8"); //轉碼 PrintWriter out; try { out = response.getWriter(); out.flush(); out.println("<script>"); out.println("alert('帳號或密碼錯誤!');"); out.println("history.back();"); out.println("</script>"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "index"; } } }
j.編寫前臺index.jsp
<form name="formuser" id="formuser" method="post" action="userlogin.do"> <input type="text" id="username" name="username" placeholder="請輸入帳號" /><br /><br /> <input type="password" class="index_mi" id="password" name="password" placeholder="請輸入密碼" /><br /><br /> <input class="index_z_fi" type="submit" value="登陸" /> </form>
k.編寫前臺loginsuccess.jsp
添加${sessionScope.listuser[0].username}
接受後臺傳來的session.setAttribute("listuser", listuser);值。
5.總結
一個登陸寫完,其實我只是把文件中的一部分賦值過來,便於瞭解。
在寫這個系統以前,有過不少問題:
a.路徑問題,return 後面接jsp路徑的話,要把路徑寫對,在提交時候XXX.do要寫對。不然404。
b.jar包衝突問題,當初困擾我好久,後來把衝突包刪除,再發布後還會出現,索性把jar包都放到lib文件夾下。
c.亂碼,在web.xml下配置,相互傳值要注意。比較好改。
最後:文檔只是一部分,若有須要請聯繫1763907618。記得關注和點贊,謝謝各位。