• Hibernate 操作数据库


    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);
        }
        
        
    }
  • 相关阅读:
    SDN作业(4)
    SDN作业(3)
    第一次个人编程作业
    SDN作业(2)
    SDN作业(1)
    第一次博客作业
    浅谈闭包
    预编译And作用域链
    定时器
    window事件
  • 原文地址:https://www.cnblogs.com/jili6254/p/8568745.html
Copyright © 2020-2023  润新知