Spring Hibernate结合

需要applicationcontext.xml hibernate.cfg.xml 

建立UserPo.hbm.xml  用来连接表中属性

建立数据库

建立工程iocWebApp(选择WebDynamic Web Project) 下一步

Project name中填写iocWebApp

Target Runtime 选择 ApacheTomcat v6.0

Configurations 选择 defaultconfigutation for Apache Tomcat v6.0

创建包结构如下

根据功能,创建顺序如下

//UserPo.java

=======================================================================

public class UserPo {

private String userName;

private String userId;

private String userPwd;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getUserPwd() {

return userPwd;

}

public void setUserPwd(String userPwd) {

this.userPwd = userPwd;

}

}

=======================================================================

//接口创建->UserDao.java

=======================================================================

import com.po.UserPo;

public interface UserDao {

public UserPo getUser(String userId);

}

//UserDaoImpl01.java

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.springframework.orm.hibernate3.HibernateTemplate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.UserDao;

import com.po.UserPo;

public class UserDaoImpl01 extends HibernateDaoSupport implements UserDao {

private UserPo userid = null;

@Override

public UserPo getUser(String userId) {

Session session = null;

Transaction transaction = null;

List list = null;

// 查找

try {

session = this.getSessionFactory().openSession();

String hql = "FROM com.po.UserPo";

Query query = session.createQuery(hql);

list = query.list();

} catch (Exception ex) {

}

return (UserPo) list.get(0);

}

}

=======================================================================

//创建接口->UserService.java

=======================================================================

import org.springframework.web.servlet.ModelAndView;

public interface UserService {

public boolean validateLogin(String usrId, String usrPwd);

public void test();

}

//继承接口UserService,创建UserServiceImlp.java

import com.dao.UserDao;

import com.po.UserPo;

import com.service.UserService;

public class UserServiceImpl implements UserService {

@Override

public void test() {

System.out.println("this is test");

}

private UserDao userDao = null;

@Override

public boolean validateLogin(String usrId, String usrPwd) {

// System.out.println("UserServiceImpl run");

boolean b = false;

if (null != usrId && usrId.trim().length() != 0 && null != usrPwd

&& usrPwd.trim().length() != 0) {

UserPo userPo = this.getUserDao().getUser(usrId);

if (null != userPo && usrPwd.equals(userPo.getUserPwd())) {

b = true;

}

}

return b;

}

public UserDao getUserDao() {

return userDao;

}

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

}

=======================================================================

UserPo.hbm.xml文件配置

=======================================================================

<hibernate-mapping>

<class name="com.po.UserPo" table="userpo"> //UserPo类  userpo数据库中的表

<id name="userId" type="java.lang.String">//userId->UserPo中属性 type->userid类型

<column name="user_Id"></column>//数据库中对应userId的属性名

<generator class="assigned"></generator>

</id>

<property name="userName" type="java.lang.String">

<column name="user_Name" not-null="true"></column>

</property>

<property name="userPwd" type="java.lang.String">

<column name="user_Pwd" not-null="true"></column>

</property>

</class>

</hibernate-mapping>

=======================================================================

分析:

class name="com.po.UserPo" 中name="映射数据库表的文件本例子用UserPo来影射数据库中的内容

table="userpo"table="所建数据库中表的名字,注意大小写"

<id name="userId"di name="UserPo中的属性属性名应和原文相同

type="java.lang.String" typename属性的类型

<column name="user_Id"column name="所连接数据库中相对应的表中属性"

 not-null="true"值不能为空

Applicationcontext.xml文件设置

<beans></beans>之间填入如下:

=======================================================================

<bean id="dataSourceMysql5"  

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />

<property name="url" value="jdbc:mysql://127.0.0.1:3306/ioctest" /><!--数据库信息-->

<property name="username" value="root" />

<property name="password" value="wj0105" />

</bean>

<bean id="sessionFactoryMysql5"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml"><!--hibernate.cfg.xml 地址-->

</property>

<property name="dataSource" ref="dataSourceMysql5" />

</bean>

<!--userDaoImpl01注入到userService-->

<bean id="userDaoImpl01" class="com.dao.impl.UserDaoImpl01"> 

<property name="sessionFactory" ref="sessionFactoryMysql5"></property><!--继承HibernateSessionSupport-->

</bean>

<bean id="userService" class="com.service.impl.UserServiceImpl">

<property name="userDao" ref="userDaoImpl01"></property>

</bean>

========================================================================

分析:

其中<bean id="dataSourceMysql5"id="填入别名"  

class="org.springframework.jdbc.datasource.DriverManagerDataSource">class="填入别名所引用的类"

<property name="driverClassName"name="填入应用的类中所包含的(你要用到)方法、属性

value="com.mysql.jdbc.Driver"value="填入值"

前两个bean间的关系:在第一个bean中类org.springframework.jdbc.datasource.DriverManagerDataSource,它被引用了两个属性driverClassNameurlusernamepassword并赋值。

第二个beanorg.springframework.orm.hibernate3.LocalSessionFactoryBean类,他所含有的俩个属性一个被赋值为/WEB-INF/hibernate.cfg.xml(是我们上面配置的一个文件的地址)。另一个方法dataSource它用ref指向第一个bean中别名为dataSourceMysql5

就这样,我们把springhibernate俩者联系了起来

由于我们是面向接口编程,所以,后俩个bean通过userDaouserDaoImpl01userService联系起来,这样可以减少类间的依赖,而依赖与接口

hibernate.cfg.xml 配置文件

======================================================================

<hibernate-configuration>

<session-factory>

<property name="show_sql">true</property>

<mapping resource="com/hbn/UserPo.hbm.xml" /> <!--UserPo地址-->

</session-factory>

</hibernate-configuration>

======================================================================

分析:

<property name="show_sql">true</property> 显示查询语句

<mapping resource="com/hbn/UserPo.hbm.xml" />影射源文件UserPo的地址

Login.jsp

<body></body>标签之间输入

======================================================================

<form action="login.do" method="post"><input type="hidden"

name="event" value="CHK">

<table>

<tr>

<td>用户ID<input type="text" name="userId">

</tr>

<tr>

<td>密码:<input type="password" name="userPwd">

</tr>

<tr>

<td><input type="submit" value="提交" name="submitBtn">

</tr>

</table>

</form>

======================================================================

Index.xml

======================================================================

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script>

<!-- 

document.location.href="login.do?"

-->

</script>

</head>

<body>

</body>

</html>

======================================================================

Main.jsp

======================================================================

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>

</head>

<body>

<br>

login successfully already. 

</body>

</html>

======================================================================

False.jsp

======================================================================

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>

</head>

<body>

<br>

login fail

</body>

</html>

======================================================================

indel.xml页面中点击右键,选择运行方式->Running on Service

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值