D05 Sping Boot 入门 Sping框架--Java Web之书城项目(一)
项目搭建目录结构:
开发步骤
1、先创建书城需要的数据库和表(基于MySQL和Navicat)。
2、编写数据库表对应的JavaBean对象
在com.gychen.pojo里新建User类,如下:
1 package com.gychen.pojo; 2 3 public class User { 4 private Integer id; 5 private String username; 6 private String password; 7 private String email; 8 9 public Integer getId() { 10 return id; 11 } 12 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 17 public String getUsername() { 18 return username; 19 } 20 21 public void setUsername(String username) { 22 this.username = username; 23 } 24 25 public String getPassword() { 26 return password; 27 } 28 29 public void setPassword(String password) { 30 this.password = password; 31 } 32 33 public String getEmail() { 34 return email; 35 } 36 37 public void setEmail(String email) { 38 this.email = email; 39 } 40 41 42 @Override 43 public String toString() { 44 return "User{" + 45 "id=" + id + 46 ", username='" + username + '\'' + 47 ", password='" + password + '\'' + 48 ", email='" + email + '\'' + 49 '}'; 50 } 51 52 53 public User() { 54 55 } 56 57 public User(Integer id, String username, String password, String email) { 58 this.id = id; 59 this.username = username; 60 this.password = password; 61 this.email = email; 62 } 63 }
3、编写 JdbcUtils 工具类
在com.gychen.utils里新建JdbcUtils类
1 package com.gychen.utils; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import com.alibaba.druid.pool.DruidDataSourceFactory; 5 6 import java.io.InputStream; 7 import java.sql.Connection; 8 import java.sql.SQLException; 9 import java.util.Properties; 10 11 public class JdbcUtils { 12 13 private static DruidDataSource dataSource; 14 15 static { 16 17 try { 18 Properties properties = new Properties(); 19 //读取jdbc.properties配置文件属性 20 InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); 21 //从流中加载数据 22 properties.load(inputStream); 23 //创建 数据库 连接池 24 dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); 25 // System.out.println(dataSource.getConnection()); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 30 } 31 32 33 /** 34 * 获取数据库连接池中的连接 35 * @return 如果返回null 说明获取连接失败<br/> 有值就是获取连接成功 36 */ 37 public static Connection getConnection(){ 38 Connection conn = null; 39 try { 40 conn = dataSource.getConnection(); 41 } catch (SQLException e) { 42 e.printStackTrace(); 43 } 44 return conn; 45 } 46 47 /** 48 * 关闭连接,放回数据库连接池 49 * @param conn 50 */ 51 public static void close(Connection conn){ 52 if(conn != null){ 53 try { 54 conn.close(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 } 59 } 60 61 public static void main(String[] args) { 62 63 } 64 65 }
在com.gychen.test中新建JdbcUtilsTest类编写单元测试
4、编写 BaseDao
在com.gychen.dao.impl里新建BaseDao类
1 package com.gychen.dao.impl; 2 3 import com.gychen.utils.JdbcUtils; 4 import org.apache.commons.dbutils.QueryRunner; 5 import org.apache.commons.dbutils.handlers.BeanHandler; 6 import org.apache.commons.dbutils.handlers.BeanListHandler; 7 import org.apache.commons.dbutils.handlers.ScalarHandler; 8 9 import java.sql.Connection; 10 import java.sql.SQLException; 11 import java.util.List; 12 13 //复用类使用抽象abstract类 14 public abstract class BaseDao { 15 16 //使用DbUtils操作数据库 17 private QueryRunner queryRunner = new QueryRunner(); 18 19 /** 20 * update()方法用来执行Insert、Update、Delete语句 21 * @return 如果返回-1,说明执行失败<br/>返回其他表示影响的行数 22 */ 23 public int update(String sql, Object ...args){ 24 Connection connection = JdbcUtils.getConnection(); 25 try { 26 return queryRunner.update(connection,sql,args); 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 }finally { 30 JdbcUtils.close(connection); 31 } 32 return -1; 33 } 34 35 /** 36 * 查询返回一个JavaBean的sql语句 37 * @param type 返回的对象类型 38 * @param sql 执行的sql语句 39 * @param args sql对应的参数值 40 * @param <T> 返回的类型的泛型 41 * @return 42 */ 43 public <T> Object queryForOne(Class<T> type,String sql,Object...args){ 44 Connection connection = JdbcUtils.getConnection(); 45 try { 46 return queryRunner.query(connection,sql,new BeanHandler<T>(type),args); 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 }finally { 50 JdbcUtils.close(connection); 51 } 52 return null; 53 } 54 55 /** 56 * 查询返回多个JavaBean的sql语句 57 * @param type 返回的对象类型 58 * @param sql 执行的sql语句 59 * @param args sql对应的参数值 60 * @param <T> 返回的类型的泛型 61 * @return 62 */ 63 public <T> List<T> queryForList(Class<T> type, String sql, Object...args){ 64 Connection connection = JdbcUtils.getConnection(); 65 try { 66 return queryRunner.query(connection,sql,new BeanListHandler<T>(type),args); 67 } catch (SQLException e) { 68 e.printStackTrace(); 69 }finally { 70 JdbcUtils.close(connection); 71 } 72 return null; 73 } 74 75 /** 76 * 执行返回一行一列的sql语句 77 * @param sql 执行的sql语句 78 * @param args sql对应的参数值 79 * @return 80 */ 81 public Object queryForSingleValue(String sql,Object...args){ 82 Connection connection = JdbcUtils.getConnection(); 83 try { 84 return queryRunner.query(connection,sql,new ScalarHandler(),args); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 }finally { 88 JdbcUtils.close(connection); 89 } 90 return null; 91 } 92 }
5、编写 UserDao 和测试
Ⅰ、在com.gychen.dao.impl中新建UserDao接口类并移动到com.gychen.dao包中(删除首行的.impl并Alt+Enter)(快速生成测试类:在需要测试的类中按Ctr+Shift+t)
1 package com.gychen.dao; 2 3 import com.gychen.pojo.User; 4 5 public interface UserDao { 6 7 /** 8 * 根据用户名查询用户信息 9 * @param username 用户名 10 * @return 如果返回null,说明没有该用户 11 */ 12 public User queryUserByUsername(String username); 13 14 /** 15 * 根据用户名和密码查询用户信息 16 * @param username 17 * @param password 18 * @return 如果返回null,说明用户名或密码错误 19 */ 20 public User queryUserByUsernameAndPassword(String username,String password); 21 22 /** 23 * 保存用户信息 24 * @param user 25 * @return 26 */ 27 public int saveUser(User user); 28 }
Ⅱ、在com.gychen.dao.impl里新建UserDaoImpl类,写sql语句
1 package com.gychen.dao.impl; 2 3 import com.gychen.dao.UserDao; 4 import com.gychen.pojo.User; 5 6 //继承BaseDao并且实现UserDao接口 7 public class UserDaoImpl extends BaseDao implements UserDao { 8 9 @Override 10 public User queryUserByUsername(String username) { 11 String sql = "select id,username,password,email from t_user where username = ?"; 12 13 return (User) queryForOne(User.class,sql,username); 14 } 15 16 @Override 17 public User queryUserByUsernameAndPassword(String username, String password) { 18 String sql = "select id,username,password,email from t_user where username = ? and password = ?"; 19 20 return (User) queryForOne(User.class,sql,username,password); 21 } 22 23 @Override 24 public int saveUser(User user) { 25 String sql = "insert into t_user(username,password,email)values(?,?,?)"; 26 return update(sql,user.getUsername(),user.getPassword(),user.getEmail()); 27 } 28 }
Ⅲ、编写测试类(快速生成测试类:在UserDaoImpl类中按Ctr+Shift+t)(查看方法的注释按住Ctrl+Q)
1 package com.gychen.test; 2 3 import com.gychen.dao.UserDao; 4 import com.gychen.dao.impl.UserDaoImpl; 5 import com.gychen.pojo.User; 6 import org.junit.Test; 7 8 import static org.junit.Assert.*; 9 10 public class UserDaoImplTest { 11 UserDao userDao = new UserDaoImpl(); 12 13 @Test 14 public void queryUserByUsername() { 15 16 // System.out.println(userDao.queryUserByUsername("admin")); 17 if(userDao.queryUserByUsername("admin") == null){ 18 System.out.println("用户名可用"); 19 }else{ 20 System.out.println("用户名已存在"); 21 } 22 } 23 24 @Test 25 public void queryUserByUsernameAndPassword() { 26 // UserDao userDao = new UserDaoImpl(); 27 // System.out.println(userDao.queryUserByUsernameAndPassword("admin","admin")); 28 if(userDao.queryUserByUsernameAndPassword("admin","admin") == null){ 29 System.out.println("用户名或密码错误,登录失败"); 30 }else{ 31 System.out.println("用户名和密码正确"); 32 } 33 } 34 35 @Test 36 public void saveUser() { 37 System.out.println(userDao.saveUser(new User(null,"test01","test01","test01@qq.com"))); 38 } 39 }
6、编写UserService 和测试
Ⅰ、在com.gychen.service.impl中新建UserService接口类并移动到com.gychen.service包中
1 package com.gychen.service; 2 3 import com.gychen.pojo.User; 4 5 public interface UserService { 6 7 /** 8 * 注册用户 9 * @param user 用户信息 10 */ 11 public void registUser(User user); 12 13 /** 14 * 登录 15 * @param user 16 * @return 17 */ 18 public User login(User user); 19 20 /** 21 * 检查用户名是否可用 22 * @param username 23 * @return 返回true表示用户名已存在<br/>返回false表示用户名可用 24 */ 25 public boolean existUsername(String username); 26 27 }
Ⅱ、在com.gychen.service.impl里新建UserServiceImpl类
1 package com.gychen.service.impl; 2 3 import com.gychen.dao.UserDao; 4 import com.gychen.dao.impl.UserDaoImpl; 5 import com.gychen.pojo.User; 6 import com.gychen.service.UserService; 7 8 public class UserServiceImpl implements UserService { 9 10 private UserDao userDao = new UserDaoImpl(); 11 12 @Override 13 public void registUser(User user) { 14 userDao.saveUser(user); 15 } 16 17 @Override 18 public User login(User user) { 19 return userDao.queryUserByUsernameAndPassword(user.getUsername(),user.getPassword()); 20 } 21 22 @Override 23 public boolean existUsername(String username) { 24 if(userDao.queryUserByUsername(username) == null){ 25 //等于null说明没查到,说明用户名可用 26 return false; 27 } 28 return true; 29 } 30 }
Ⅲ、编写测试类(快速生成测试类:在UserServiceImpl类中按Ctr+Shift+t)(查看方法的注释按住Ctrl+Q)
1 package com.gychen.test; 2 3 import com.gychen.pojo.User; 4 import com.gychen.service.UserService; 5 import com.gychen.service.impl.UserServiceImpl; 6 import org.junit.Test; 7 8 import static org.junit.Assert.*; 9 10 public class UserServiceTest { 11 12 13 UserService userService = new UserServiceImpl(); 14 15 @Test 16 public void registUser() { 17 userService.registUser(new User(null,"test02","test02","test02@qq.com")); 18 userService.registUser(new User(null,"test03","test03","test03@qq.com")); 19 } 20 21 @Test 22 public void login() { 23 System.out.println(userService.login(new User(null,"test02","test02",null))); 24 } 25 26 @Test 27 public void existUsername() { 28 // System.out.println(userService.existUsername("test04")); 29 if(userService.existUsername("test04")){ 30 System.out.println("用户名已存在"); 31 }else{ 32 System.out.println("用户名可用"); 33 } 34 } 35 }