JdbcTemplate主要提供以下五类方法:
1、execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
Execute、executeQuery、executeUpdate
2、update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句 SQL SERVCER(GO SQL语句 GO) ;
3、query方法及queryForXXX方法:用于执行查询相关语句;
4、call方法:用于执行存储过程、函数相关语句。
首先导入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency>
数据库结构
4.添加数据
添加entity包,创建Book类
@Data @AllArgsConstructor @NoArgsConstructor public class Book { private String bookid; private String bookName; private String bookprice; }
创建Dao层面添加方法
public interface IBookDao { List<Book> findAll(); }
创建DaoMapper重写方法
方法继承JdbcDaoSupport
public class IBookDaoImpl extends JdbcDaoSupport implements IBookDao { @Override public List<Book> findAll() { JdbcTemplate jdbcTemplate = this.getJdbcTemplate(); String sql = "select * from book"; /*List<Book> list = jdbcTemplate.query(sql, new RowMapper<Book>() { @Override *//** * * @param rs * @param rowNum * @return * @throws SQLException *//* public Book mapRow(ResultSet rs, int rowNum) throws SQLException { Book book = new Book(); book.setBookid(rs.getString("bookid")); book.setBookName(rs.getString("bookName")); book.setBookprice(rs.getString("bookprice")); return book; } });*/ RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class); List<Book> list = jdbcTemplate.query(sql,rowMapper); return list; } }
创建Service
public interface IBookService { List<Book> findAll(); }
创建ServiceImpl
public class IBookServiceImpl implements IBookService { private IBookDao iBookDao; public void setiBookDao(IBookDao iBookDao) { this.iBookDao = iBookDao; } public IBookDao getiBookDao() { return iBookDao; } @Override public List<Book> findAll() { return iBookDao.findAll(); } }
配置applicationContext-day19jdbc.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--1.配置数据源--> <!--Spring内置的内置源:创建或者用来提供连接的,不负责管理,使用连接池--> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--2.识别到配置文件--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="datasource"></property> </bean> <!--4.创建Dao--> <bean id="bookDao" class="cn.jdbc.dao.IBookDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--5.Service--> <bean id="bookService" class="cn.jdbc.service.IBookServiceImpl"> <property name="iBookDao" ref="bookDao"></property> </bean> </beans>
编写测试类TextJDBC
@Test public void tesss(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-day19jdbc.xml"); IBookService bookService = (IBookService) ctx.getBean("bookService"); List<Book> all = bookService.findAll(); for (Book book:all){ System.out.print(book.getBookid()+"\t"); System.out.print(book.getBookName()+"\t"); System.out.print(book.getBookprice()); System.out.println(); } }
运行效果
原文:https://www.cnblogs.com/ws1149939228/p/11787367.html