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;