初步认识了struts2,并与hibernate进行整合,完成了一个登录的案例,下面贴源码
1.实体类User
public class User { private Integer id; private String uname; private String upass; ...省略set和get方法 }
2.实体类的映射文件
1 <class name="www.change.tm.bean.User" table="USERS"> 2 <id name="id" type="java.lang.Integer"> 3 <column name="ID" /> 4 <generator class="native" /> 5 </id> 6 <property name="uname" type="java.lang.String"> 7 <column name="UNAME" /> 8 </property> 9 <property name="upass" type="java.lang.String"> 10 <column name="UPASS" /> 11 </property> 12 </class>
3.hibernate.cfg.xml
1 <session-factory> 2 <!-- 配置hibernate的基本信息 --> 3 <property name="connection.username">****</property> 4 <property name="connection.password">****</property> 5 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 6 <property name="connection.url">jdbc:mysql:///hibernate3</property> 7 8 <!-- hibernate的基本配置 --> 9 <!-- 数据库方言 --> 10 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 11 12 <!-- 是否打印sql --> 13 <property name="show_sql">true</property> 14 <!-- 是否格式化sql --> 15 <property name="format_sql">true</property> 16 <!-- 生成表的策略 --> 17 <property name="hbm2ddl.auto">update</property> 18 19 <mapping resource="www/change/tm/bean/User.hbm.xml"/> 20 21 </session-factory>
4.action类
package www.change.tm.action; import java.util.Map; import org.apache.struts2.ServletActionContext; import www.change.tm.bean.User; import www.change.tm.dao.UserDao; import www.change.tm.dao.UserDaoImpl; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class Login extends ActionSupport{ //声明dao对象 UserDao userdao = new UserDaoImpl(); private String uname; private String upass; public String login(){ User user = userdao.login(uname, upass); if (user != null ) { // 存入到session会话中 ServletActionContext.getRequest().getSession() .setAttribute("user", user); return SUCCESS; } else { ServletActionContext.getRequest().setAttribute("msg", "用户名或者密码"); return ERROR; } } public void setUname(String uname) { this.uname = uname; } public void setUpass(String upass) { this.upass = upass; } }
5.dao层
5.1 BaseDao
public interface BaseDao {
public Session getSession();
}
5.2 UserDao
public interface UserDao { /* * 登录验证处理 */ public User login(String uname,String upass); }
5.3 UserDaoImpl
public class UserDaoImpl extends BaseDaoImpl implements UserDao{ @Override public User login(String uname, String upass) { //1.获取session对象 Session session = getSession(); //2.执行查询 Query createQuery = session.createQuery("from User u where u.uname=? and u.upass=?"); User user = (User)createQuery.setString(0, uname).setString(1, upass).uniqueResult(); /*总的写 User user =(User) getSession().createQuery("").setString(0, uname).setString(1, upass).uniqueResult(); */ //3.session关闭 HiberSessionFactory.closeSession(); return user; } }
5.4 BaseDaoImpl
public class BaseDaoImpl implements BaseDao{ @Override public Session getSession() { // TODO Auto-generated method stub return HibernateControl.getSession(); } }
6.util包里
引入HiberSessionFactory.java文件
1 package www.change.tm.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.service.ServiceRegistry; 7 import org.hibernate.service.ServiceRegistryBuilder; 8 9 /** 10 * Configures and provides access to Hibernate sessions, tied to the 11 * current thread of execution. Follows the Thread Local Session 12 * pattern, see {@link http://hibernate.org/42.html }. 13 */ 14 public class HiberSessionFactory { 15 16 /** 17 * Location of hibernate.cfg.xml file. 18 * Location should be on the classpath as Hibernate uses 19 * #resourceAsStream style lookup for its configuration file. 20 * The default classpath location of the hibernate config file is 21 * in the default package. Use #setConfigFile() to update 22 * the location of the configuration file for the current session. 23 */ 24 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); 25 private static org.hibernate.SessionFactory sessionFactory; 26 27 private static Configuration configuration = new Configuration(); 28 private static ServiceRegistry serviceRegistry; 29 30 static { 31 try { 32 System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 33 configuration.configure(); 34 System.out.println("configuration="+configuration); 35 serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 36 System.out.println("serviceRegistry="+serviceRegistry); 37 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 38 System.out.println(); 39 } catch (Exception e) { 40 System.err.println("%%%% Error Creating SessionFactory %%%%"); 41 e.printStackTrace(); 42 } 43 } 44 private HiberSessionFactory() { 45 } 46 47 /** 48 * Returns the ThreadLocal Session instance. Lazy initialize 49 * the <code>SessionFactory</code> if needed. 50 * 51 * @return Session 52 * @throws HibernateException 53 */ 54 public static Session getSession() throws HibernateException { 55 Session session = (Session) threadLocal.get(); 56 57 if (session == null || !session.isOpen()) { 58 if (sessionFactory == null) { 59 rebuildSessionFactory(); 60 } 61 session = (sessionFactory != null) ? sessionFactory.openSession() 62 : null; 63 threadLocal.set(session); 64 } 65 66 return session; 67 } 68 69 /** 70 * Rebuild hibernate session factory 71 * 72 */ 73 public static void rebuildSessionFactory() { 74 try { 75 configuration.configure(); 76 serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 77 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 78 } catch (Exception e) { 79 System.err.println("%%%% Error Creating SessionFactory %%%%"); 80 e.printStackTrace(); 81 } 82 } 83 84 /** 85 * Close the single hibernate session instance. 86 * 87 * @throws HibernateException 88 */ 89 public static void closeSession() throws HibernateException { 90 Session session = (Session) threadLocal.get(); 91 threadLocal.set(null); 92 93 if (session != null) { 94 session.close(); 95 } 96 } 97 98 /** 99 * return session factory 100 * 101 */ 102 public static org.hibernate.SessionFactory getSessionFactory() { 103 return sessionFactory; 104 } 105 /** 106 * return hibernate configuration 107 * 108 */ 109 public static Configuration getConfiguration() { 110 return configuration; 111 } 112 113 }
7.数据库