1、hibernate交给spring管理,配置applicationContext
<!-- 配置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文件
2、删除映射文件,给实体类加上注解(简化实体类)
<span style="font-size:18px;"><span style="font-size:18px;"><span style="background-color: rgb(255, 255, 255);">@Entity //声明当前类为hibernate映射到数据库中的实体类
@Table(name="news") //声明table的名称
public class News {
@Id //声明此列为主键,作为映射对象的标识符
/**
* @GeneratedValue注解来定义生成策略
* GenerationType.TABLES 当前主键的值单独保存到一个数据库的表中
* GenerationType.SEQUENCE 利用底层数据库提供的序列生成标识符
* GenerationType.IDENTITY 采取数据库的自增策略
* GenerationType.AUTO 根据不同数据库自动选择合适的id生成方案,这里使用mysql,为递增型
*/
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name="title",nullable=false)
private String title;
@Column(name="content",nullable=false)
private String content;
@Column(name="begintime",nullable=false)
private Date begintime;
@Column(name="username",nullable=false)
private String username;
public News() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getBegintime() {
return begintime;
}
public void setBegintime(Date begintime) {
this.begintime = begintime;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}</span></span></span>
配置applicationContext.xml
<!-- 自动扫描实体 -->
<property name="packagesToScan">
<list>
<value>entity.News</value>
</list>
</property>
然后删除News.hbm.xml文件
3、简化applicationContext.xml文件
把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可能会注入错误,所以最好加上。
在applicationContext中配置了<context:annotation-config/>之后就可以把
public void setSf(SessionFactory sf) { this.sf = sf; }删除。
4、简化applicationContext配置文件中的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.