(感谢您的关注和点赞支持!)
Spring JdbcTemplate
Spring提供的数据访问模板,提供了很多方便操作数据库的方法
- update (更新数据)
- batchUpdate (批量更新数据库)
- queryForObject (查询单行)
- query (查询多行)
- queryForObject (单值查询)不同的
步骤:
1. 添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
完整 pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring06-JdbcTemplated</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>
2. Spring 配置文件
spring.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: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/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--
JdbcTempldate
datasource - JdbcTempldate - Dao -Service -Controller
-->
<context:component-scan base-package="com.wdzl"/>
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/booksys"></property>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
</bean>
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
3. 编写类
实体类 Book :
@Data
public class Book {
private Integer bookid;
private String bookname;
private Float price;
private String author;
private Date pubDate;
}
Dao 接口 IBookDao :
public interface IBookDao {
List<Book> queryAll();
Book get(Integer bookid);
void save(Book book);
}
Dao 接口实现类 BookDaoImpl:
@Repository("bookDao")
public class BookDaoImpl implements IBookDao {
@Autowired
private JdbcTemplate template;
@Override
public List<Book> queryAll() {
String sql = "select bookid,bookname,pubdate,price,authorid from book";
List<Book> list = template.query(sql, new BookRowMapper());
return list;
}
@Override
public Book get(Integer bookid) {
return null;
}
@Override
public void save(Book book) {
}
// 内部类 实现 RowMapper 接口
class BookRowMapper implements RowMapper{
@Override
public Book mapRow(ResultSet resultSet, int i) throws SQLException {
Book book = new Book();
book.setBookname(resultSet.getString("bookname"));
book.setPrice(resultSet.getFloat("price"));
// book.setAuthor(resultSet.getString("author"));
book.setPubDate(resultSet.getDate("pubdate"));
return book;
}
}
}
业务类接口 IBookService :
public interface IBookService {
List<Book> queryAll();
Book get(Integer bookid);
void save(Book book);
}
业务类 BookService :
@Service
public class BookService implements IBookService {
@Autowired
@Qualifier("bookDao")
private IBookDao bookDao;
@Override
public List<Book> queryAll() {
return bookDao.queryAll();
}
@Override
public Book get(Integer bookid) {
return bookDao.get(bookid);
}
@Override
public void save(Book book) {
bookDao.save(book);
}
}
4. 测试类
public class TestJdbc {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("sping.xml");
IBookService bookSerice = context.getBean("bookService", IBookService.class);
bookSerice.queryAll().forEach(System.out::println);
}
}
BeanPropertyRowMapper 用法
@Override
public Book get(Integer bookid) {
String sql = "select bookid ,bookname,pubdate,price,authorid from book where bookid=?";
// Book book = template.queryForObject(sql, Book.class,bookid);
Book book = template.queryForObject(sql, new BeanPropertyRowMapper<>(Book.class),bookid);
return book;
}
批量更新
public void batchSave(List<Book> list){
String sql = "insert into book (bookname,price,authorid,pubdate)values(?,?,?,?)";
List<Object[]> params = new ArrayList<>();
for (Book book : list) {
Object[] param = {book.getBookname(),book.getPrice(),2,new Date()};
params.add(param);
}
int[] ints = template.batchUpdate(sql, params);
System.out.println(Arrays.toString(ints));
}
c3p0 连接池
添加依赖
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
配置更换数据源
<!-- c3p0 连接 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/booksys"></property>
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="initialPoolSize" value="3"/>
</bean>
更换完成后,直接使用之前的测试类运行即可!
属性文件引用
配置引入文件
<context:property-placeholder location="db.properties"/>
修改数据源配置
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="initialPoolSize" value="3"/>
</bean>
上面问题的解决办法如下:
属性文件文件 db.properties 如下:
jdbc.url=jdbc:mysql://127.0.0.1:3306/booksys?serverTimezone=GMT
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=root
spring.xml 配置文件中使用 ${} 引用上面的键即可
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="initialPoolSize" value="3"/>
</b