使用embedded database

这篇文章没有上下文,只是我自己的备忘,等以后我搞清楚了,再把文章的前后文加上吧。


首先新建  embeddedDataSource.xml,编辑内容如下:


<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- 使用内存数据库 -->
    <jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:schema.sql"/>
        <jdbc:script location="classpath:data.sql"/>
    </jdbc:embedded-database>

</beans>

--------------------------------------------------------------------------------------------------------------

其次,在web.xml中加入以下内容:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:embeddedDataSource.xml</param-value>
    </context-param>

--------------------------------------------------------------------------------------------------------------

然后,在classpath目录下,新建 schema.sql 和 data.sql

schema.sql 内容如下:

drop table expenseRecords if exists;
drop table employeeInfo if exists;
create table expenseRecords(uid varchar(5), type varchar(20), cost decimal(6,2));
create table employeeInfo(uid varchar(5), uname varchar(20), exp decimal(3,1));


data.sql 内容如下:

insert into expenseRecords(uid, type, cost) values('00001', 'Books', 150);
insert into expenseRecords(uid, type, cost) values('00002', 'Training/Education', 800);
insert into expenseRecords(uid, type, cost) values('00003', 'Books', 40);
insert into expenseRecords(uid, type, cost) values('00004', 'Books', 70.5);
insert into expenseRecords(uid, type, cost) values('00005', 'Books', 900);
insert into expenseRecords(uid, type, cost) values('00006', 'Training/Education', 60.5);
insert into expenseRecords(uid, type, cost) values('00002', 'Books', 160.5);
insert into expenseRecords(uid, type, cost) values('00001', 'other', 300);
insert into expenseRecords(uid, type, cost) values('00001', 'Training/Education', 1000);


insert into employeeInfo(uid, uname, exp) values('00001', 'guanyuan', 1.1);
insert into employeeInfo(uid, uname, exp) values('00002', 'lihongjing', 0.2);
insert into employeeInfo(uid, uname, exp) values('00003', 'zhaolan', 0.7);
insert into employeeInfo(uid, uname, exp) values('00004', 'chendan', 1.5);
insert into employeeInfo(uid, uname, exp) values('00005', 'xiefang', 2.1);
insert into employeeInfo(uid, uname, exp) values('00006', 'pangmin', 0.9);

--------------------------------------------------------------------------------------------------------------

最后,修改DataManager.java 文件内容全部替换为:

    private static EmbeddedDatabase db;

    static {

        db = new EmbeddedDatabaseBuilder().addDefaultScripts().build();    

//这里其实应该不需要的把,因为已经将datasource注册为bean了呀,可是不加测试不能通过,不知道是为什么?

    }


    public static ResultSet getResultSet(String sql) throws SQLException {
        Connection conn = db.getConnection();
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet;
    }


--------------------------------------------------------------------------------------------------------------

最后的最后,写测试:

    @Test
    public void test() throws SQLException {
        String sql = "select * from employeeInfo where uid = '00001';"; //查询用户信息
        ResultSet resultSet = DataManager.getResultSet(sql);
        String result = null;
        while (resultSet.next()) {
            result = resultSet.getString("uname");
        }
        assertEquals("guanyuan", result);
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值