详细整理分层开发步骤!
不点蓝字,我们哪来故事?
一、创建数据库,在数据库中创建表
二、创建java项目(起名要有意义,eg:petSys)
三、在项目中新建包,包名为:cn.bdqn.petSys.entity
四、在cn.bdqn.petSys.entity包中新建实体类,实体类是根据数据表而来,一个表对应一个实体类,数据库中的字段对应类中的属性
五、选中项目,在项目中新建一个文件夹,起名为lib,将sqlserver的jar文件复制到该目录下面,并且右击该jar文件,选择Build pathadd to build path.
六、新建包cn.bdqn.petSys.utils,在该包中新建一个BaseDao,用来连接数组库,BaseDao的代码如下:
public class BaseDao {
//连接数据库
public Connection getConn(){
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = petdb","sa","123456");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭连接
public void closeConn(Connection conn,PreparedStatement pre,ResultSet rs){
try {
if (rs!=null) {
rs.close();
}
if (pre!=null) {
pre.close();
}
if (conn!=null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
七、新建包cn.bdqn.petSys.dao,在包中新建接口,一个实体类对应一个接口,例如:ICatDao(起名规范:I+实体类名+Dao),在该接口中写需要实现的功能接口
增删改,返回类型为int.参数类型为实体,例如:int addCat(Cat cat);
根据编号查询,返回类型为实体,参数类型为int,例如:Cat findCatById(int cid);
查询全部,返回类型为泛型集合(List<Cat>),例如:List<Cat> findCatAll();
注意:除增删改外,返回类型取决于查询的结果集,如果返回1条以上的结果集,返回类型为List集合,如果返回1条结果集,返回类型为实体(Cat)
例如:
//根据编号查询
Cat findCatById(int cid);
//查询全部
List<Cat> findCatAll();
//根据名称模糊查询
List<Cat> findCatByName(String name);
//根据姓名和密码查询
Cat findCatByNameAndPass(String name,String pass);
八、新建包cn.bdqn.petSys.dao.impl,)(该包为dao层的实现类所在的包),在包内新建实现类,CatDaoImpl,该类需要实现dao层的ICatDao,代码如下:
public class CatDaoImpl implements ICatDao {
BaseDao base = new BaseDao();
Connection conn = base.getConn();
PreparedStatement pre = null;
ResultSet rs = null;
//添加
@Override
public int addCat(Cat cat) {
String sql = "insert into cat values(?,?,?,?,?);";
int rel = 0;
try {
pre = conn.prepareStatement(sql);
pre.setInt(1,cat.getCid());
pre.setString(2, cat.getCname());
pre.setString(3,cat.getCpass());
pre.setInt(4,cat.getClove());
pre.setString(5, cat.getCsex());
rel = pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return rel;
}
//修改
@Override
public int updateCat(Cat cat) {
String sql = "update cat set cname = ?,cpass = ?,clove = ? csex = ? where cid = ?";
int rel = 0;
try {
pre = conn.prepareStatement(sql);
pre.setString(1, cat.getCname());
pre.setString(2,cat.getCpass());
pre.setInt(3,cat.getClove());
pre.setString(4, cat.getCsex());
pre.setInt(5,cat.getCid());
rel = pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return rel;
}
//删除
@Override
public int delCat(Cat cat) {
String sql = "delete from cat where cid = ?";
int rel = 0;
try {
pre.setInt(1,cat.getCid());
rel = pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return rel;
}
//根据编号查询
@Override
public Cat findCatById(int cid) {
// TODO Auto-generated method stub
return null;
}
//查询全部
@Override
public List<Cat> findCatAll() {
return null;
}
//根据名称模糊查询
@Override
public List<Cat> findCatByName(String name) {
String sql = "select * from cat where cname like ?";
List<Cat> clist = new ArrayList<Cat>();
try {
pre = conn.prepareStatement(sql);
pre.setString(1, "%"+name+"%");
rs = pre.executeQuery();
while(rs.next()){
Cat cat = new Cat();
cat.setCid(rs.getInt(1));
cat.setCname(rs.getString(2));
cat.setCpass(rs.getString("cpass")); //或者3
cat.setCsex(rs.getString("csex"));
cat.setClove(rs.getInt("clove"));
clist.add(cat);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
base.closeConn(conn, pre, rs);
}
return clist;
}
//根据名称和密码查询
@Override
public Cat findCatByNameAndPass(String name, String pass) {
// TODO Auto-generated method stub
return null;
}
}
九、新建包cn.bdqn.petSys.service,在该包中新建接口,ICatService。此包为业务逻辑层的包,其中ICatService接口中的代码如下:
//添加
int addCat(Cat cat);
//修改
int updateCat(Cat cat);
//删除
int delCat(Cat cat);
//根据编号查询
Cat findCatById(int cid);
//查询全部
List<Cat> findCatAll();
//根据名称模糊查询
List<Cat> findCatByName(String name);
//根据姓名和密码查询
Cat findCatByNameAndPass(String name,String pass);
十、新建包cn.bdqn.petSys.Service.impl,(该包中的类主要是实现cn.bdqn.petSysService包中的接口),在该包中新建类CatServiceImpl,该类中的代码如下:
public class CatServiceImpl implements ICatService {
ICatDao catDao = new CatDaoImpl();
@Override
public int addCat(Cat cat) {
// TODO Auto-generated method stub
return catDao.addCat(cat);
}
@Override
public int updateCat(Cat cat) {
// TODO Auto-generated method stub
return catDao.updateCat(cat);
}
@Override
public int delCat(Cat cat) {
// TODO Auto-generated method stub
return catDao.delCat(cat);
}
@Override
public Cat findCatById(int cid) {
// TODO Auto-generated method stub
return catDao.findCatById(cid);
}
@Override
public List<Cat> findCatAll() {
// TODO Auto-generated method stub
return catDao.findCatAll();
}
@Override
public List<Cat> findCatByName(String name) {
// TODO Auto-generated method stub
return catDao.findCatByName(name);
}
@Override
public Cat findCatByNameAndPass(String name, String pass) {
// TODO Auto-generated method stub
return catDao.findCatByNameAndPass(name, pass);
}
}
十一、新建包cn.bdqn.petSys.test包,该包中放置的是测试类,代码如下:
public class TestCat {
static ICatService catService = new CatServiceImpl();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
findCatAll();
}
//添加猫
public static void addCat(){
System.out.println("请输入编号:");
int cid = sc.nextInt();
System.out.println("请输入名称:");
String cname = sc.next();
System.out.println("请输入密码:");
String cpass = sc.next();
System.out.println("请输入亲密度:");
int clove = sc.nextInt();
System.out.println("请输入性别:");
String csex = sc.next();
Cat cat = new Cat();
cat.setCid(cid);
cat.setCname(cname);
cat.setCpass(cpass);
cat.setClove(clove);
cat.setCsex(csex);
int rel = catService.addCat(cat);
if(rel>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
//查询全部
public static void findCatAll(){
List<Cat> clist = catService.findCatAll();
System.out.println("编号\t名称\t密码\t亲密度\t性别\t");
for(int i = 0;i<clist.size();i++){
System.out.print(clist.get(i).getCid()+"\t");
System.out.print(clist.get(i).getCname()+"\t");
System.out.print(clist.get(i).getCpass()+"\t");
System.out.print(clist.get(i).getClove()+"\t");
System.out.println(clist.get(i).getCsex());
}
}
}
往期精彩
点分享
点点赞
点在看