直接代码就行,为了以后忘了再看
配置Hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库连接 --> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <!-- 数据库用户名 --> <property name="connection.username">root</property> <!-- 数据库密码 --> <property name="connection.password">xiaohu</property> <!-- 数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 是否显示sql语句--> <property name="show_sql">true</property> <!-- 格式化sql语句 --> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/tenghu/client/model/Log.hbm.xml"/> <mapping resource="com/tenghu/client/model/Users.hbm.xml"/> </session-factory> </hibernate-configuration>
配置实体类文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tenghu.client.model"> <class name="Log" table="tab_log"> <id name="id" column="id"> <generator class="native"/> </id> <property name="datetime" column="date_time"/> <property name="type" column="type"/> <many-to-one name="user" class="Users" column="user_id"/> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tenghu.client.model"> <class name="Users" table="tab_user"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name"/> <set name="log" table="tab_log"> <key column="user_id"/> <one-to-many class="Log" /> </set> </class> </hibernate-mapping>
配置Spring相关文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byType"> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 那些类需要使用到事务 --> <aop:config> <aop:pointcut expression="execution(* com.tenghu.client.dao.impl.*.*(..))" id="allManagerMedel"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMedel"/> </aop:config> <!-- 事务的传播性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byType"> <!-- 用户操作实现类 --> <bean id="userDao" class="com.tenghu.client.dao.impl.UsersDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> <property name="logDao" ref="logDao"/> </bean> <!-- 日志操作实现类 --> <bean id="logDao" class="com.tenghu.client.dao.impl.LogDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 用户操作服务类 --> <bean id="userService" class="com.tenghu.client.service.impl.UsersServiceImpl"> <property name="userdao" ref="userDao"/> </bean> </beans>
实体类
package com.tenghu.client.model; import java.io.Serializable; import java.util.Date; /** * 记录日志类 * @author xiaohu * */ public class Log implements Serializable{ //id private int id; //日志类型 private String type; //日志日期 private Date datetime; //日志操作人 private Users user; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Date getDatetime() { return datetime; } public void setDatetime(Date datetime) { this.datetime = datetime; } public Users getUser() { return user; } public void setUser(Users user) { this.user = user; } public Log() { } }
package com.tenghu.client.model; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Users implements Serializable{ //用户id private int id; //用户名 private String name; //日志集合 private Set log=new HashSet(); 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 Set getLog() { return log; } public void setLog(Set log) { this.log = log; } public Users(int id, String name) { this.id = id; this.name = name; } public Users() { } }
dao层
package com.tenghu.client.dao; import com.tenghu.client.model.Log; public interface LogDao { //添加日志 public void addLog(Log log); }
package com.tenghu.client.dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.tenghu.client.dao.LogDao; import com.tenghu.client.model.Log; /** * 日志操作实现类 * @author xiaohu * */ public class LogDaoImpl extends HibernateDaoSupport implements LogDao{ @Override public void addLog(Log log) { //保存日志 this.getHibernateTemplate().save(log); } }
package com.tenghu.client.model; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Users implements Serializable{ //用户id private int id; //用户名 private String name; //日志集合 private Set log=new HashSet(); 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 Set getLog() { return log; } public void setLog(Set log) { this.log = log; } public Users(int id, String name) { this.id = id; this.name = name; } public Users() { } }
package com.tenghu.client.dao.impl; import java.util.Date; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.tenghu.client.dao.LogDao; import com.tenghu.client.dao.UsersDao; import com.tenghu.client.model.Log; import com.tenghu.client.model.Users; /** * 用户操作实现类 * @author xiaohu * */ public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao{ //创建日志操作实现类 private LogDao logDao; @Override public void addUsers(Users user) { //保存用户 this.getHibernateTemplate().save(user); //创建Log Log log=new Log(); log.setDatetime(new Date()); log.setType("操作日志-->添加用户"); log.setUser(user); //添加日志记录 logDao.addLog(log); } public void setLogDao(LogDao logDao) { this.logDao = logDao; } /** * 查询用户 */ @Override public Users queryUser(Users user) { //获取Session对象 Session session=this.getSession(); //创建Query对象 Query query=session.createQuery("from Users where name=?"); //设置参数 query.setString(0, user.getName()); //执行查询 List<Users> list=query.list(); if(list.size()>0) return list.get(0); else return null; } }
service层
package com.tenghu.client.service; import com.tenghu.client.model.Users; /** * 用户操作服务接口 * @author xiaohu * */ public interface UsersService { //添加用户 public void addUsers(Users user); //查询用户 public Users queryUser(Users user); }
package com.tenghu.client.service.impl; import com.tenghu.client.dao.UsersDao; import com.tenghu.client.model.Users; import com.tenghu.client.service.UsersService; public class UsersServiceImpl implements UsersService{ //创建用户操作接口 private UsersDao userdao; @Override public void addUsers(Users user) { userdao.addUsers(user); throw new RuntimeException(); } @Override public Users queryUser(Users user) { return userdao.queryUser(user); } public void setUserdao(UsersDao userdao) { this.userdao = userdao; } }
测试
package com.tenghu.client.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tenghu.client.dao.UsersDao; import com.tenghu.client.dao.impl.UsersDaoImpl; import com.tenghu.client.model.Users; import com.tenghu.client.service.UsersService; public class Test { public static void main(String[] args) { //获取ApplicationContext对象 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-*.xml"); //获取用户实现类 UsersService userService=(UsersService) context.getBean("userService"); //创建用户对象 Users user=new Users(); user.setName("李四"); //查询用户 Users u=userService.queryUser(user); if(null==u){ userService.addUsers(user); }else{ System.out.println("用户已存在"); } } }