1.配置 hibernate.cfg.xml 在src里面
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="ls/student/pojo/Student.hbm.xml" />
<mapping resource="ls/teacher/pojo/Teacher.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.配置pojo里面Teacher类的 Teacher.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="ls.teacher.pojo">
<class name="ls.teacher.pojo.Teacher" table="Teacher">
<id name="userid" column="userid" type="string" >
<generator class="assigned"/>
</id>
<property name="name" column="name" type="string"></property>
<property name="password" column="password" type="string"></property>
<property name="age" column="age" type="string"></property>
</class>
</hibernate-mapping>
3.数据操作 增删改查
package ls.teacher.dao;
import java.util.List;
import ls.teacher.pojo.Teacher;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TeacherDao {
private static Teacher teacher = null;
/**
* 添加老师
* @param password
* @return
*/
public Teacher addTeacher(String userid,String name,String password,String age){
Configuration cfg = new Configuration().addResource("hibernate.cfg.xml");//初始化configuration配置文件
SessionFactory sf = cfg.configure().buildSessionFactory();//建一个SessionFactory
Session session = sf.openSession();//打开Session
Transaction transaction = session.beginTransaction();//开始一个事物
try {
teacher = new Teacher();
teacher.setUserid(userid);
teacher.setName(name);
teacher.setPassword(password);
teacher.setAge(age);
session.save(teacher);
transaction.commit();
} catch (Exception e) {
// TODO: handle exception
transaction.rollback();
}
return teacher;
}
/**
* 通过id查询老师信息
* @param userid
* @return Teacher
*/
public Teacher selectTeacher(String userid){
Configuration cfg = new Configuration().addResource("hibernate.cfg.xml");//初始化configuration配置文件
SessionFactory sf = cfg.configure().buildSessionFactory();//建一个SessionFactory
Session session = sf.openSession();//打开Session
Transaction transaction = session.beginTransaction();//开始一个事物
String hql = "from Teacher teacher where teacher.userid=?";
List<Teacher> list = null;
try {
list = session.createQuery(hql).setString(0, userid).list();
System.out.println("pp:"+list.get(0).getPassword());
transaction.commit();
teacher = list.get(0);
} catch (Exception e) {
// TODO: handle exception
transaction.rollback();
}finally{
session.close();
}
return teacher;
}
/**
* 更新老师密码
* @return
*/
public boolean updateTeacher(String userid,String password){
boolean flag = false;
Configuration cfg = new Configuration().addResource("hibernate.cfg.xml");
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction transaction = session.beginTransaction();
String hql = "update Teacher teacher set teacher.password=? where teacher.userid=?";
try {
Query queryupdate = session.createQuery(hql).setString(0, password).setString(1, userid);
queryupdate.executeUpdate();
transaction.commit();
flag = true;
} catch (Exception e) {
// TODO: handle exception
transaction.rollback();
}finally{
session.close();
}
return flag;
}
/**
* 删除老师信息
* @return
*/
public boolean deleteTeacher(String userid) {
boolean flag = false;
Configuration cfg = new Configuration().addResource("hibernate.cfg.xml");
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction transaction = session.beginTransaction();
String hql = "delete from Teacher teacher where teacher=?";
try {
Query querydelete = session.createQuery(hql).setString(0, userid);
querydelete.executeUpdate();
transaction.commit();
flag = true;
} catch (Exception e) {
// TODO: handle exception
session.close();
}
return flag;
}
}