Discuss / Java / 在上述示例的基础上,继续给UserService注入DataSource,并把注册和登录功能通过数据库实现。

在上述示例的基础上,继续给UserService注入DataSource,并把注册和登录功能通过数据库实现。

Topic source

净净一隅

#1 Created at ... [Delete] [Delete and Lock User]

1. application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userService" class="com.service.UserService">
<property name="mailService" ref="mailService" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="mailService" class="com.service.MailService" />

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/learnjdbc?useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="maximumPoolSize" value="10" />
<property name="connectionTimeout" value="10000" />
<property name="idleTimeout" value="60000" />
<property name="autoCommit" value="true" />
</bean>
</beans>

2. UserService 

package com.service;

import com.entity.User;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserService {
private MailService mailService;
private DataSource dataSource;

public void setMailService(MailService mailService) {
this.mailService = mailService;
}

public User getUserFromDB(String email,String password) {
try(Connection conn=this.dataSource.getConnection()){
String sql="select id,name from user where email=? and password=? limit 1";
System.out.println(sql);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, email);
ps.setObject(2, password);
try(ResultSet rs=ps.executeQuery()){
while (rs.next()) {
long id = rs.getLong("id");
String name = rs.getString("name");
return new User(id,email,password,name);
}
}
}catch (SQLException e){
e.printStackTrace();
return null;
}catch (Exception e){
e.printStackTrace();
return null;
}
return null;
}

public User getUserFromDB(String email) throws SQLException {
try(Connection conn=this.dataSource.getConnection()){
String sql="select id,email,name from user where email=?";
System.out.println(sql);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, email);
try(ResultSet rs=ps.executeQuery()){
while (rs.next()) {
long id = rs.getLong("id");
String name = rs.getString("name");
User user=new User();
user.setName(name);
user.setId(id);
user.setEmail(email);
return user;
}
}
}
return null;
}

public User login(String email, String password) {
User user=getUserFromDB(email,password);
if(user!=null){
mailService.sendLoginMail(user);
return user;
}
throw new RuntimeException("login failed.");
}

public User registerUser2DB(User user) throws SQLException{
try(Connection conn=this.dataSource.getConnection()) {
String sql="insert into user(name,email,password) values(?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, user.getName());
ps.setObject(2,user.getEmail());
ps.setObject(3,user.getPassword());
int rs=ps.executeUpdate();
if(rs>0){
return user;
}
}
return null;
}

public User register(String email, String password, String name) throws SQLException{
if(getUserFromDB(email)!=null){
throw new RuntimeException("email exist.");
}
User user=new User();
user.setEmail(email);
user.setName(name);
user.setPassword(password);
this.registerUser2DB(user);
mailService.sendRegistrationMail(user);
return user;
}

public DataSource getDataSource() {
return dataSource;
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}


  • 1

Reply