curd 1. 实现步骤 2. maven dependency 3. curd代码
database: oracle dataSource: alibaba druid
1. 实现步骤
1. 导入spring-jdbc 和 spring-tx(事务)坐标
2. 创建数据库表和实体
3.创建JdbcTemplate对象JdbcTemplate jdbc = new JdbcTemplate();jdbc.setDataSource(dataSource);
4. 执行数据库操作更新操作jdbc.update(sql, params)查询操作jdbc.query(sql, Mapper, params)jdbc.queryForObject(sql, Mapper, params)
2. maven dependency
< dependency> < groupId> com.alibaba</ groupId> < artifactId> druid</ artifactId> < version> 1.1.10</ version> </ dependency> < dependency> < groupId> com.oracle.database.jdbc</ groupId> < artifactId> ojdbc6</ artifactId> < version> 11.2.0.4</ version> </ dependency> < dependency> < groupId> org.springframework</ groupId> < artifactId> spring-jdbc</ artifactId> < version> 4.3.8.RELEASE</ version> </ dependency> < dependency> < groupId> com.labun</ groupId> < artifactId> spring-tx</ artifactId> < version> 5.2.1.RELEASE.patched</ version> </ dependency>
3. curd代码
package com. lovely. jdbc_template; import com. lovely. entity. User;
import org. springframework. context. ApplicationContext;
import org. springframework. context. support. ClassPathXmlApplicationContext;
import org. springframework. jdbc. core. JdbcTemplate; import org. springframework. jdbc. core. BeanPropertyRowMapper; import java. util. List; public class SpringJdbcTemplateCurd { private static final JdbcTemplate jdbcTemplate; static { ApplicationContext app = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ; jdbcTemplate = app. getBean ( JdbcTemplate. class ) ; } public static void main ( String[ ] args) { queryOne ( ) ; queryCount ( ) ; } static void queryOne ( ) { User user = jdbcTemplate. queryForObject ( "select * from users where id = ?" , new BeanPropertyRowMapper < User> ( User. class ) , 1 ) ; System. out. println ( user) ; } static void queryCount ( ) { Integer count = jdbcTemplate. queryForObject ( "select count(*) from users" , Integer. class ) ; System. out. println ( count) ; } static void queryAllUsers ( ) { List< User> query = jdbcTemplate. query ( "select * from users" , new BeanPropertyRowMapper < User> ( User. class ) ) ; System. out. println ( query) ; } public static void save ( ) { jdbcTemplate. update ( "insert into users values (?, ?, ?)" , new Object [ ] { 5 , "gorgeous" , 111 } ) ; } public void update ( ) { String sql = "update users set name=? where id=?" ; jdbcTemplate. update ( sql, new Object [ ] { "小李子" , 4 } ) ; } public void delete ( ) { jdbcTemplate. update ( "delete from user where id = ?" , new Object [ ] { 5 } ) ; } }
spring xml配置 (将DataSource和JdbcTemplate的创建交给IOC容器)
< context: property-placeholder location = " classpath:jdbc.properties" /> < bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" > < property name = " driverClassName" value = " ${jdbc.driver}" /> < property name = " url" value = " ${jdbc.url}" /> < property name = " username" value = " ${jdbc.userName}" /> < property name = " password" value = " ${jdbc.userPassword}" /> </ bean> < bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" > < property name = " dataSource" ref = " dataSource" > </ property> </ bean>
更多文章👇 spring数据源配置 注解方式创建bean