注解简化SSH框架
一、把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>42
1
<!-- 配置SessionFactory -->2
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 3
<property name="dataSource" ref="dataSource" />4
<!-- 配置hibernate -->5
<property name="hibernateProperties">6
<props>7
<!-- 数据库的方言 -->8
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>9
<!-- 是否在控制台输出SQL语句 -->10
<prop key="hibernate.show_sql">true</prop>11
<!-- 是否输出格式化后的sql -->12
<prop key="hibernate.format_sql">true</prop>13
<!-- 是否自动提交 -->14
<prop key="hibernate.connection.autocommit">false</prop>15
<!-- 开机自动生成表 -->16
<prop key="hibernate.hbm2ddl.auto">update</prop>17
</props>18
</property>19
<!-- 指明使用注解的实体类 -->20
<property name="annotatedClasses">21
<list>22
<value>entity.News</value>23
</list>24
</property>25
</bean>26
<!-- 配置C3P0数据库连接池 -->27
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">28
<!-- 配置数据库驱动,这里使用mysql -->29
<property name="driverClass" value="com.mysql.jdbc.Driver" />30
<!-- 设置数据库的连接URL localhost表示服务器名,News表示数据库名 -->31
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/news" />32
<!-- 连接数据库的用户名 -->33
<property name="user" value="root" />34
<!-- 连接数据库的密码 -->35
<property name="password" value="123456" />36
<!-- 每300秒检查所有连接池中的空闲连接 -->37
<property name="idleConnectionTestPeriod" value="300"></property>38
<!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->39
<property name="maxIdleTime" value="900"></property>40
<!-- 最大连接数 -->41
<property name="maxPoolSize" value="2"></property>42
</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;
}
}x
1
56
1
package entity;2
3
import java.sql.Date;4
import javax.persistence.Column;5
import javax.persistence.Entity;6
import javax.persistence.GeneratedValue;7
import javax.persistence.GenerationType;8
import javax.persistence.Id;9
import javax.persistence.Table;10
11
@Entity12
@Table(name="News")13
public class News {14
@Id15
@Column(name="id")16
@GeneratedValue(strategy=GenerationType.IDENTITY)17
private Integer id;18
@Column(name="userName")19
private String userName;20
@Column(name="content")21
private String content;22
@Column(name="title")23
private String title;24
@Column(name="begintime")25
private Date begintime;26
public Integer getId() {27
return id;28
}29
public void setId(Integer id) {30
this.id = id;31
}32
public String getUserName() {33
return userName;34
}35
public void setUserName(String userName) {36
this.userName = userName;37
}38
public String getContent() {39
return content;40
}41
public void setContent(String content) {42
this.content = content;43
}44
public String getTitle() {45
return title;46
}47
public void setTitle(String title) {48
this.title = title;49
}50
public Date getBegintime() {51
return begintime;52
}53
public void setBegintime(Date begintime) {54
this.begintime = begintime;55
}56
}@Entity表示当前类是个实体类,@Table(name="News")表示这个类要映射到数据库的那张表,@Id表的Id,@Column(name="id")表示映射到表对应的字段,字段名和属性名相同可以不写,@GeneratedValue()定义ID的生产策略。
2.在applicationContext.xml配置文件配置以下属性
<!-- 自动扫描实体 --><property name="packagesToScan">
<list>
<value>entity.News</value>
</list></property>1
<!-- 自动扫描实体 --><property name="packagesToScan">2
<list>3
<value>entity.News</value>4
</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;
}1
@Repository("newsDaoImpl")2
@Scope("prototype")3
public class NewsDaoImpl implements NewsDaoIntf{4
@Autowired5
@Qualifier("sessionFactory")6
private SessionFactory sf;7
8
}@Scope("prototype")表示非单例模式,每次接收一个请求创建一个Action对象,(@Repository、@service、@Controller这三个注解也可以混用,Spring现在还无法识别具体是哪一层。)
然后删除applicationContext.xml对应的bean.
浙公网安备 33010602011771号