需要applicationcontext.xml hibernate.cfg.xml
建立UserPo.hbm.xml 用来连接表中属性
建立数据库
建立工程iocWebApp(选择Web下Dynamic 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" 中type是name属性的类型
<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,它被引用了两个属性driverClassName、url、username、password并赋值。
第二个bean中org.springframework.orm.hibernate3.LocalSessionFactoryBean类,他所含有的俩个属性一个被赋值为/WEB-INF/hibernate.cfg.xml(是我们上面配置的一个文件的地址)。另一个方法dataSource它用ref指向第一个bean中别名为dataSourceMysql5
就这样,我们把spring与hibernate俩者联系了起来
由于我们是面向接口编程,所以,后俩个bean通过userDao把userDaoImpl01与userService联系起来,这样可以减少类间的依赖,而依赖与接口
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