zoukankan      html  css  js  c++  java
  • springmvc3.2+spring+hibernate4全注解方式整合(三)

    service接口

    package com.fangjian.core.platform.service;
    
    import com.fangjian.core.platform.po.User;
    
    public interface UserService {
        void saveUser(User user);
    }

    service实现

    package com.fangjian.core.platform.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.fangjian.core.platform.dao.UserDao;
    import com.fangjian.core.platform.po.User;
    import com.fangjian.core.platform.service.UserService;
    
    @Service("userService")
    public class UserServiceImpl implements UserService {
        
        @Autowired
        private UserDao userdao;
        
        @Override
        public void saveUser(User user) {
            this.userdao.saveUser(user);
            //System.out.println(1/0);
            //this.userdao.saveUser(user);
        }
    
    }

    dao接口

    package com.fangjian.core.platform.dao;
    
    import com.fangjian.core.platform.po.User;
    
    public interface UserDao {
        void saveUser(User user);
    }

    dao实现

    package com.fangjian.core.platform.dao.impl;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Repository;
    
    import com.fangjian.core.platform.dao.UserDao;
    import com.fangjian.core.platform.po.User;
    
    @Repository(value="userDao")
    public class UserDaoImpl  implements UserDao {
        
        @Autowired  
        @Qualifier("sessionFactory")  
        private SessionFactory sessionFactory;  
       
        public Session getSession() {  
            //事务必须是开启的,否则获取不到  
            return sessionFactory.getCurrentSession();  
        }
        
        @Override
        public void saveUser(User user) {
            this.getSession().save(user);
        }
    
    }

    实体类

    package com.fangjian.core.platform.po;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import org.hibernate.annotations.GenericGenerator;
    
    @Entity
    @Table(name="IEMS_USER")
    public class User {
    
        @Id
        @GeneratedValue(generator="system-uuid")
        @GenericGenerator(name = "system-uuid",strategy="uuid")
        @Column(length=32)
        private String id;
        
        @Column(length=32)
        private String name;
        
        @Column(length=32)
        private String username;
        
        @Column(length=32)
        private String password;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        
        
    }

    实体类SQL

    CREATE TABLE `iems_user` (
      `id` varchar(72) NOT NULL,
      `name` varchar(72) default NULL,
      `password` varchar(72) default NULL,
      `username` varchar(72) default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 相关阅读:
    软件工程问题清单
    问题清单
    2020软件工程3作业
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
    2020软件工程作业03
    2020软件工程作业05
    软件工程作业 疑问总结
    2020软件工程作业04
  • 原文地址:https://www.cnblogs.com/fangj/p/3820247.html
Copyright © 2011-2022 走看看