wordpress样板,超级优化大师下载,wordpress 主页 慢,c2c模式平台有哪些目录 JdbcTemplate#xff08;操作数据库-查询返回值#xff09;
1.创建数据库
2.创建实体类
3.创建dao层
4.创建service层
5.创建测试类#xff1a;
6.xml配置
7.测试结果#xff1a;
8.结构示意#xff1a; JdbcTemplate#xff08;操作数据库-查询返回值…目录 JdbcTemplate操作数据库-查询返回值
1.创建数据库
2.创建实体类
3.创建dao层
4.创建service层
5.创建测试类
6.xml配置
7.测试结果
8.结构示意 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对象Autowiredprivate JdbcTemplate jdbcTemplate;// 查询Overridepublic int selectCount() {String sqlselect 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 {//注入daoAutowiredprivate 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
{Testpublic void test01(){ApplicationContext contextnew ClassPathXmlApplicationContext(bean1.xml);BookService bookService context.getBean(bookService, BookService.class);// 查询返回某个值int count bookService.findCount();
// 做输出System.out.println(count);}}6.xml配置
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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-packageorg.example/context:component-scan
!--数据库连接池--bean iddataSource classcom.alibaba.druid.pool.DruidDataSource destroy-methodcloseproperty nameurl valuejdbc:mysql://localhost:3306/user_db?useSSLfalseamp;useUnicodetrueamp;characterEncodingUTF-8/property nameusername valueroot/property namepassword valuesise/property namedriverClassName valuecom.mysql.jdbc.Driver//bean!-- 创建jdbcTemplate对象--bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate
!--需要注入数据源信息--property namedataSource refdataSource/property/bean
/beans 7.测试结果 8.结构示意