一、把hibernate交由spring来管理,在applicationContext.xml配置如下信息:
<!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 配置hibernate --> <property name="hibernateProperties"> <props> <!-- 数据库的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <!-- 是否在控制台输出SQL语句 --> <prop key="hibernate.show_sql">true</prop> <!-- 是否输出格式化后的sql --> <prop key="hibernate.format_sql">true</prop> <!-- 是否自动提交 --> <prop key="hibernate.connection.autocommit">false</prop> <!-- 开机自动生成表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 指明使用注解的实体类 --> <property name="annotatedClasses"> <list> <value>entity.News</value> </list> </property> </bean> <!-- 配置C3P0数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置数据库驱动,这里使用mysql --> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <!-- 设置数据库的连接URL localhost表示服务器名,News表示数据库名 --> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/news" /> <!-- 连接数据库的用户名 --> <property name="user" value="root" /> <!-- 连接数据库的密码 --> <property name="password" value="123456" /> <!-- 每300秒检查所有连接池中的空闲连接 --> <property name="idleConnectionTestPeriod" value="300"></property> <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 --> <property name="maxIdleTime" value="900"></property> <!-- 最大连接数 --> <property name="maxPoolSize" value="2"></property> </bean>
配置完后删除hibernate.cfg.xml。
二 、注解简化实体类
1.配置实体类
package entity; import java.sql.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="News") public class News { @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(name="userName") private String userName; @Column(name="content") private String content; @Column(name="title") private String title; @Column(name="begintime") private Date begintime; 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; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getBegintime() { return begintime; } public void setBegintime(Date begintime) { this.begintime = begintime; } }
@Entity表示当前类是个实体类,@Table(name="News")表示这个类要映射到数据库的那张表,@Id表的Id,@Column(name="id")表示映射到表对应的字段,字段名和属性名相同可以不写,@GeneratedValue()定义ID的生产策略。
2.在applicationContext.xml配置文件配置以下属性
<!-- 自动扫描实体 --> <property name="packagesToScan"> <list> <value>entity.News</value> </list> </property>
配置完后,就可以把News.hbm.xml删除。
三、注解简化applicationContext.xml配置文件
1.把set注入改用注解注入,这里分两种:一种是JDK的注解@Resource,另一种是spring自带的注解@Autowired、@Qualifier
JDK的注解@Resource:
@Resource(name="sessionFactory")
private SessionFactory sf;
在需要注入的属性上加上@Resource(name="sessionFactory") ,name是你要注入的实现类。
spring自带的注解@Autowired、@Qualifier:
@Autowired @Qualifier("sessionFactory") private SessionFactory sf;
@Qualifier("sessionFactory")可以不加,spring会更加类型进行注入,如果多个属性都注入同一个实现类,spring可能会注入错误,所以最好加上。
2.在applicationContext.xml配置
<context:annotation-config />
来启动spring注解后,就可以把
public void setSf(SessionFactory sf) { this.sf = sf; }
<property name="sf" ref="sessionFactory"></property>
删掉。
四、注解简化applicationContext.xml配置文件的bean
1.在applicationContext.xml配置文件添加以下属性
<!-- 自动扫描与装配bean --> <context:component-scan base-package="dao"></context:component-scan>
base-package 要扫描的包名,多个可以用逗号隔开。
2.使用spring的注解@Repository(数据访问层对应DAO)、@Service(业务层对应Service)、@Controller(控制层对应Action)
@Repository("newsDaoImpl") @Scope("prototype") public class NewsDaoImpl implements NewsDaoIntf{ @Autowired @Qualifier("sessionFactory") private SessionFactory sf; }
@Scope("prototype")表示非单例模式,每次接收一个请求创建一个Action对象,(@Repository、@service、@Controller这三个注解也可以混用,Spring现在还无法识别具体是哪一层。)
然后删除applicationContext.xml对应的bean.