spring-210728-03—JdbcTemplate操作数据库-查询功能
演示
UserDao02.java(dao层接口)
package com.bgy.spring.dao;
import com.bgy.spring.entity.User;
import java.util.List;
public interface UserDao02 {
int seletCount();
User finUserInfo(String id);
List<User> finUserAll();
}
UserDaoImpl02.java(这里实现DQL操作)
package com.bgy.spring.dao;
import com.bgy.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.List;
@Repository
public class UserDaoImpl02 implements UserDao02{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int seletCount() {
String sql = "select count(*) from t_user";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
@Override
public User finUserInfo(String id) {
String sql = "select * from t_user where user_id=?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
return user;
}
@Override
public List<User> finUserAll() {
String sql = "select * from t_user";
List<User> userList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
return userList;
}
}
UserService02.java(这里调用dao)
package com.bgy.spring.service;
import com.bgy.spring.dao.UserDao02;
import com.bgy.spring.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService02 {
@Autowired
private UserDao02 userDao02;
public int findCount(){
return userDao02.seletCount();
}
public User findUserInfo(String id){
return userDao02.finUserInfo(id);
}
public List<User> findUserAll(){
return userDao02.finUserAll();
}
}
TestUser02.java(测试)
import com.bgy.spring.entity.User;
import com.bgy.spring.service.UserService02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class TestUser02 {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
UserService02 userService02 = context.getBean("userService02", UserService02.class);
int count = userService02.findCount();
System.out.println(count);
}
@Test
public void test02(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
UserService02 userService02 = context.getBean("userService02", UserService02.class);
User userInfo = userService02.findUserInfo("1003");
System.out.println(userInfo);
System.out.println(userInfo.getUserId());
System.out.println(userInfo.getUsername());
System.out.println(userInfo.getUstatus());
}
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
UserService02 userService02 = context.getBean("userService02", UserService02.class);
List<User> userAll = userService02.findUserAll();
System.out.println(userAll);
}
}
bean01.xml(Spring配置文件)
<?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.bgy.spring"></context:component-scan>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring_test_210728_01?serverTimezone=UTC&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
User.java(实体类)
package com.bgy.spring.entity;
public class User {
private String userId;
private String username;
private String ustatus;
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;
}
public String getUstatus() {
return ustatus;
}
public void setUstatus(String ustatus) {
this.ustatus = ustatus;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", username='" + username + '\'' +
", ustatus='" + ustatus + '\'' +
'}';
}
}