Hibernate基础的增删改查详解编程语言

Java bean:

package com.my.bean; 
 
public class User { 
 
    private Integer id;      
    private String username; 
 
    public Integer getId() { 
        return id; 
    } 
    public void setId(Integer id) { 
        this.id = id; 
    } 
    public String getUsername() { 
        return username; 
    } 
    public void setUsername(String username) { 
        this.username = username; 
    } 
    @Override 
    public String toString() { 
        return "User [id=" + id + ", username=" + username + "]"; 
    } 
 
} 

hbm, User.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC  
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping package="com.my.bean" > 
    <class name="User" table="t_user"> 
        <id name="id"> 
            <generator class="native"></generator> 
        </id> 
        <property name="username" ></property> 
    </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.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">1234</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 
 
    <property name="hibernate.format_sql">true</property> 
    <property name="hibernate.show_sql">true</property> 
 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <property name="javax.persistence.validation.mode">none</property> 
 
    <mapping resource="com/my/bean/User.hbm.xml" /> 
 
</session-factory> 
 
</hibernate-configuration>

UserDao:

package com.my.dao; 
import java.util.List; 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
import com.my.bean.User; 
public class UserDao { 
public void save(User user) { 
Configuration config = new Configuration().configure(); 
SessionFactory factory = config.buildSessionFactory(); 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
session.save(user); 
transaction.commit(); 
session.close(); 
factory.close(); 
} 
public void update(User user) { 
Configuration config = new Configuration().configure(); 
SessionFactory factory = config.buildSessionFactory(); 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
session.update(user); 
transaction.commit(); 
session.close(); 
factory.close(); 
} 
public void delete(User user) { 
Configuration config = new Configuration().configure(); 
SessionFactory factory = config.buildSessionFactory(); 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
session.delete(user); 
transaction.commit(); 
session.close(); 
factory.close(); 
} 
public User findById(Integer id) { 
Configuration config = new Configuration().configure(); 
SessionFactory factory = config.buildSessionFactory(); 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
User user = (User) session.get(User.class, id); 
transaction.commit(); 
session.close(); 
factory.close(); 
return user; 
} 
public List<User> findAll() { 
Configuration config = new Configuration().configure(); 
SessionFactory factory = config.buildSessionFactory(); 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Query query = session.createQuery("from User"); 
List<User> allUser = query.list(); 
transaction.commit(); 
session.close(); 
factory.close(); 
return allUser; 
} 
public List<User> findAllPage(int startIndex, int pageSize) {  
Configuration config = new Configuration().configure(); 
SessionFactory factory = config.buildSessionFactory(); 
Session session = factory.openSession(); 
Transaction transaction = session.beginTransaction(); 
Query query = session.createQuery("from User"); 
query.setFirstResult(startIndex); 
query.setMaxResults(pageSize); 
Object obj = session.createQuery("select count(*) from User").uniqueResult(); 
System.out.println(obj); 
List<User> allUser = query.list(); 
transaction.commit(); 
session.close(); 
factory.close(); 
return allUser; 
} 
} 

UserDaoTest:

package com.my.dao; 
import java.util.List; 
import org.junit.Test; 
import com.my.bean.User; 
public class UserDaoTest { 
UserDao userDao = new UserDao(); 
@Test 
public void testSave() { 
User user = new User(); 
user.setUsername("tom"); 
userDao.save(user); 
} 
@Test 
public void testUpdate() { 
User user = new User(); 
user.setId(2); 
user.setUsername("jack"); 
userDao.update(user); 
} 
@Test 
public void testDelete() { 
User user = new User(); 
user.setId(3); 
userDao.delete(user); 
} 
@Test 
public void testFindById() { 
User user = userDao.findById(2); 
System.out.println(user); 
} 
@Test 
public void testFindAll() { 
List<User> allUser = userDao.findAll(); 
for (User user : allUser) { 
System.out.println(user); 
} 
} 
@Test 
public void testFindAllPage() { 
List<User> allUser = userDao.findAllPage(0, 2); 
for (User user : allUser) { 
System.out.println(user); 
} 
} 
} 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/12091.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论