Spring基础学习7——Spring持久层开发
普通jdbc使用
// 创建连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/homework");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// 配置jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("INSERT INTO student values('4','11','男','1') ");
使用注解配置jdbc
<!--配置连接池-->
<bean id="dataSSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/homework"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置jdbc模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSSource"/>
</bean>
普通的jdbc在连接数据库时需要频繁地进行数据库连接,比较消耗数据库资源,因此,我们用连接池的方法进行配置,节省资源
第三方连接池的使用
dbcp的使用
引入dbcp对应的jar包
在context中设置对应的连接池和模板
<!--配置连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/homework"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
其他使用方法和jdbc一致
c3p0的使用
引入c3p0对应的jar包
在context中配置对应的连接池和模板
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/homework"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
将配置内容进行提取到配置文件
1、创建配置文件jdbcConfigurer.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/homework
jdbc.user=root
jdbc.password=123456
在里面添加要提取的属性内容
2、在applicationContext中使用
<!--方式1:通过一个bean标签引入-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbcConfigurer.properties"/>
</bean>
<!-- 方式2:通过context标签引入-->
<context:property-placeholder location="classpath:jdbcConfigurer.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
查询对象
@Test
public void demo3(){
StudentBean mStudent = jdbcTemplate.queryForObject("select * from student where id =?", new MyRowMapper(), 5);
System.out.println(mStudent);
}
// 将查询的对象需要进行封装才能成为对象
class MyRowMapper implements RowMapper<StudentBean>{
@Override
public StudentBean mapRow(ResultSet resultSet, int i) throws SQLException {
StudentBean mStudent = new StudentBean();
mStudent.setSid(resultSet.getInt("sid"));
mStudent.setSname(resultSet.getString("sname"));
mStudent.setGender(resultSet.getString("Gender"));
return mStudent;
}
}