com.tao.pojo实体类

package com.tao.pojo;

public class User {
private int id;
private String name;
private String password; public User() {
super();
}
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
} } User.hbm.xml映射文件 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.hibernate.test.schemaupdate"> <class name="com.tao.pojo.User" table="user">
<id name="id">
<generator class="identity"></generator>
</id>
<property name="name"/>
<property name="password" column="pass"/>
</class> </hibernate-mapping> 配置文件hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test0102?characterEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property> <!-- 因为加载的时候只加载配置文件,没有加载映射文件,让它去找 -->
<mapping resource="com/tao/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration> DAO方法
com.tao.dao package com.tao.dao; import java.util.List; public interface BaseDAO<T> {
// 通用的功能
public List<T> findAll(); public T findById(int id); public void deleteById(int id); public boolean update(T t); public boolean save(T t); } package com.tao.dao; import com.tao.pojo.User; public interface UserDAO extends BaseDAO<User> { public User login(String name,String password); } 实现类 com.tao.dao.Impl package com.tao.dao.Impl; import java.io.Serializable;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query; import com.tao.dao.UserDAO;
import com.tao.pojo.User; public class UserDAOImpl implements UserDAO{ @Override
public List<User> findAll() {
// TODO Auto-generated method stub
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Query<User> query = session.createQuery("from User", User.class);
List<User> list = query.list(); session.getTransaction().commit();
session.close();
factory.close(); return list;
} @Override
public User findById(int id) {
// TODO Auto-generated method stub
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Query<User> query = session.createQuery("from User where id=?", User.class);
query.setParameter(0, id);
User user = query.uniqueResult(); session.getTransaction().commit();
session.close();
factory.close(); return user;
} @Override
public void deleteById(int id) {
// TODO Auto-generated method stub
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
//方法1
// User user = session.get(User.class, id);
// session.delete(user);
//放法2
// User user = new User();
// user.setId(id);
// session.delete(user); //方法3
Query query = session.createQuery("delete from User where id=?");
query.setParameter(0, id);
int rows = query.executeUpdate();
System.out.println(rows+"=============rows");
session.getTransaction().commit();
session.close();
factory.close(); } @Override
public boolean update(User t) {
// TODO Auto-generated method stub
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
//update()只根据Id更新
// session.update(t); //HQL语句(User是类名,name,password是类里面的属性名)
Query query = session.createQuery("update User set name=?,password=? where id=?");
query.setParameter(0, t.getName());
query.setParameter(1, t.getPassword());
query.setParameter(2, t.getId());
int rows = query.executeUpdate(); session.getTransaction().commit();
session.close();
factory.close();
if(rows>0) {
return true;
}
return false;
} @Override
public boolean save(User t) {
// TODO Auto-generated method stub
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Integer id = (Integer) session.save(t); session.getTransaction().commit();
session.close();
factory.close();
if(id>0) {
return true;
}
return false;
} @Override
public User login(String name, String password) {
// TODO Auto-generated method stub
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Query<User> query = session.createQuery("from User where name=? and password=?",User.class);
query.setParameter(0, name).setParameter(1, password);
User user = query.uniqueResult(); session.getTransaction().commit();
session.close();
factory.close();
return user;
} }
JUnit测试 package com.tao.test; import static org.junit.Assert.*; import java.util.List; import org.junit.Test; import com.tao.dao.Impl.UserDAOImpl;
import com.tao.pojo.User; public class JUnit_User { UserDAOImpl impl = new UserDAOImpl();
@Test
public void testAll() {
List<User> list = impl.findAll();
for (User uu : list) {
System.out.println(uu);
} }
@Test
public void testById() { User user = impl.findById(2);
System.out.println(user);
} @Test
public void testdeleteById() {
System.out.println("delete++++++++++++++++++");
impl.deleteById(10); } @Test
public void save() {
User user = new User(12, "ss", "ss");
boolean b = impl.save(user);
System.out.println(b);
} @Test
public void update() {
User user = new User(2, "ss", "222");
impl.update(user);
} //登录
@Test
public void login() {
User user = impl.login("ss", "222");
System.out.println(user);
} }

