博客开发笔记二——Spring3 数据库最小系统?在Spring 3中开发数据库应用其实挺简单的,只要在Spring3的最小配
博客开发笔记二——Spring3 数据库最小系统
?
在Spring 3中开发数据库应用其实挺简单的,只要在Spring3的最小配置基础上,稍微配置一下就好了,如下所示:? ? ??<bean?id=
"oracleDataSource"?class=
"org.apache.commons.dbcp.BasicDataSource"????????????destroy-method=
"close">????????????<property?name=
"driverClassName"?value=
"oracle.jdbc.driver.OracleDriver"?/>????????????<property?name=
"url"?value=
"jdbc:oracle:thin:@localhost:1521:WebLog"?/>????????????<property?name=
"username"?value=
"admin"?/>????????????<property?name=
"password"?value=
"admin"?/>??????</bean>上面就不解释了,你懂的。下面主要使用注解的方式,有以下几个特点:1、类是
AnnotationSessionFactoryBean2、有一个属性
packagesToScan指定的是注解扫描的路径3、Hibernate的属性
hibernate.hbm2ddl.auto如果是create会自动建表
??????<bean?id=
"sessionFactory"????????????class=
"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">????????????<property?name=
"dataSource"?ref=
"oracleDataSource"?/>????????????<property?name=
"packagesToScan">??????????????????<list>????????????????????????<value>myproject.module.*</value>??????????????????</list>????????????</property>????????????<property?name=
"hibernateProperties">??????????????????<props>????????????????????????<prop?key=
"hibernate.dialect">??????????????????????????????org.hibernate.dialect.OracleDialect????????????????</prop>????????????????????????<prop?key=
"hibernate.hbm2ddl.auto">??????????????????????????????update????????????????</prop>????????????????????????<prop?key=
"hibernate.show_sql">??????????????????????????????true????????????????</prop>????????????????????????<prop?key=
"hibernate.format_sql">basic</prop>????????????????????????<!-- 连接释放 -->????????????????????????<prop?key=
"hibernate.connection.release_mode">after_transaction</prop>??????????????????</props>????????????</property>??????</bean>配置依然是很简单,接下来创建实体类,也很简单,不解释了:@Entity@Table(name="article")
public?
class?Article {
??????@Id??????@GeneratedValue??????@Column??????
private?
long?id;????????????@Column??????
private?String?title;????????????@Column??????@Lob??????@Basic(fetch=FetchType.
LAZY)??????
private?String?content;????????????@ManyToOne??????@JoinColumn(name="username")??????
private?User?author;????????????@Column??????
private?Date?createDate;????????????@Column??????
private?Date?updateDate;
??????
public?
long?getId() {????????????
return?id;??????}
??????
public?
void?setId(
long?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?User getAuthor() {????????????
return?author;??????}
??????
public?
void?setAuthor(User author) {????????????
this.author?= author;??????}
??????
public?Date getCreateDate() {????????????
return?createDate;??????}
??????
public?
void?setCreateDate(Date createDate) {????????????
this.createDate?= createDate;??????}
??????
public?Date getUpdateDate() {????????????
return?updateDate;??????}
??????
public?
void?setUpdateDate(Date updateDate) {????????????
this.updateDate?= updateDate;??????}??????}
最后创建DAO类,可以操作数据库了,@Repository注解就是告诉Spring这个Bean是数据存储的:@Repository
public?
class?ContentDao?
extends?CommonDao<Article>?{????????????
public?List<Article> list() {????????????Session session = openSession();????????????
return?session.createQuery("from Article order by updateDate desc").list();??????}????????????
public?List<Article> list(
int?start,?
int?count) {????????????Session session = openSession();????????????Query query = session.createQuery("from Article order by updateDate desc");????????????query.setFirstResult(start);????????????query.setMaxResults(count);????????????
return?query.list();??????}????????????
public?Article get(Long id) {????????????Session session = openSession();????????????Article article = (Article) session.get(Article.
class, id);????????????session.close();????????????
return?article;??????}????????????
public?
long?getArticleCount() {????????????Query query = openSession().createQuery("select count(id) from Article");????????????
return?(Long) query.uniqueResult();??????}}是不是很简单,使用注解真的很简单,总结一下只需三步即可:1、配置2、编写实体Bean3、编写DAO
?