Hibernate访问数据库,HibernateTemplate操作数据库,实现增删改查
1、首先創建封裝Hibernate的Dao類
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Component;
@Component
public class UserDao<T> {
?? ?
?? ?@Resource(name="hibernateTemplate")
?? ?private HibernateTemplate hibernateTemplate;
?? ?
?? ?public void add(Object o){
?? ??? ?hibernateTemplate.save(o);
?? ?}
?? ?public List<T> select(String ?hql){
?? ??? ?List<T> stus=(List<T>) hibernateTemplate.find(hql);
?? ??? ?return stus;
?? ?}
?? ?
?? ?public Object get(Class clazz,Serializable id) {
?? ??? ?Object obj = hibernateTemplate.get(clazz, id);
?? ??? ?return obj;
?? ?}
?? ?public Object getbyid(Class class1,Long id){
?? ??? ?Object object = hibernateTemplate.get(class1, id);
?? ??? ?return object;
?? ?}
?? ?
?? ?public void delete(Class clazz,Serializable id) {
?? ??? ?hibernateTemplate.delete(this.get(clazz, id));
?? ?}
?? ?
?? ?public void update(Object o) {
?? ??? ?hibernateTemplate.update(o);
?? ?}
?? ?
}
2、中間服務類
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ssh.bean.Student;
import com.ssh.bean.Userinfodz;
import com.ssh.dao.UserDao;
@Transactional
@Service
public class UserService {
?? ?
?? ?@Resource(name="userDao")
?? ?private UserDao userDao;
?? ?
?? ?public void add(Student stu){
?? ??? ?userDao.add(stu);
?? ?}
?? ?
?? ?public List<Student> showUser(){
//?? ??? ?List<Student> stus = userDao.select("from Student");//查詢所有
?? ??? ?List<Student> stus = userDao.select("from Student where pword LIKE ?'%1%'");//條件查詢
?? ??? ?/*users=(List<Student>)hibernateTemplate.find("from User where uname=?","秦維");*/
?? ??? ?return stus;
?? ?}
?? ?public List<Userinfodz> showinfo(){
?? ??? ?
?? ??? ?List<Userinfodz> inList = userDao.select("from Userinfodz");
?? ??? ?
?? ??? ?return inList;
?? ?}
?? ?public Userinfodz getobjUserinfodzbyid (Long ab){
?? ??? ?Userinfodz userinfodz = (Userinfodz) userDao.getbyid(Userinfodz.class,ab );
?? ??? ?
?? ??? ?return userinfodz;
?? ?}
?? ?public void adduserinfo(Userinfodz userinfodz){
?? ??? ?userDao.add(userinfodz);
?? ?}
}
3、測試類:
?? ?@Resource(name="userService")
?? ?private UserService userService;
Userinfodz userinfodz2 = userService.getobjUserinfodzbyid(11L);
(1)、查詢
//?? ??? ?List<Student> stus = userDao.select("from Student");//查詢所有
?? ??? ?List<Student> stus = userDao.select("from Student where pword LIKE ?'%1%'");//條件查詢
?
根據id查詢
?? ?public Userinfodz getobjUserinfodzbyid (Long ab){
?? ??? ?Userinfodz userinfodz = (Userinfodz) userDao.getbyid(Userinfodz.class,ab );
?? ??? ?
?? ??? ?return userinfodz;
?? ?}
(2)、插入數據
?? ??? ?Userinfodz userinfodz = new Userinfodz();
?? ??? ?userinfodz.setUsername("sqlname");
?? ??? ?userinfodz.setUserno(999);
?? ??? ?userinfodz.setUserpwd("sqlpwd");
?? ??? ?userService.adduserinfo(userinfodz);
@Resource(name="hibernateTemplate")
?? ?private HibernateTemplate hibernateTemplate;
hibernateTemplate.save(userinfodz);
(3)刪除數據
dao方法
?? ?public Object getbyid(Class class1,Long id){
?? ??? ?Object object = hibernateTemplate.get(class1, id);
?? ??? ?return object;
?? ?}
?? ?
?? ?public void deletebyid(Class class1,Long id){
?? ??? ?hibernateTemplate.delete(this.getbyid(class1, id));
?? ?}
中間服務
?? ?public void deletebyid(Long ab){
?? ??? ?userDao.deletebyid(Userinfodz.class, ab);
?? ?}
測試
userService.deletebyid(10L);
(4)修改數據
?? ?public void updatebyid(Class class1,String uname,String upwd,Long id){
?? ??? ?Userinfodz userinfodz = (Userinfodz) this.getbyid(class1,id );
?? ??? ?userinfodz.setUsername(uname);
?? ??? ?userinfodz.setUserpwd(upwd);
?? ??? ?hibernateTemplate.update(userinfodz);
?? ?}
?? ?public void updatebyid(Class class1,String uname,String upwd,Long id){
?? ??? ?userDao.updatebyid(class1,uname,upwd, id);
?? ?}
userService.updatebyid(Userinfodz.class,"修改的銘2","修改的pwd2", 7L);
?
?
?
?
總結
以上是生活随笔為你收集整理的Hibernate访问数据库,HibernateTemplate操作数据库,实现增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WCF 服务端+客户端动态调用
- 下一篇: php sns 源码,ThinkSNS