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();
    }
}
相关推荐
Flynn Liang12 分钟前
大模型怎么自己调函数?Spring AI 2.0 Tool Calling 自动循环原理 + 完整时序图
java·人工智能·spring·agent·函数调用·springai·toolcalling
John_Hui1 小时前
Windows 10 安装版本:mysql-8.1.0-winx64.zip
数据库·windows·mysql
莫晓燚1 小时前
双机部署若依 (RuoYi‑Vue) 传统环境部署---实战
linux·运维·数据库·redis·mysql·nginx·ruoyi
乌暮1 小时前
请求进门先安检:Spring 拦截器与统一功能实战指南
java·开发语言·后端·学习·spring
学长毕设1 小时前
基于SpringBoot的烘焙商品商品系统-附源码
java·spring boot·后端
updayday8542 小时前
客户附件不该发往旧邮箱,如何在发送前拦截 - Ping64 邮件安全管控
大数据·网络·数据库·安全·智能路由器
倔强的石头_2 小时前
连接异常排障手册:拒连、超时、认证失败、空闲断开
数据库
金銀銅鐵2 小时前
[Java] 用纯 Swing 做一个《論語·學而》阅读器
java·ai编程·交互设计
lhbweilai2 小时前
QT(了解各个控件和布局)上
数据库·qt
莫晓燚3 小时前
搭建Redis 主从复制---实战
数据库·redis·缓存