分类:
spring
一、
案例、
1、坐标
<dependencies> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--spring的jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--spring的text--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency> </dependencies>
2、
3、
4、
5、
6、
代码
package com.itheima.dao.impl; import com.itheima.dao.AccountDao; import com.itheima.domain.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import java.util.List; @Repository("accountDao") public class AccountDaoImpl implements AccountDao { @Autowired private JdbcTemplate jdbcTemplate; public void save(Account account) { String sql = "insert into account (name,money) values (?,?)"; jdbcTemplate.update(sql, account.getName(), account.getMoney()); } public void delete(Integer id) { String sql = "delete from account where id=?"; jdbcTemplate.update(sql, id); } public void update(Account account) { String sql = "update account set name=?,money=? where id=?"; jdbcTemplate.update(sql, account.getName(), account.getMoney(), account.getId()); } public List<Account> findAll() { //使用spring自带的行映射解析器.要求必须是标准封装 String sql = "select * from account"; return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class)); } public List<Account> findAll(Integer pageNum, Integer preNum) { String sql = "SELECT * FROM ACCOUNT LIMIT ?,?"; //使用spring自带的行映射解析器,要求必须是标准封装 return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class)); } public String findNameById(Integer id) { String sql = "select name from account where id =?"; return jdbcTemplate.queryForObject(sql, String.class, id); } public Account findById(Integer id) { String sql = "select * from account where id =?"; //支持自定义行映射解析器 RowMapper<Account> rm = (rs, rowNum) -> { Account account = new Account(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getDouble("money")); return account; }; return jdbcTemplate.queryForObject(sql, rm, id); } public Long getCount() { String sql = "SELECT COUNT(id) FROM ACCOUNT"; return jdbcTemplate.queryForObject(sql, Long.class); } }
7、
8、
9、
10、
二、具名参数
就是参数不是用问号来代替的
用法、
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术