Spring: A Developer's Notebook笔记和小结(11)
2006-02-18 19:22
615 查看
/**
作者:Willpower
来源:Rifoo Technology(http://www.rifoo.com)
时间:2006-02-15
备注:转载请保留以上声明
**/
特别声明:离上一篇笔记已经有一个月的时间了,因为期间要过年,年前和年后都特别忙,所以没有继续更新,特此抱歉。
今天开始,我们要学习Spring在持久层的使用了。
首先我们先建立数据库和表结构,数据库使用的是开源的Mysql数据库:
1) 安装Mysql数据库(省略)
2) 进入Mysql命令行,输入以下命令列举所有现有的数据库:
mysql> create database bikestore;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+------------+
| Database |
+------------+
| bikestore |
| mysql |
| test |
+------------+
3 rows in set (0.00 sec)
这里我们用create database bikestore创建了一个数据库bikestore,另外两个是Mysql装好后就有的。
3)安装Mysql Connector/J,是JAVA程序用来连接Mysql数据库所用到的。就是后面说到的数据库驱动,将其中的mysql-connector-java-3.0.14-production-bin.jar文件放到/WEB-INF/lib/下。
4)创建表,以下是建表脚本rentabike.sql:
Example 4-2. rentabike.sql
drop database bikestore;
create database bikestore;
use bikestore;
create table bikes (
bikeId int(11) not null auto_increment,
manufacturer char(50) not null,
model char(50) not null,
frame int(11) not null default '0',
serialNo char(20) not null,
weight double not null default '0',
`status` char(15) not null,
primary key (bikeId));
create table customers (
custId int(11) not null auto_increment,
firstname char(50) not null,
lastname char(50) not null,
primary key (custId));
create table reservations (
resId int(11) not null auto_increment,
bikeId int(11) not null,
custId int(11) not null,
resDate date not null,
primary key (resId));
这里建立了三个表,bikes存放山地车信息,customers 存放顾客信息,reservations存放订购信息。建完表后,给我们的Mysql账号指定所有表的可操作权限。
5)试着写一个简单的例子程序来验证我们的数据库驱动是否正常:
Example 4-3. ControllerTest.java
public void testJDBC( ) throws Exception {
try {
System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/bikestore");
} catch (Exception ex) {
fail("Failed to open connection:" + ex.getMessage( ));
}
assertTrue(true);
}
6)往表里插入一些例子数据。
insert into bikes values(1, "Shimano", "Roadmaster", 20, "11111", 15, "Fair");
insert into bikes values(2, "Cannondale", "F2000 XTR", 18, "22222",12, "Excellent");
insert into bikes values(3, "Trek","6000", 19, "33333", 12.4, "Fair");
insert into customers values(1, "Justin", "Gehtland");
insert into customers values(2, "Bruce", "Tate");
insert into reservations values(1, 2, 2, '2004-09-15');
insert into reservations values(2, 3, 1, '2004-10-07');
7)修改ant的build.xml文件,使得我们在集成时使用rentabike.sql文件来创建数据库。因此我们新建两个property和一个指向数据库驱动文件的classpath路径。
Example 4-5. build.xml
<property name="database.url" value="jdbc:mysql://localhost/bikestore"/>
<property name="database.username" value="bikestore"/>
<path id="mysql.class.path">
<pathelement location="${war.dir}/WEB-INF/lib/
mysql-connector-java-3.0.14-production-bin.jar"/>
</path>
<target name="create.tables">
<sql driver="com.mysql.jdbc.Driver"
url="${database.url}"
userid="${database.username}"
password="">
<classpath>
<path refid="mysql.class.path"/>
</classpath>
<fileset dir="${db.dir}">
<include name="rentabike.sql"/>
</fileset>
</sql>
</target>
现在我们完成了简单的前期任务,为后面的开发工作打下了良好的外部基础。
作者:Willpower
来源:Rifoo Technology(http://www.rifoo.com)
时间:2006-02-15
备注:转载请保留以上声明
**/
特别声明:离上一篇笔记已经有一个月的时间了,因为期间要过年,年前和年后都特别忙,所以没有继续更新,特此抱歉。
今天开始,我们要学习Spring在持久层的使用了。
首先我们先建立数据库和表结构,数据库使用的是开源的Mysql数据库:
1) 安装Mysql数据库(省略)
2) 进入Mysql命令行,输入以下命令列举所有现有的数据库:
mysql> create database bikestore;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+------------+
| Database |
+------------+
| bikestore |
| mysql |
| test |
+------------+
3 rows in set (0.00 sec)
这里我们用create database bikestore创建了一个数据库bikestore,另外两个是Mysql装好后就有的。
3)安装Mysql Connector/J,是JAVA程序用来连接Mysql数据库所用到的。就是后面说到的数据库驱动,将其中的mysql-connector-java-3.0.14-production-bin.jar文件放到/WEB-INF/lib/下。
4)创建表,以下是建表脚本rentabike.sql:
Example 4-2. rentabike.sql
drop database bikestore;
create database bikestore;
use bikestore;
create table bikes (
bikeId int(11) not null auto_increment,
manufacturer char(50) not null,
model char(50) not null,
frame int(11) not null default '0',
serialNo char(20) not null,
weight double not null default '0',
`status` char(15) not null,
primary key (bikeId));
create table customers (
custId int(11) not null auto_increment,
firstname char(50) not null,
lastname char(50) not null,
primary key (custId));
create table reservations (
resId int(11) not null auto_increment,
bikeId int(11) not null,
custId int(11) not null,
resDate date not null,
primary key (resId));
这里建立了三个表,bikes存放山地车信息,customers 存放顾客信息,reservations存放订购信息。建完表后,给我们的Mysql账号指定所有表的可操作权限。
5)试着写一个简单的例子程序来验证我们的数据库驱动是否正常:
Example 4-3. ControllerTest.java
public void testJDBC( ) throws Exception {
try {
System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/bikestore");
} catch (Exception ex) {
fail("Failed to open connection:" + ex.getMessage( ));
}
assertTrue(true);
}
6)往表里插入一些例子数据。
insert into bikes values(1, "Shimano", "Roadmaster", 20, "11111", 15, "Fair");
insert into bikes values(2, "Cannondale", "F2000 XTR", 18, "22222",12, "Excellent");
insert into bikes values(3, "Trek","6000", 19, "33333", 12.4, "Fair");
insert into customers values(1, "Justin", "Gehtland");
insert into customers values(2, "Bruce", "Tate");
insert into reservations values(1, 2, 2, '2004-09-15');
insert into reservations values(2, 3, 1, '2004-10-07');
7)修改ant的build.xml文件,使得我们在集成时使用rentabike.sql文件来创建数据库。因此我们新建两个property和一个指向数据库驱动文件的classpath路径。
Example 4-5. build.xml
<property name="database.url" value="jdbc:mysql://localhost/bikestore"/>
<property name="database.username" value="bikestore"/>
<path id="mysql.class.path">
<pathelement location="${war.dir}/WEB-INF/lib/
mysql-connector-java-3.0.14-production-bin.jar"/>
</path>
<target name="create.tables">
<sql driver="com.mysql.jdbc.Driver"
url="${database.url}"
userid="${database.username}"
password="">
<classpath>
<path refid="mysql.class.path"/>
</classpath>
<fileset dir="${db.dir}">
<include name="rentabike.sql"/>
</fileset>
</sql>
</target>
现在我们完成了简单的前期任务,为后面的开发工作打下了良好的外部基础。
相关文章推荐
- Spring: A Developer's Notebook笔记和小结(6)
- Spring: A Developer's Notebook笔记和小结(7)
- Spring: A Developer's Notebook笔记和小结(1)
- Spring: A Developer's Notebook笔记和小结(8)
- [索引帖]Spring: A Developer's Notebook笔记和小结(共21篇,结束)
- [原创]Spring: A Developer's Notebook笔记和小结(21)全部完
- Spring: A Developer's Notebook笔记和小结(2)
- Spring: A Developer's Notebook笔记和小结(9)
- Spring: A Developer's Notebook笔记和小结(10)
- [原创]Spring: A Developer's Notebook笔记和小结(14)
- Spring: A Developer's Notebook笔记和小结(3)
- Spring: A Developer's Notebook笔记和小结(12)
- [原创]Spring: A Developer's Notebook笔记和小结(15)
- [原创]Spring: A Developer's Notebook笔记和小结(18)
- Spring: A Developer's Notebook笔记和小结(4)
- Spring: A Developer's Notebook笔记和小结(13)
- [原创]Spring: A Developer's Notebook笔记和小结(16)
- [原创]Spring: A Developer's Notebook笔记和小结(19)
- [原创]Spring: A Developer's Notebook笔记和小结(17)
- [原创]Spring: A Developer's Notebook笔记和小结(20)