JAVA 3层架构及其实例文件/代码规范( 二 )


数据访问层 dao 接口
package com.teaching.jsp.dao;import com.teaching.jsp.entity.Brand;import java.util.List;//DAO 的全称是Data Access Objectpublic interface BrandDao {List selectAll();boolean insert(Brand brand);Brand selectBrandById(String id);boolean updateBrand(Brand brand);}
dao 操作
对数据库的连接不应该写在此层 , 应该写在层
package com.teaching.web.dao;import com.teaching.web.entity.User;import com.teaching.web.utils.DbUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class UserDaoImpl implements UserDao {@Overridepublic User selectUserByEntity(User user, Connection connection) throws SQLException {String sql="select id,username,password from user where username=? and password=?";User u = null;PreparedStatement ps = connection.prepareStatement(sql);ps.setString(1, user.getUsername());ps.setString(2, user.getPassword());ResultSet rs = ps.executeQuery();if (rs.next()){u =new User(rs.getInt(1),rs.getString(2),rs.getString(3));}return u;}@Overridepublic int insertUser(User user, Connection connection) throws SQLException {String sql="insert into user values (null,?,?)";PreparedStatement ps = connection.prepareStatement(sql);ps.setString(1, user.getUsername());ps.setString(2, user.getPassword());int i = ps.executeUpdate();DbUtils.close(ps,null,null);return i;}@Overridepublic int selectUserByUsername(String username, Connection connection) throws SQLException {PreparedStatement ps=null;ResultSet resultSet=null;int anInt=0;try {String sql="select count(*) from user where username=?";ps = connection.prepareStatement(sql);ps.setString(1,username);resultSet = ps.executeQuery();if (resultSet.next()){anInt = resultSet.getInt(1);}} finally {DbUtils.close(ps,resultSet,null);}return anInt;}}
package com.teaching.jsp.entity;public class Brand {// id 主键private Integer id;// 品牌名称private String brandName;// 企业名称private String companyName;// 排序字段private Integer ordered;// 描述信息private String description;// 状态:0:禁用1:启用private Integer status;public Brand() {}public Brand(Integer id, String brandName, String companyName, String description) {this.id = id;this.brandName = brandName;this.companyName = companyName;this.description = description;}public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {this.id = id;this.brandName = brandName;this.companyName = companyName;this.ordered = ordered;this.description = description;this.status = status;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBrandName() {return brandName;}public void setBrandName(String brandName) {this.brandName = brandName;}public String getCompanyName() {return companyName;}public void setCompanyName(String companyName) {this.companyName = companyName;}public Integer getOrdered() {return ordered;}public void setOrdered(Integer ordered) {this.ordered = ordered;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}@Overridepublic String toString() {return "Brand{" +"id=" + id +", brandName='" + brandName + '\'' +", companyName='" + companyName + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}}
package com.teaching.jsp.utils;import com.alibaba.druid.pool.DruidDataSourceFactory;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import javax.sql.DataSource;import java.io.FileInputStream;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;public class DbUtils {private static DataSource dataSource;static {Properties prop=new Properties();try {prop.load(new FileInputStream("F:\\JAVA EE Preject\\课堂练习\\src\\com\\teaching\\jsp\\db.properties"));System.out.println("iiii");dataSource = DruidDataSourceFactory.createDataSource(prop);} catch (IOException e) {throw new RuntimeException(e);} catch (Exception e) {throw new RuntimeException(e);}}public static void test(){System.out.println(DbUtils.class.getClassLoader().getResource("db.properties"));}public static Connection getConnection() throws SQLException {System.out.println("iiii111");return dataSource.getConnection();}public static void close(Connection connection) throws SQLException {if (connection!=null){connection.close();}}public static void close(PreparedStatement ps, ResultSet rs,Connection connection) throws SQLException {if (rs!=null){rs.close();}if (ps!=null){ps.close();}if (connection!=null){connection.close();}}}