Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作
2018-01-01 00:42
561 查看
查询目标是完成3个功能:
(1)查询表,返回某一个值。例如查询表中记录的条数,返回一个int类型数据
(2)查询表,返回结果为某一个对象。
(3)查询表,返回结果为某一个泛型的list集合。
一、查询表中记录的条数,返回一个int类型数据的操作方法
使用jdbcTemplate
原理是把加载驱动Class.forName("com.mysql.jdbc.Driver");
和连接数据库Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root");
用一个对象完成DriverManagerDataSource dataSource=new DriverManagerDataSource();
查询数据库操作使用JdbcTemplate对象根据数据源直接使用qureyForObject方法完成,第二个参数Integer.class为返回值类型的class。
之前完成需按下边方法:
PreparedStatement ps=conn.prepareStatement("select count(*) from sw_user");
???ResultSet rs=ps.executeQuery();???有待验证
上边代码使用Spring框架注解生成对象方法
xml配置文件代码如下:
使用Servlet类进行测试:
运行结果图
(1)查询表,返回某一个值。例如查询表中记录的条数,返回一个int类型数据
(2)查询表,返回结果为某一个对象。
(3)查询表,返回结果为某一个泛型的list集合。
一、查询表中记录的条数,返回一个int类型数据的操作方法
使用jdbcTemplate
原理是把加载驱动Class.forName("com.mysql.jdbc.Driver");
和连接数据库Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root");
用一个对象完成DriverManagerDataSource dataSource=new DriverManagerDataSource();
package com.swift; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.stereotype.Component; @Component(value="jdbcTemplateDemo") public class JdbcTemplateDemo { public int queryCount() { DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database"); dataSource.setUsername("root"); dataSource.setPassword("root"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="select count(*) from sw_user"; int count=jdbcTemplate.qureyForObject(sql, Integer.class); return count; } public boolean update() { DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database"); dataSource.setUsername("root"); dataSource.setPassword("root"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="update sw_user set password=? where username=?"; int count=jdbcTemplate.update(sql,"lunchtime","doomsday"); if(count!=0) { return true; } return false; } }
查询数据库操作使用JdbcTemplate对象根据数据源直接使用qureyForObject方法完成,第二个参数Integer.class为返回值类型的class。
之前完成需按下边方法:
PreparedStatement ps=conn.prepareStatement("select count(*) from sw_user");
???ResultSet rs=ps.executeQuery();???有待验证
上边代码使用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" 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="com.swift"></context:component-scan> <!-- 开启aop注解方法 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
使用Servlet类进行测试:
package com.swift; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet("/test") public class ServletTest extends HttpServlet { private static final long serialVersionUID = 9d0e 1L; public ServletTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml"); JdbcTemplateDemo jdbcTemplateDemo=(JdbcTemplateDemo) context.getBean("jdbcTemplateDemo"); response.getWriter().append(jdbcTemplateDemo.queryCount()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
运行结果图
相关文章推荐
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为某一个对象。
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合
- Spring框架针对dao层的jdbcTemplate操作crud之update修改数据库操作
- Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载
- J:\java资料全套\基础+就业\Spring框架2016版视频\day03视频\08-jdbcTemplate实现crud操作(查询list集合).java
- Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载
- Spring框架学习第六讲(JDBCTemplate实现CRUD操作)
- jdbcTemplate实现crud操作(查询介绍)
- 利用反射实现对sqlite3数据库的crud(增删改查)操作的一个baseAndroidDao封装,安卓开发中
- jdbcTemplate queryForObject 查询 结果集 数量
- Hibernate学习-07:CRUD(增删改查)操作之查询多条/所有记录HQL、QBC、SQL三种方式
- Hdu 4918 Query on the subtree(一棵树,两种操作,一种是将某个点的权值修改为v,另一种是查询距离点u不超过d的点的权值和。)
- JAVA实现DAO基本层CRUD操作
- 编写DAO,通过JdbcTemplate操作数据库的实践
- Entity Framework 程序设计入门二 对数据进行CRUD操作和查询
- ArcGIS API for JavaScript 4.2学习笔记[20] 使用缓冲区结合Query对象进行地震点查询【重温异步操作思想】
- 使用Spring框架下的JdbcTemplate 完成对数据库的增删改查操作
- PRB:ASP 返回“Operation Must Use an Updateable Query”(操作必须使用可更新的查询)错误
- Spring的数据库操作---- Spring框架对JDBC的整合 ---- 初始化JdbcTemplate对象