Spring集成JDBC

配置阿里巴巴提供的数据库连接类,让Spring帮忙管理

java 复制代码
<context:property-placeholder location="classpath:config.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${uname}"></property>
        <property name="password" value="${upassword}"></property>
        <property name="initialSize" value="${initialSize}"></property><!--初始化连接数量-->
        <property name="maxActive" value="${maxActive}"></property><!--最大连接数量-->
        <property name="maxWait" value="${maxWait}"></property><!--最大等待超时时间-->
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

JdbcTemplate是Spring对jdbc的封装,提供了对数据库操作的方法,里面还可以直接包含数据源对象。

java 复制代码
@Data
@Repository
public class UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    Integer id;
    String name;

    public void select(){
        jdbcTemplate.update("insert into user(name)value (?)","aaa");
    }
}
java 复制代码
@Data
@Service("userService")
public class UserService {
    @Resource
    UserDao userDao;
    public void select(){
        userDao.select();
    }
}
java 复制代码
public class test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");//读取配置文件
        UserService userService=applicationContext.getBean("userService",UserService.class);
        userService.select();
    }
}
相关推荐
kirs_ur2 小时前
ECC & LDPC — SSD 的数据卫士
服务器·数据库·性能优化
qq_185198692 小时前
(Spring Bean + delegateExpression)实现http服务 的完整可运行项目
spring
是三一seven3 小时前
Sql注入基础
数据库·安全·网络安全
小Ti客栈3 小时前
Spring Boot 集成 Springdoc-OpenAPI 与 Knife4j实现接口文档与可视化调试
java·spring boot·后端
Sirens.3 小时前
MySQL表设计进阶-约束范式连接索引与事务
android·数据库·mysql
Ai拆代码的曹操3 小时前
Spring 事务 REQUIRES_NEW 嵌套调用:连接池翻倍的秘密
java·后端·spring
动恰客流统计4 小时前
ReID边缘计算视觉统计:餐饮店客流增长的数字化破局路径
java·大数据·运维·人工智能
字节跳动开源4 小时前
火山引擎开源 Agent 驱动的搜索自迭代技术
数据库·开源·agent
Ivanqhz4 小时前
Rust &‘static str浅析
java·前端·javascript·rust
Oo大司命oO6 小时前
藏在正则表达式里的陷阱
数据库·mysql·正则表达式