Spring框架—day04
spring连接数据库
1.最原始的方法,通过spring的xml文件的方式来进行注入。
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="conn" class="com.zll.db.Conn">
<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/bigdata06?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
//实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Conn {
private String driver;
private String url;
private String username;
private String password;
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
}
public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("bean.xml");
Conn conn=app.getBean("conn",Conn.class);
System.out.println(conn.getConnection());
}
}
也可以使用注解的方法,但是使用注解时比较麻烦,当需要修改时,需要修改代码,使用xml文件的方式,可以把代码分离,修改时修改xml文件即可。
2.使用外部文件 properties(属性文件上)
属性文件(properties)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/bigdata06?useUnicode=true&characterEncoding=utf8&useSSL=false&&serverTimezone=UTC
username=root
password=1234
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: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) -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location"><value>jdbc.properties</value></property>
</bean>
<!-- spring3.0 提供的新标签 -->
<context:property-placeholder location="jdbc.properties" />
<bean id="conn02" class="com.zll.db02.Conn">
<property name="driver"><value>${driver}</value></property>
<property name="url"><value>${url}</value></property>
<property name="username"><value>${username}</value></property>
<property name="password"><value>${password}</value></property>
</bean>
</beans>
实体类和测试类和1中的一样
3.spring 注入c3p0
属性文件(properties)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/bigdata06?serverTimezone=UTC
user=root
password=1234
<?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: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">
//导入外部文件
<context:property-placeholder location="jdbc.properties"/>
<bean id="c3p0DataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- name不能随便起名字,要根据对应的去找 -->
<property name="driverClass"><value>${driver}</value></property>
<property name="jdbcUrl"><value>${url}</value></property>
<property name="user"><value>${user}</value></property>
<property name="password"><value>${password}</value></property>
</bean>
</beans>
public static void main(String[] args) throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("beanc3p0.xml");
ComboPooledDataSource c1=app.getBean("c3p0DataSources",ComboPooledDataSource.class);
System.out.println(c1.getConnection());
}
4.spring注入druid
属性文件(properties)和上面一样
<context:property-placeholder location="jdbc.properties"/>
<bean id="druid01" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>${url}</value></property>
<property name="username"><value>${user}</value></property>
<property name="password"><value>${password}</value></property>
</bean>
public static void main(String[] args) throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("beanDruid.xml");
DruidDataSource c1=app.getBean("druid01",DruidDataSource.class);
System.out.println(c1.getConnection());
}
5.spring注入hikariCP
<context:property-placeholder location="jdbc.properties"/>
<bean id="hikariCP" class="com.zaxxer.hikari.HikariDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="jdbcUrl"><value>${url}</value></property>
<property name="username"><value>${user}</value></property>
<property name="password"><value>${password}</value></property>
</bean>
public static void main(String[] args) throws SQLException {
ApplicationContext app=new ClassPathXmlApplicationContext("beanhikariCP.xml");
HikariDataSource c1=app.getBean("hikariCP",HikariDataSource.class);
System.out.println(c1.getConnection());
}
使用的这种方式主要是由许多东西注入式会使用这种方式,例如:数据库的连接,事务管理,缓存的操作等等。
Spring中bean和bean之间有继承关系
<?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: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">
<bean id="father" class="com.zll.parent.Father">
<property name="id" value="111"></property>
<property name="name" value="lxy"></property>
<property name="password" value="zll"></property>
</bean>
<bean id="son" class="com.zll.parent.Son" parent="father">
<property name="id" value="666"></property>
<property name="name" value="zll"></property>
<property name="password" value="zll999"></property>
</bean>
</beans>
子类和父类之间有了相关的关系, 子类就可以覆盖父类的属性和方法。parent=“father”,parent标签建立联系
实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Father {
public int id;
public String name;
public String password;
}
public class Son extends Father{
}
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("beanParent.xml");
Father father=app.getBean("father", Father.class);
System.out.println(father.id+"\t"+father.name);
Son son=app.getBean("son",Son.class);
System.out.println(son.id+"\t"+son.name);
}