1.导入坐标
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.3.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.3.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency></dependencies>
2.测试
1.准备数据源, Spring 内置的数据源DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUsername("root");dataSource.setPassword("123");dataSource.setUrl("jdbc:mysql://localhost:3306/test");//JdbcTemplate jt = new JdbcTemplate(dataSource); 构造方法传入一样JdbcTemplate jt = new JdbcTemplate();jt.setDataSource(dataSource);jt.execute("insert into account(name ,money)values ('wywyc',15000)");
IOC版
1.bean.xml配置
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="datasource"></property></bean><bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value="123"></property></bean>
2.测试
1.获取容器ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");JdbcTemplate jdbcTemplate = ac.getBean("jdbcTemplate",JdbcTemplate.class);jdbcTemplate.execute("insert into account(name ,money)values ('mmmmm',1500)");jdbcTemplate.update("insert into account(name ,money)values (?,?)","llll",456);jdbcTemplate.update("update account set name =?,money=? where id = ?","333",789,9);jdbcTemplate.update("delete from account where id=?",7);List<Account> accounts = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));for (Account account : accounts) {System.out.println(account);}查询一个Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class),9);System.out.println(account);聚合函数Integer count = jdbcTemplate.queryForObject("select count(*) from account where money > ?", int.class, 500);System.out.println(count);
JdbcDaoSupport
在持久层 每一有一个 Dao 类 都会定一个 Private JdbcTemlate jdbctemplate;
所以继承JdbcDaoSupport这个类 就可以不用了 并且继承的方法 在用Spring IOC开发时 ,只能使用 xml配置的方式注入
用 uper.getJdbcTenplate( ) 调用 父类的get方法 获取 jdbctemplate 对象
配置父类的set需要的DataSource类型的参数 传入 datasource
<bean id="accountDao" class="com.wyc.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"></property></bean>
配置数据源-<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value="123"></property></bean>