企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 持久层总图 ![](https://box.kancloud.cn/690643a5bdaf4beccc2499b359c94bb8_1074x504.png) ## 概述 JDBCTemplate就是spring提供的dao层用来和数据库交互的技术.是spring对JDBC+C3P0的封装. 1. jdbc+c3p0. 2. DBUtils:apache组织提供的对JDBC+C3P0的封装. 3. hibernate:对JDBC+C3P0的封装. 4. JDBCTemplate:spring对JDBC+C3P0的封装. 5. HibernateTemplate:spring对hibernate又封装一次. 6. MyBatis:对JDBC+C3P0的封装. 7. SqlMapClientTemplate:spring对MyBatis又封装一次. ## 步骤 ~~~ <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> ~~~ ## 快速入门 ~~~ public static void main(String[] args) { //准备数据源,spring内置的数据源 DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://192.168.10.10:3306/jdbc"); dataSource.setUsername("homestead"); dataSource.setPassword("secret"); JdbcTemplate jt = new JdbcTemplate(dataSource); jt.execute("insert into account values (3,'richard',1000)"); } ~~~