Kiss MySQL goodbye for development and say hello to HSQLDB
2014-12-23 13:09
453 查看
The days of using MySQL, DB2, PostgreSQL etc for development is over.. I don’t know why any programmer would be developing using them..Every deveroper should be running some in memory database like HSQLDB as part of the project for development and testing then move the a full size database for unit testing, staging and production.This is a sample Spring Project to show how to use JavaConfig and HSQLDB. This example also will show how to use @PropertySource for reading properties and using the Environment Object to add properties to your objects.
How to use Spring JavaConfig and not XML files for configuation
Consider replacing Spring XML configuration with JavaConfigUsing Spring XML configuration is so 2000’s the time has come to push the XML away and look at JavaConfig.Here is the main code to my sample projectview sourceprint?01.
public
class
Main
02.
{
03.
04.
private
static
final
Logger LOGGER = getLogger(Main.
class
);
05.
06.
public
static
void
main(
String
[] args)
07.
{
08.
// in this setup, both the main(String[]) method and the JUnit method both specify that
09.
ApplicationContext context =
new
AnnotationConfigApplicationContext( DatabaseConfiguration.
class
);
10.
11.
MessageService mService = context.getBean(MessageService.
class
);
12.
13.
/**
14.
* Saving Message to database
15.
*/
16.
Message message =
new
Message();
17.
message.setMessage(
"Hello World"
);
18.
mService.SaveMessage(message);
19.
/**
20.
* Saving 2nd Message in database.
21.
*/
22.
message.setMessage(
"I love NYC"
);
23.
mService.SaveMessage(message);
24.
25.
/**
26.
* Getting messages from database
27.
*- display number of message(s)
28.
*- display each message in database
29.
*/
30.
List<Message> myList = mService.listMessages();
31.
LOGGER.debug(
"You Have "
+ myList.size() +
" Message(s) In The Database"
);
32.
33.
for
(Message i : myList)
34.
{
35.
LOGGER.debug(
"Message: ID: "
+ i.getId() +
", Message: "
+ i.getMessage() +
"."
);
36.
}
37.
}
38.
}Now lets take a look at how I setup the database in JavaConfig and not in a XML file.view sourceprint?
01.
@Configuration
02.
@EnableTransactionManagement
03.
@ComponentScan(basePackageClasses = {Main.
class
})
04.
@PropertySource(
"classpath:application.properties"
)
05.
public
class
DatabaseConfiguration
06.
{
07.
@Bean
08.
public
DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
09.
ResourceDatabasePopulator resourceDatabasePopulator =
new
ResourceDatabasePopulator();
10.
resourceDatabasePopulator.addScript(
new
ClassPathResource(
"/schema.sql"
));
11.
12.
DataSourceInitializer dataSourceInitializer =
new
DataSourceInitializer();
13.
dataSourceInitializer.setDataSource(dataSource);
14.
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
15.
return
dataSourceInitializer;
16.
}
17.
18.
@Bean
19.
public
DataSource hsqlDataSource() {
20.
BasicDataSource basicDataSource =
new
BasicDataSource();
21.
basicDataSource.setDriverClassName(org.hsqldb.jdbcDriver.
class
.getName());
22.
basicDataSource.setUsername(
"sa"
);
23.
basicDataSource.setPassword(
""
);
24.
basicDataSource.setUrl(
"jdbc:hsqldb:mem:mydb"
);
25.
return
basicDataSource;
26.
}
27.
28.
@Bean
29.
public
LocalSessionFactoryBean sessionFactory(Environment environment,
30.
DataSource dataSource) {
31.
32.
String
packageOfModelBeans = Message.
class
.getPackage().getName();
33.
LocalSessionFactoryBean factoryBean =
new
LocalSessionFactoryBean();
34.
factoryBean.setDataSource(dataSource);
35.
factoryBean.setHibernateProperties(buildHibernateProperties(environment));
36.
factoryBean.setPackagesToScan(packageOfModelBeans);
37.
return
factoryBean;
38.
}
39.
40.
protected
Properties buildHibernateProperties(Environment env) {
41.
Properties hibernateProperties =
new
Properties();
42.
43.
hibernateProperties.setProperty(
"hibernate.dialect"
, env.getProperty(
"hibernate.dialect"
));
44.
hibernateProperties.setProperty(
"hibernate.show_sql"
, env.getProperty(
"hibernate.show_sql"
));
45.
hibernateProperties.setProperty(
"hibernate.use_sql_comments"
, env.getProperty(
"hibernate.use_sql_comments"
));
46.
hibernateProperties.setProperty(
"hibernate.format_sql"
, env.getProperty(
"hibernate.format_sql"
));
47.
hibernateProperties.setProperty(
"hibernate.hbm2ddl.auto"
, env.getProperty(
"hibernate.hbm2ddl.auto"
));
48.
49.
hibernateProperties.setProperty(
"hibernate.generate_statistics"
, env.getProperty(
"hibernate.generate_statistics"
));
50.
51.
hibernateProperties.setProperty(
"javax.persistence.validation.mode"
, env.getProperty(
"javax.persistence.validation.mode"
));
52.
53.
//Audit History flags
54.
hibernateProperties.setProperty(
"org.hibernate.envers.store_data_at_delete"
, env.getProperty(
"org.hibernate.envers.store_data_at_delete"
));
55.
hibernateProperties.setProperty(
"org.hibernate.envers.global_with_modified_flag"
, env.getProperty(
"org.hibernate.envers.global_with_modified_flag"
));
56.
57.
return
hibernateProperties;
58.
}
59.
60.
@Bean
61.
public
HibernateTransactionManager hibernateTransactionManager(SessionFactory sessionFactory) {
62.
return
new
HibernateTransactionManager(sessionFactory);
63.
}
64.
}You can see how easy it is to use JavaConfig and Not XML.. The time of using XML files with Springs is over…
Down and Run Project
If you would like to download the project from GitHub and run it just follow the following commands:view sourceprint?1.
git clone git@github.com:JohnathanMarkSmith/NoMySQL.git
2.
cd NoMySQL
3.
mvn
package
4.
cd target
5.
java -jar NoMySQL.jarThats it and you should see the following line on the console:
2013-04-30 10:47:17,790 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - You Have 2 Message(s) In The Database2013-04-30 10:47:17,790 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - Message: ID: 1, Message: Hello World.2013-04-30 10:47:17,790 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - Message: ID: 2, Message: I love NYC.This Project is using Java, Spring, Hibernate, Maven, jUnit, Log4J, HSQLDB and Github.LIFE IS SO EASY WORKING WITH GIT, MAVEN, SPRING….
相关文章推荐
- javascript调试工具:Blackbird !say "hello" to Blackbird and "goodbye" to alert().
- Comparing Xamarin and Delphi XE5 to Xcode for Cross Platform Mobile App Development
- 【转载】M2K (Music-to-Knowledge): A tool set for MIR/MDL development and evaluation
- Linq Provider for MySql, Oracle and PostgreSQL Linq to Oracle 使用教程目录
- say goodbye to my visual studio 2010 and my code
- MySQL复制错误 The slave I/O thread stopsbecause master and slave have equal MySQL server UUIDs; these UUIDs must bedifferent for replication to work 解析
- How to set up ADB for remote machine development and local device deployment
- New Comer ,just say hello to everyone and me
- How to Cluster Magento, nginx and MySQL on Multiple Servers for High Availability
- (ZT) Is it legal (and moral) for a member function to say delete this?
- Rapid Mobile Enterprise Development for Symbian OS : An Introduction to OPL Application Design and P
- Introduction to Model Driven Development with AndroMDA
- Time to say goodbye
- 论文翻译:Development and Evaluation of Emerging Design Patterns for Ubiquitous Computing
- An Introduction to AJAX Techniques and Frameworks for ASP.NET
- When the RTP port is set to 8005, why is 8006 used by RTP and 8007 for RTCP for all communications ?
- Say goodbye to 2006
- HOWTO set up a MySQL Cluster for two servers
- [MUSIC]I DON'T WANT TO SAY GOODBYE
- How to get Intellisense for Web.config and App.config in Visual Studio .NET?(转载)