Derby+spring+hibernate和其它数据库+spring+hibernate基本一样。
下面给出一个例子。
applicationContext.xml
大家重点看下这个配置文件的写法。。。。。
UserInfo.java
UserInfo.hbm.xml
UserInfoDAO.java
呵呵,其实用myeclipse开发还是很方面的,上面的这些文件全都是用myeclipse自动生成的
下面给出一个例子。
applicationContext.xml
大家重点看下这个配置文件的写法。。。。。
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
3
4 < beans >
5
6
7 < bean id ="dataSource"
8 class ="org.apache.commons.dbcp.BasicDataSource" >
9 < property name ="driverClassName" >
10 < value > org.apache.derby.jdbc.EmbeddedDriver </ value >
11 </ property >
12 < property name ="url" >
13 < value > jdbc:derby:c:/TESTDB;create=true </ value >
14 </ property >
15 < property name ="username" >
16 < value > test </ value >
17 </ property >
18 < property name ="password" >
19 < value > test </ value >
20 </ property >
21 </ bean >
22 < bean id ="sessionFactory"
23 class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
24 < property name ="dataSource" >
25 < ref bean ="dataSource" />
26 </ property >
27 < property name ="hibernateProperties" >
28 < props >
29 < prop key ="hibernate.dialect" > org.hibernate.dialect.DerbyDialect </ prop >
30 </ props >
31 </ property >
32 < property name ="mappingResources" >
33 < list >
34 < value > UserInfo.hbm.xml </ value ></ list >
35 </ property ></ bean >
36 < bean id ="UserInfoDAO" class ="UserInfoDAO" >
37 < property name ="sessionFactory" >
38 < ref bean ="sessionFactory" />
39 </ property >
40 </ bean ></ beans >
2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
3
4 < beans >
5
6
7 < bean id ="dataSource"
8 class ="org.apache.commons.dbcp.BasicDataSource" >
9 < property name ="driverClassName" >
10 < value > org.apache.derby.jdbc.EmbeddedDriver </ value >
11 </ property >
12 < property name ="url" >
13 < value > jdbc:derby:c:/TESTDB;create=true </ value >
14 </ property >
15 < property name ="username" >
16 < value > test </ value >
17 </ property >
18 < property name ="password" >
19 < value > test </ value >
20 </ property >
21 </ bean >
22 < bean id ="sessionFactory"
23 class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
24 < property name ="dataSource" >
25 < ref bean ="dataSource" />
26 </ property >
27 < property name ="hibernateProperties" >
28 < props >
29 < prop key ="hibernate.dialect" > org.hibernate.dialect.DerbyDialect </ prop >
30 </ props >
31 </ property >
32 < property name ="mappingResources" >
33 < list >
34 < value > UserInfo.hbm.xml </ value ></ list >
35 </ property ></ bean >
36 < bean id ="UserInfoDAO" class ="UserInfoDAO" >
37 < property name ="sessionFactory" >
38 < ref bean ="sessionFactory" />
39 </ property >
40 </ bean ></ beans >
UserInfo.java
1
//
default package
2
3
4
5 /**
6 * UserInfo generated by MyEclipse - Hibernate Tools
7 */
8
9 public class UserInfo implements java.io.Serializable {
10
11
12 // Fields
13
14 private Integer id;
15 private String name;
16
17
18 // Constructors
19
20 /** default constructor */
21 public UserInfo() {
22 }
23
24 /** minimal constructor */
25 public UserInfo(Integer id) {
26 this .id = id;
27 }
28
29 /** full constructor */
30 public UserInfo(Integer id, String name) {
31 this .id = id;
32 this .name = name;
33 }
34
35
36 // Property accessors
37
38 public Integer getId() {
39 return this .id;
40 }
41
42 public void setId(Integer id) {
43 this .id = id;
44 }
45
46 public String getName() {
47 return this .name;
48 }
49
50 public void setName(String name) {
51 this .name = name;
52 }
53
54
55
56
57
58
59
60
61
62 }
2
3
4
5 /**
6 * UserInfo generated by MyEclipse - Hibernate Tools
7 */
8
9 public class UserInfo implements java.io.Serializable {
10
11
12 // Fields
13
14 private Integer id;
15 private String name;
16
17
18 // Constructors
19
20 /** default constructor */
21 public UserInfo() {
22 }
23
24 /** minimal constructor */
25 public UserInfo(Integer id) {
26 this .id = id;
27 }
28
29 /** full constructor */
30 public UserInfo(Integer id, String name) {
31 this .id = id;
32 this .name = name;
33 }
34
35
36 // Property accessors
37
38 public Integer getId() {
39 return this .id;
40 }
41
42 public void setId(Integer id) {
43 this .id = id;
44 }
45
46 public String getName() {
47 return this .name;
48 }
49
50 public void setName(String name) {
51 this .name = name;
52 }
53
54
55
56
57
58
59
60
61
62 }
UserInfo.hbm.xml
1
<?
xml version="1.0" encoding="utf-8"
?>
2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4 <!--
5 Mapping file autogenerated by MyEclipse - Hibernate Tools
6 -->
7 < hibernate-mapping >
8 < class name ="UserInfo" table ="USER_INFO" schema ="APP" >
9 < id name ="id" type ="java.lang.Integer" >
10 < column name ="ID" />
11 < generator class ="assigned" />
12 </ id >
13 < property name ="name" type ="java.lang.String" >
14 < column name ="NAME" length ="10" />
15 </ property >
16 </ class >
17 </ hibernate-mapping >
18
2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4 <!--
5 Mapping file autogenerated by MyEclipse - Hibernate Tools
6 -->
7 < hibernate-mapping >
8 < class name ="UserInfo" table ="USER_INFO" schema ="APP" >
9 < id name ="id" type ="java.lang.Integer" >
10 < column name ="ID" />
11 < generator class ="assigned" />
12 </ id >
13 < property name ="name" type ="java.lang.String" >
14 < column name ="NAME" length ="10" />
15 </ property >
16 </ class >
17 </ hibernate-mapping >
18
UserInfoDAO.java
1
//
default package
2
3 import java.util.List;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.hibernate.LockMode;
7 import org.springframework.context.ApplicationContext;
8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
9
10 /**
11 * Data access object (DAO) for domain model class UserInfo.
12 * @see .UserInfo
13 * @author MyEclipse - Hibernate Tools
14 */
15 public class UserInfoDAO extends HibernateDaoSupport {
16
17 private static final Log log = LogFactory.getLog(UserInfoDAO. class );
18
19 protected void initDao() {
20 // do nothing
21 }
22
23 public void save(UserInfo transientInstance) {
24 log.debug( " saving UserInfo instance " );
25 try {
26 getHibernateTemplate().save(transientInstance);
27 log.debug( " save successful " );
28 } catch (RuntimeException re) {
29 log.error( " save failed " , re);
30 throw re;
31 }
32 }
33
34 public void delete(UserInfo persistentInstance) {
35 log.debug( " deleting UserInfo instance " );
36 try {
37 getHibernateTemplate().delete(persistentInstance);
38 log.debug( " delete successful " );
39 } catch (RuntimeException re) {
40 log.error( " delete failed " , re);
41 throw re;
42 }
43 }
44
45 public UserInfo findById( java.lang.Integer id) {
46 log.debug( " getting UserInfo instance with id: " + id);
47 try {
48 UserInfo instance = (UserInfo) getHibernateTemplate()
49 .get( " UserInfo " , id);
50 return instance;
51 } catch (RuntimeException re) {
52 log.error( " get failed " , re);
53 throw re;
54 }
55 }
56
57
58 public List findByExample(UserInfo instance) {
59 log.debug( " finding UserInfo instance by example " );
60 try {
61 List results = getHibernateTemplate().findByExample(instance);
62 log.debug( " find by example successful, result size: " + results.size());
63 return results;
64 } catch (RuntimeException re) {
65 log.error( " find by example failed " , re);
66 throw re;
67 }
68 }
69
70 public List findByProperty(String propertyName, Object value) {
71 log.debug( " finding UserInfo instance with property: " + propertyName
72 + " , value: " + value);
73 try {
74 String queryString = " from UserInfo as model where model. "
75 + propertyName + " = ? " ;
76 return getHibernateTemplate().find(queryString, value);
77 } catch (RuntimeException re) {
78 log.error( " find by property name failed " , re);
79 throw re;
80 }
81 }
82
83 public UserInfo merge(UserInfo detachedInstance) {
84 log.debug( " merging UserInfo instance " );
85 try {
86 UserInfo result = (UserInfo) getHibernateTemplate()
87 .merge(detachedInstance);
88 log.debug( " merge successful " );
89 return result;
90 } catch (RuntimeException re) {
91 log.error( " merge failed " , re);
92 throw re;
93 }
94 }
95
96 public void attachDirty(UserInfo instance) {
97 log.debug( " attaching dirty UserInfo instance " );
98 try {
99 getHibernateTemplate().saveOrUpdate(instance);
100 log.debug( " attach successful " );
101 } catch (RuntimeException re) {
102 log.error( " attach failed " , re);
103 throw re;
104 }
105 }
106
107 public void attachClean(UserInfo instance) {
108 log.debug( " attaching clean UserInfo instance " );
109 try {
110 getHibernateTemplate().lock(instance, LockMode.NONE);
111 log.debug( " attach successful " );
112 } catch (RuntimeException re) {
113 log.error( " attach failed " , re);
114 throw re;
115 }
116 }
117
118 public static UserInfoDAO getFromApplicationContext(ApplicationContext ctx) {
119 return (UserInfoDAO) ctx.getBean( " UserInfoDAO " );
120 }
121 }
2
3 import java.util.List;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.hibernate.LockMode;
7 import org.springframework.context.ApplicationContext;
8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
9
10 /**
11 * Data access object (DAO) for domain model class UserInfo.
12 * @see .UserInfo
13 * @author MyEclipse - Hibernate Tools
14 */
15 public class UserInfoDAO extends HibernateDaoSupport {
16
17 private static final Log log = LogFactory.getLog(UserInfoDAO. class );
18
19 protected void initDao() {
20 // do nothing
21 }
22
23 public void save(UserInfo transientInstance) {
24 log.debug( " saving UserInfo instance " );
25 try {
26 getHibernateTemplate().save(transientInstance);
27 log.debug( " save successful " );
28 } catch (RuntimeException re) {
29 log.error( " save failed " , re);
30 throw re;
31 }
32 }
33
34 public void delete(UserInfo persistentInstance) {
35 log.debug( " deleting UserInfo instance " );
36 try {
37 getHibernateTemplate().delete(persistentInstance);
38 log.debug( " delete successful " );
39 } catch (RuntimeException re) {
40 log.error( " delete failed " , re);
41 throw re;
42 }
43 }
44
45 public UserInfo findById( java.lang.Integer id) {
46 log.debug( " getting UserInfo instance with id: " + id);
47 try {
48 UserInfo instance = (UserInfo) getHibernateTemplate()
49 .get( " UserInfo " , id);
50 return instance;
51 } catch (RuntimeException re) {
52 log.error( " get failed " , re);
53 throw re;
54 }
55 }
56
57
58 public List findByExample(UserInfo instance) {
59 log.debug( " finding UserInfo instance by example " );
60 try {
61 List results = getHibernateTemplate().findByExample(instance);
62 log.debug( " find by example successful, result size: " + results.size());
63 return results;
64 } catch (RuntimeException re) {
65 log.error( " find by example failed " , re);
66 throw re;
67 }
68 }
69
70 public List findByProperty(String propertyName, Object value) {
71 log.debug( " finding UserInfo instance with property: " + propertyName
72 + " , value: " + value);
73 try {
74 String queryString = " from UserInfo as model where model. "
75 + propertyName + " = ? " ;
76 return getHibernateTemplate().find(queryString, value);
77 } catch (RuntimeException re) {
78 log.error( " find by property name failed " , re);
79 throw re;
80 }
81 }
82
83 public UserInfo merge(UserInfo detachedInstance) {
84 log.debug( " merging UserInfo instance " );
85 try {
86 UserInfo result = (UserInfo) getHibernateTemplate()
87 .merge(detachedInstance);
88 log.debug( " merge successful " );
89 return result;
90 } catch (RuntimeException re) {
91 log.error( " merge failed " , re);
92 throw re;
93 }
94 }
95
96 public void attachDirty(UserInfo instance) {
97 log.debug( " attaching dirty UserInfo instance " );
98 try {
99 getHibernateTemplate().saveOrUpdate(instance);
100 log.debug( " attach successful " );
101 } catch (RuntimeException re) {
102 log.error( " attach failed " , re);
103 throw re;
104 }
105 }
106
107 public void attachClean(UserInfo instance) {
108 log.debug( " attaching clean UserInfo instance " );
109 try {
110 getHibernateTemplate().lock(instance, LockMode.NONE);
111 log.debug( " attach successful " );
112 } catch (RuntimeException re) {
113 log.error( " attach failed " , re);
114 throw re;
115 }
116 }
117
118 public static UserInfoDAO getFromApplicationContext(ApplicationContext ctx) {
119 return (UserInfoDAO) ctx.getBean( " UserInfoDAO " );
120 }
121 }
呵呵,其实用myeclipse开发还是很方面的,上面的这些文件全都是用myeclipse自动生成的