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();
    }
}
相关推荐
码少女7 分钟前
MySQL--库与表的增删改查基础
java·数据库·mysql
Eyfcom11 分钟前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
打工的小王1 小时前
postgresql和mysql对比
数据库·mysql·postgresql
Flynt2 小时前
掘金热文说 Spring Boot 4.1 默认开虚拟线程,我实测后发现了更狠的坑
java·spring boot·性能优化
数据库急诊室_阿哲2 小时前
一次割接后的性能复核记录:基线、样本、回滚点
运维·数据库·dba
拄杖忙学轻声码2 小时前
Spring中的 HttpHeaders.readOnlyHttpHeaders() 作用是什么
spring
Resean02233 小时前
Spring 源码分析(一)IOC 容器总览与 refresh() 全景
java·后端·spring
保利威视频SaaS小张3 小时前
企业直播接入笔记 12|企业内训直播怎么做签到和学时统计,防挂机这件事比想象中难
java·前端·数据库·笔记·视频云·polvy·polyv
开开心心就好4 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
顧棟5 小时前
【ES实战】 索引模板「可追溯、可校验」:一种模板治理的工程化思路
java·数据库·elasticsearch