Spring集成Jdbc

近期在找工作的,加上学校的事情没有更新。下面接着之前的更新。

JdbcTemplate是Spring对JDBC的封装,目的是使JDBC更加易于使用。
JdbcTemplate是Spring的一部分。JdbcTemplate处理了资源的建立和释放。
他帮助我们避免一些常见的错误,比如忘了总要关闭连接。他运行核心的JDBC工作流,
如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。

Spring配置数据源三种方式

测试类

package com.it.jdbc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("classpath:applicationContext2.xml")
 @ContextConfiguration("classpath:applicationContext3.xml")
 @ContextConfiguration("classpath:applicationContext4.xml")
 @ContextConfiguration("classpath:applicationContext5.xml")
public class SpringJdbcTest2 {
      @Resource
     private JdbcTemplate jdbcTemplate;
     @Test
     public void test1(){
         int rs = jdbcTemplate.update("insert into account(name,money) values(?,?)","chenchen",19000L);

         System.out.println(rs);
     }
}

一、使用spring自带的DriverManagerDataSource

<?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"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">
     <!-- spring管理jdbc和数据库连接池-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="password" value="root"></property>
        <property name="username" value="root"></property>
        <property name="url" value="jdbc:mysql:///shop"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

第二种:DBCP数据源。
需要下载的jar包:commons-dbcp.jar,commons-pool.jar

<?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"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">
    <!--    使用 开源的 数据库连接池DBCP-->
    <!-- spring管理dbcp数据库连接池-->
    <bean  id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="url" value="jdbc:mysql:///shop"></property>
        <property name="maxActive" value="20"></property>
        <property name="maxIdle" value="5"></property>
        <property name="minIdle" value="2"></property>
        <property name="maxWait" value="10000"></property>
    </bean>
    <!-- spring管理jdbc模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

第三种:C3P0数据源

<?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"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">
     <!-- spring管理c3p0数据库连接池-->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
      <property name="jdbcUrl" value="jdbc:mysql:///shop"></property>
      <property name="password" value="root"></property>
      <property name="user" value="root"></property>
  </bean>
    <!-- spring管理jdbc模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

为了方便修改,我们将连接数据库的数据分装到db.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/shop
user=root
password=root
<?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"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">

    <!--    第一种-->
<!--    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!--        <property name="location" value="db.properties"></property>-->
<!--    </bean>-->
    <!--    第二种-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="password" value="${password}"></property>
        <property name="user" value="${user}"></property>
        <property name="jdbcUrl" value="${url}"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

这里需要注意数据库连接错误A ResourcePool could not acquire a resource from its primary factory or source
oldbig是我的电脑用户名,证明我的配置username引入的是我的本地用户名,而不是我的数据库用户名,原来这时候
用了${username}之后它好像会取当前计算机用户名来连数据库,applicationContext5.xml和jdbc.properties的中username改成user,一测试成功!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring可以很方便地集成MyBatis,下面是集成步骤: 1. 引入MyBatis和Spring的依赖,例如: ```xml <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> ``` 2. 配置数据源 在Spring的配置文件中配置数据源,例如: ```xml <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> ``` 3. 配置SqlSessionFactory 在Spring的配置文件中配置SqlSessionFactory,例如: ```xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.example.model"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> ``` 其中,typeAliasesPackage指定实体类所在的包,mapperLocations指定Mapper文件所在的位置。 4. 配置MapperScannerConfigurer 在Spring的配置文件中配置MapperScannerConfigurer,例如: ```xml <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 其中,basePackage指定Mapper接口所在的包。 5. 编写Mapper接口和Mapper.xml 编写Mapper接口和Mapper.xml文件,例如: ```java public interface UserMapper { User getUserById(int id); } ``` ```xml <select id="getUserById" resultType="com.example.model.User"> select * from user where id = #{id} </select> ``` 6. 注入Mapper 在需要使用Mapper的地方注入Mapper,例如: ```java @Autowired private UserMapper userMapper; ``` 至此,Spring集成MyBatis的配置完成,可以愉快地使用MyBatis了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值