目录
JdbcTemplate(操作数据库-查询返回值)
1.创建数据库
数据库中有3条记录,数据库名是user_db,数据库表是t_book
2.创建实体类
实体类属性对应数据库每一条记录
package org.example.spring.entity;
public class Book {
private String userId;
private String username;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
3.创建dao层
抽象类:
package org.example.spring.dao;
import org.example.spring.entity.Book;
public interface BookDao {
//查询的方法
int selectCount();
}
实现类:
package org.example.spring.dao;
import org.example.spring.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class BookDaoImpl implements BookDao{
//注入jdbcTemplate对象
@Autowired
private JdbcTemplate jdbcTemplate;
// 查询
@Override
public int selectCount() {
String sql="select count(*) from t_book";
// 此方法有两个参数
//一个参数是sql,一个参数是根据返回值类型而定,如果是String类型就返回String类型,是Int类型就返回Int类型
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
}
4.创建service层
package org.example.spring.service;
import org.example.spring.dao.BookDao;
import org.example.spring.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
//查询表中的记录数
public int findCount(){
return bookDao.selectCount();
}
}
5.创建测试类:
package org.example.spring.test;
import org.example.spring.entity.Book;
import org.example.spring.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBook
{
@Test
public void test01(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
// 查询返回某个值
int count = bookService.findCount();
// 做输出
System.out.println(count);
}
}
6.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启组件扫描-->
<context:component-scan base-package="org.example">
</context:component-scan>
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="sise"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<!-- 创建jdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--需要注入数据源信息-->
<property name="dataSource" ref="dataSource">
</property>
</bean>
</beans>
7.测试结果:
8.结构示意: