Spring-JDBCTemplate操作数据库(查询)
jdbcTemplate操作数据库(查询)
一、查询返回某个值
查询表里面有多少条记录,返回是某个值
queryForObject(String sql, Class<T> requiredType)
第一个参数:sql语句
第二个参数:返回类型Class
二、查询返回对象
queryForObject(String sql, RowMapper rowMapper, Object... args)
第一个参数:sql语句
第二个参数:RowMapper,是接口,返回不同类型数据,使用这个接口里面实现类完成数据封装
第三个参数:sql语句值
三、查询返回集合
query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
第一个参数:sql
第二个参数:RowMapper,是接口,返回不同类型数据,使用这个接口里面实现类完成数据封装
第三个参数:sql语句值
package com.orzjiangxiaoyu.spring.dao; import com.orzjiangxiaoyu.spring.entity.User; import java.util.List; /** * @author orz * @create 2020-08-18 9:36 */ public interface UserDao { public int findCount(); public User findUserInfo(int id); public List<User> findUserListInfo(); }
package com.orzjiangxiaoyu.spring.dao; import com.orzjiangxiaoyu.spring.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.Arrays; import java.util.List; /** * @author orz * @create 2020-08-18 9:37 */ @Repository public class UserDaoImpl implements UserDao { //注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; @Override public int findCount() { //1.创建sql语句 String sql = "select count(*) from t_user"; //2.调用方法实现 Integer count = jdbcTemplate.queryForObject(sql, Integer.class); return count; } @Override public User findUserInfo(int id) { //1.创建sql语句 String sql = "select * from t_user where user_id=?"; //2.调用方法实现 User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id); return user; } @Override public List<User> findUserListInfo() { //1.创建sql语句 String sql = "select * from t_user "; //2.调用方法实现 List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class)); return list; } }
package com.orzjiangxiaoyu.spring.service; import com.orzjiangxiaoyu.spring.dao.UserDao; import com.orzjiangxiaoyu.spring.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author orz * @create 2020-08-18 9:36 */ @Service public class UserService { //注入dao @Autowired private UserDao userDao; //查询表记录数 public int selectCount() { int i=userDao.findCount(); return i; } //查询返回对象 public User findUser(int id) { return userDao.findUserInfo(id); } //查询返回集合 public List<User> findUserList() { return userDao.findUserListInfo(); } }
package com.orzjiangxiaoyu.spring.mysqltestdemo; import com.orzjiangxiaoyu.spring.entity.User; import com.orzjiangxiaoyu.spring.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.ArrayList; import java.util.List; /** * @author orz * @create 2020-08-18 10:20 */ public class Test2 { //查询返回某个值 @Test public void test4() { ApplicationContext context=new ClassPathXmlApplicationContext("jdbcbean.xml"); UserService userService = context.getBean("userService", UserService.class); int i = userService.selectCount(); System.out.println(i); } //查询返回对象 @Test public void test5() { ApplicationContext context=new ClassPathXmlApplicationContext("jdbcbean.xml"); UserService userService = context.getBean("userService", UserService.class); User user = userService.findUser(1); System.out.println(user); } //查询返回集合 @Test public void test6() { ApplicationContext context=new ClassPathXmlApplicationContext("jdbcbean.xml"); UserService userService = context.getBean("userService", UserService.class); List<User> userList = userService.findUserList(); System.out.println(userList); } }