Hibernate 操作数据库的更多相关文章

  1. hibernate操作数据库总结

    这篇文章用于总结hibernate操作数据库的各种方法 一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就 ...

  2. Java_Web三大框架之Hibernate操作数据库(三)

    使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...

  3. hibernate操作数据库例子

    1.工程目录结构如下 2.引入需要的jar包,如上图. 3.创建持久化类User对应数据库中的user表 package com.hibernate.配置文件.pojo; import java.sq ...

  4. hibernate操作数据库总结(转)

    一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就猜测因为不是原生态的sql语句,数据库不支持,因此情愿选 ...

  5. Hibernate操作数据库的回调机制--Callback

     1:一般情况下,在使用Hibernate Session存取数据库的代码中,基本上大部分是相同的,如下两个方法所示, //查询Teacher操作 ublic Teacher getTeacher ...

  6. 转 使用Hibernate操作数据库时报:No CurrentSessionContext configured! 异常

    没有currentSession配置错误,即在我们使用currentSession的时候要在hibernate.cfg.xml中进行相关的事务配置:1.本地事务<property name=&q ...

  7. hibernate操作数据库时报错解决方式

    java.sql.SQLException: Parameter index out of range (28 > number of parameters, which is 27). 这个说 ...

  8. Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库

    概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...

  9. 5 -- Hibernate的基本用法 --2 2 Hibernate的数据库操作

    在所有的ORM框架中有一个非常重要的媒介 : PO(持久化对象:Persistent Object).持久化对象的作用是完成持久化操作,简单地说,通过该对象可对数据执行增.删.改的操作 ------ ...

随机推荐

  1. Hibernate与Mybatis的比较

    Hibernate与Mybatis的比较: Hibernate: 标准的.重量级.全自动化的ORM框架 可以写sql(SQLQuery,sql )也可以不写sql(Query,hql) ORM映射主要 ...

  2. iOS中tableView组头部或尾部标题的设置

    解决在tableView返回组标题直接返回字符串,带来的不便设置组标题样式的问题解决办法,设置尾部标题和此类似  // 返回组头部view的高度 - (CGFloat)tableView:(UITab ...

  3. hive的高级查询(group by、 order by、 join 、 distribute by、sort by、 clusrer by、 union all等)

    查询操作 group by. order by. join . distribute by. sort by. clusrer by. union all 底层的实现 mapreduce 常见的聚合操 ...

  4. go 实现struct转map

    从python转golang大约一个月了,对struct的使用还算顺手,但是很多时候还是会想念python的便捷.比如同时遍历两个字典,python使用for (x, y) in zip(map1, ...

  5. R实战 第七篇:网格(grid)

    grid包是R底层的图形系统,可以绘制几乎所有的图形.除了绘制图形之外,grid包还能对图形进行布局.在绘图时,有时候会遇到这样一种情景,客户想把多个代表不同KPI的图形分布到同一个画布(Page)上 ...

  6. JavaScript验证和数据处理的干货(经典)

    在开发web项目的时候,难免遇到各种对网页数据的处理,比如对用户在表单中输入的电话号码.邮箱.金额.身份证号.密码长度和复杂程度等等的验证,以及对后台返回数据的格式化比如金额,返回的值为null,还有 ...

  7. EF CodeFirst 数据库初始化策略

    最近用EF做了几个小东西,了解简单使用后有了深入研究的兴趣,所以想系统的研究一下EF CodeFist的几个要点.下面简单列一下目录 1.1 目录 数据库初始化策略和数据迁移Migration的简单介 ...

  8. html5 兼容版本 video

    <!-- first try HTML5 playback: if serving as XML, expand `controls` to `controls="controls&q ...

  9. RabbitMQ Linux(Redhat6.5)安装(二 )

    一.安装erlang 由于RabbitMq的linux运行环境需要erlang环境,所以需要先安装erlang: 1.erlang下载: http://erlang.org/download/(我下载 ...

  10. IAAS-虚拟化技术组件介绍

    虚拟化技术组件涉及众多,下面对一些组件所处的层级以及定位做个简单的汇总介绍,部分信息来自于网络整理,如有不准确之处,请指正.