今天花了一部分时间调整心情,花了大部分时间整合spring与其它产品的练习。
在前面,我们已经学习了基于spring上下文的ioc和依赖注入。 我们听的和接触的最多的估计就是orm相关的整合了。 因此这篇就针对这个进行练习。
先看项目的依赖:
<denpencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</denpendencies>
一个我们自己连接数据库的例子。
package com.automannn.springZhenhe.dao;
import java.sql.*;
/**
* @author automannn@163.com
* @time 2018/10/16 12:48
*/
public class MyOwnPersonInfo {
private Statement statement;
private Connection connection;
private ResultSet resultSet;
private String driverClassName;
private String url;
private String username;
private String password;
public MyOwnPersonInfo(){
}
private void conection() {
try {
connection= DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void initt() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public boolean execute(String sql) throws SQLException {
initt();
conection();
statement = connection.createStatement();
boolean bb =statement.execute(sql);
close();
return bb;
}
public void close() throws SQLException {
this.statement.close();
this.connection.close();
}
public Statement getStatement() {
return statement;
}
public void setStatement(Statement statement) {
this.statement = statement;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public ResultSet getResultSet() {
return resultSet;
}
public void setResultSet(ResultSet resultSet) {
this.resultSet = resultSet;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
可以看到,就是我们上java基础课时所学过的一些知识。
针对这里的参数需要做一些说明:
一,写这个类,并没有依赖任何的第三方,完全是按照 java 的 JDBC标准来的。
二,我们需要动态加载 相应的数据库服务商的驱动。 并且只是加载,并无其它操作,为什么呢? 看源码:
可以看到,在DriverManager类中有一个静态块方法。
三,当我们用这种方式操作数据库的时候,需要自己手动关闭连接。 否则数据库那边会维持连接信息,造成不必要的资源开销。
由于我们要将它交给spring容器管理,因此,我们需要将自己的类交给spring容器管理,这里采用配置文件的方式:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--
引入properties配置文件
-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="myOwnPersonInfo" class="com.automannn.springZhenhe.dao.MyOwnPersonInfo">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>
属性文件:
然后我们看看启动类:
package com.automannn.springZhenhe;
import com.automannn.springZhenhe.dao.MyBatisPersonDao;
import com.automannn.springZhenhe.dao.MyHibernatePersonDao;
import com.automannn.springZhenhe.dao.MyOwnPersonInfo;
import com.automannn.springZhenhe.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author automannn@163.com
* @time 2018/10/16 11:31
*/
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
MyOwnPersonInfo myOwnPersonInfo = (MyOwnPersonInfo) context.getBean("myOwnPersonInfo");
myOwnPersonInfo.execute("insert into person(pid,pname) values(8,'fff')");
}
}
运行程序,这个时候就可以将数据插入到数据库了: