Spring连接数据库的⼏种常⽤的⽅式开发中的⼤部分知识,需要我们去总结:
下⾯我们看看啊,
本⽂简单的讲解使⽤Spring连接数据库的⼏种常⽤⽅法:
测试主类为:
package myspring2;
import java.sql.*;
import javax.sql.DataSource;
import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
public class MySpringTest {
public static void main(String args[]) throws Exception{
ApplicationContext ctx=new ClassPathXmlApplicationContext("l");
DataSource Bean("dataSource",DataSource.class);
spring怎么读取jar文件String sql="select * from user_inf";
Connection Connection();
Statement ateStatement();
ResultSet uteQuery(sql);
())
{      System.out.println("⽤户名为:");
System.out.String(2));
}
}
}
第⼀种:使⽤spring⾃带的DriverManagerDataSource  配置⽂件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:p="/schema/p"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/aop
/schema/aop/spring-aop-3.0.xsd">
<!-- 使⽤XML Schema的p名称空间配置 -->
<bean name="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="sql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/test"
p:username="root"
p:password="123456"  / >
<!-- 采⽤property的普通配置相⽐之下有点⿇烦,但是效果是⼀样的哦,-->
<!--
<bean name="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"  value="sql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
-->
</beans>
第⼆种:C3P0数据源。
需要使c3p0的核⼼jar包,我使⽤的是c3p0-0.9.1.jar,⽐较稳定,推荐使⽤。⼀般在下载hibernate的时候都会⾃带⼀个:我在hibernate-release-4.3.0.Final\lib\optional\c3p0路径下到的。
配置⽂件中如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:p="/schema/p"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/aop
/schema/aop/spring-aop-3.0.xsd">
<!-- 使⽤XML Schema的p名称空间配置  -->
<bean name="dataSource"class="hange.v2.c3p0.ComboPooledDataSource"
p:driverClass="sql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost:3306/test"
p:user="root"
p:password="123456" >
</bean>
<!-- 采⽤property的普通配置相⽐之下有点⿇烦,但是效果是⼀样的哦建议使⽤上⾯的-->
<!--      <bean name="dataSource"class="hange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"  value="sql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
-->
</beans>
第三种:
使⽤apache的dbcp插件连接数据库需要下载的jar包:commons-dbcp.jar,commons-pool.jar,commons-collection.jar  spring的配置⽂件中如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:p="/schema/p"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/aop
/schema/aop/spring-aop-3.0.xsd">
<!-- 使⽤XML Schema的p名称空间配置 -->
<bean name="dataSource"class="org.apachemons.dbcp.BasicDataSource"
p:driverClassName="sql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/test"
p:username="root"
p:password="123456" >
</bean>
<!-- 采⽤property的普通配置相⽐之下有点⿇烦,但是效果是⼀样的哦建议使⽤上⾯的-->
<!--      <bean name="dataSource"class="org.apachemons.dbcp.BasicDataSource">
<property name="driverClassName"  value="sql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
-->
</beans>
第四种:
使⽤hibernate数据源需要hiberante核⼼jar包,我使⽤的hibernate1的版本是hibernate-release-4.3.0.Final
⽬前三⼤框架较流⾏,spring⼀般与hiberante做搭档,数据库连接⽅式写在hiberante的配置⽂件中,在spring管理hibernate中的配置⽂件中,直接读取hibernate核⼼配置⽂件即可。在使⽤hibernate连接数据库的时候需要读取l的配置⽂件和相应的实体类,
读者可参照下⾯的⾃⼰配置⼀下
<bean id="sessionFactory"class="hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<list>
<value>classpath:com/config/l</value>
</list>
</property>
<property name="mappingLocations">
<!-- 所有的实体类映射⽂件 -->
<list>
<value>classpath:com/hibernate/*.l</value>
</list>
</property>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。