Spring相关接口和配置数据源

Spring的相关接口(API):

  1. ApplicationContext:接口类型,代表应用上下文,可以通过其实例获得spring容器中的bean对象

ApplicationContext的实现类:

  1) ClassPathApplicationContext 它是从类的根路径下加载配置文件推荐使用

  2) FileSystemXmlApplicationContext 它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置

  3) AnnotationConfigApplicationContext 当使用注解配置容器对象时,需要使用此类创建spring容器,它是用来读取注解。

在配置好Spring的核心配置文件(applicationContext.xml)后,我们在使用这配置文件总是会使用以下两句:

//链接spring容器
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//下面getBean中的id是在配置文件中id名字,
app.getBean("id");

Spring配置数据源

之前学习jdbc连接数据库

1、加载数据库驱动;

  Class.forName(“com.mysql.jdbc.Driver”);

 

2、获取数据库连接;

  通过Connection建立连接,Connection是一个接口类。其功能是与数据库进行连接(会话)。

建立Connection接口类对象:

Connection conn =DriverManager.getConnection(url, user, password);

 

3、创建sql操作;
  运行对象Statement负责运行SQL语句。由Connection对象产生。

  Statement st = connection.createStatement();

  Statement接口类还派生出两个接口类PreparedStatement和CallableStatement,这两个接口类对象为我们提供了更加强大的数据訪问功能。

  PreparedStatement能够对SQL语句进行预编译,这样防止了   SQL注入 提高了安全性。

  PreparedStatement  ps=connection.prepareStatement( "update user set id=? where username=?”); ————sql语句中庸 ? 作为通配符,变量值通过参数设入:ps.setObject(1, object);

  而且预编译结果能够存储在PreparedStatement对象中。当多次运行SQL语句时能够提高效率。并且使用PrepareStatement对象还能有效的避免sql语句的注入,提高对数据库安全性。

 

4、执行sql语句;
executeQuery(String sql),该方法用于运行实现查询功能的sql语句。返回类型为ResultSet(结果集)。

  如:ResultSet  rs =st.executeQuery(sql);

  executeUpdate(Stringsql),该方法用于运行实现增、删、改功能的sql语句,返回类型为int,即受影响的行数。

  如:int flag = st.executeUpdate(sql);

 

5、处理结果集

 

6、释放资源。(后开先关的原则)

 

数据源的作用:

  数据源(连接池)是提高程序性能

       事先实例化数据源,初始化部分连接部分

       使用连接资源从数据源中获取

       使用完毕后连接资源归返给数据源

常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等

 

使用spring配置数据源:

开发步骤:

1、 导入连接池的坐标和数据库的驱动坐标

2、 创建连接池对象

3、 设置数据源的基本连接数据

 下面以Druid数据源为例:

DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
DruidPooledConnection connection = druidDataSource.getConnection();
System.out.println(connection);
connection.close();

通过上例我们不难发现它们是直接把数据池写道数据里--耦合。

于是我们使用jdbc.properties文件

 

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/本地数据库的名字
jdbc.username=用户名
jdbc.password=密码

使用spring加载数据库的配置文件可以进一步的进行解耦,可以做到一部分文件执行一部文件的内容,逐步的解耦便于后续的开发。在jdbc.properties文件的同级下创建一个Spring配置文件applicationContext.xml

在配置文件中我们需要使用jdbc的配置文件,这就需要我们在spring配置文件中是使用context标签来加载外部的文件

context 需要在跟标签下添加

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


 

<!--    加载外部的properties文件-->

<context:property-placeholder location="classpath:db.properties"/>

 在spring配置文件使用Druid数据源

<bean id="db" class="com.alibaba.druid.pool.DruidDataSource" >
<!-- <property name="driverClass" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/****"/>-->
<!-- <property name="user" value="root"/>-->
<!-- <property name="password" value="****"/>-->
<!-- 使用外部配置文件的属性名进行加载数据库-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

 在bean中有一段是注释的是未使用jdbc的配置文件。

然后再测试类中创建一个测试类:

 @Test
public void test4() throws SQLException {
// 链接spring容器
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
// 从容器中获取所需的链接池的资源
DataSource dataSource = app.getBean("db",DataSource.class);
// 加载驱动
Connection connection = dataSource.getConnection();
System.out.println(connection);
}

 

 

 

 

 

 

 

posted @   孤巷一人i  阅读(235)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示