全称为Data Access Object
,负责于数据库进行联络,完成增删查改的功能。提供接口给Service层。
调用Dao层提供的接口,业务处理,为Controller层提供接口。
负责请求转发,接收用户传来的参数,通过调用Service层提供的接口,实现将数据转发给Service层处理。接收Service的返回值,再转发给用户。
需求:
通过Spring框架,实现与数据库连接,在控制台输入用户名和密码,对用户进行校验、登陆、验证,成功返回“success”,失败返回“error”。
其中libs目录包含了spring框架所需要的jar依赖包和jdbc包
package com.yancy.entity;
public class User {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
package com.yancy.controller;
import com.yancy.entity.User;
import com.yancy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
@Autowired
UserService userService;
public String userLogin(User user) {
Boolean result = userService.userLogin(user);
if(result)
return "success";
else
return "erro";
}
}
package com.yancy.service;
import com.yancy.entity.User;
public interface UserService {
public boolean userLogin(User user);
}
package com.yancy.service.impl;
import com.yancy.dao.UserDao;
import com.yancy.entity.User;
import com.yancy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public boolean userLogin(User user) {
User sqlUser = userDao.userLogin(user);
if((sqlUser.getUserName().equals(user.getUserName())) && (sqlUser.getPassWord().equals( user.getPassWord()))) return true;
return false;
}
}
package com.yancy.dao;
import com.yancy.entity.User;
public interface UserDao {
public User userLogin(User user);
}
package com.yancy.dao.impl;
import com.yancy.dao.UserDao;
import com.yancy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public User userLogin(User user) {
String sql="select * from user where userName=?";
RowMapper<User> rowMapper=new BeanPropertyRowMapper(User.class);
User resUser= jdbcTemplate.queryForObject(sql,rowMapper,user.getUserName());
return resUser;
}
}
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver "/>
<property name="url" value="jdbc:mysql://localhost:3306/students?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="zy20011109"/>
bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<context:component-scan base-package="com.*"/>
beans>
import com.yancy.controller.UserController;
import com.yancy.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/* ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
System.out.println(jdbcTemplate);*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
UserController userController = (UserController) applicationContext.getBean("userController");
User user = new User();
Scanner in = new Scanner(System.in);
System.out.printf("请输入用户名:");
user.setUserName(in.next());
System.out.printf("请输入密码:");
user.setPassWord(in.next());
System.out.println(userController.userLogin(user));
}
}