SSM学习9:管理第三方数据源-Druid对象

   目录

建立空项目 

建立一个模块:maven项目

建立数据层dao和业务层service的接口和实现类文件

BookDao.class

BookDaoimpl.class

BookService.class

BookServiceimpl.class

建立测试类app.class

在pom.xml导入spring的两个坐标

建立配置文件applicationContext.xml,

测试结果


建立空项目 

建立一个模块:maven项目

建立数据层dao和业务层service的接口和实现类文件

目录如下:

BookDao.class

package com.itheima.dao;

public interface BookDao {
    public void save();

}

BookDaoimpl.class

package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
    public UserDaoImpl() {}
    private List<String> stringList;
    private Map<String, User> userMap;
    private Properties properties;
    public void setStringList(List<String> stringList) { this.stringList = stringList; }
    public void setUserMap(Map<String, User> userMap) { this.userMap = userMap;}
    public void setProperties(Properties properties) { this.properties = properties; }
    @Override
    public void save() {
        System.out.println(stringList);
        System.out.println(properties);
    }
}

BookService.class

package com.itheima.service;

public interface BookService {
    public void save();
}

BookServiceimpl.class

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.dao.impl.BookDaoimpl;
import com.itheima.service.BookService;

public class BookServiceimpl implements BookService {
    private BookDao book;

    public void save() {
        System.out.println("BookService运行了");
        book.save();


    }
    //alt+insert快速生成set方法,再去配置文件中让数据层和业务层有关联

    public void setBook(BookDao book) {
        this.book = book;
    }
}

建立测试类app.class

package com.itheima;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

public class APPText {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);

    }
}

在pom.xml导入spring的两个坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springioc05</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

建立配置文件applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<!--    管理Druid数据库池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="mysql://localhost:3306/db_librarysys"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
</beans>

测试结果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值