首先我們來看一下整個(gè)框架的代碼結(jié)構(gòu)
1,創(chuàng)建bean包:
這里是用來存儲(chǔ)一些實(shí)體類對(duì)象言蛇,可以保存實(shí)體的一些屬性數(shù)據(jù)僻他。
-
創(chuàng)建Customer類來保存一些顧客的信息
這里我只寫了顧客類的屬性
還需要寫它的get和set方法
還有重寫toString
還要實(shí)現(xiàn)序列化接口private Long id;
private String name;
private String gender;
private String telephone;
private String address;
2,創(chuàng)建dao包:
這里寫的是一些連接數(shù)據(jù)庫的增刪改查的類
-
創(chuàng)建類CustomerDao.java
這里面只要是寫數(shù)據(jù)庫的增刪改查
下面是它的一些方法
public void save(Customer customer){
//保存數(shù)據(jù)到數(shù)據(jù)庫中
}
public void update(Customer customer){ //更新數(shù)據(jù)庫中的數(shù)據(jù)
}
public Customer findByName(String name){//根據(jù)名字查找
}
public void delete(){//刪除數(shù)據(jù)庫中的數(shù)據(jù)
}
public List<Customer> findAll(){//查找所有的數(shù)據(jù)
}
在這些方法中比較復(fù)雜的方法就是findByName按照名字來查找宵距,我講解一下這個(gè)方法的實(shí)現(xiàn)。
- 我們可以發(fā)現(xiàn)當(dāng)進(jìn)行查詢數(shù)據(jù)的時(shí)候吨拗,大部分的代碼都類似满哪,可變的部分就是三個(gè)。
一劝篷,傳入的sql語句
二哨鸭,sql語句中傳入的參數(shù),數(shù)量不確定并且類型也不確定娇妓,所以要寫成Object類型的數(shù)組
public Customer findByName(String name){
Customer data=null;
String sql = "select * from ctmer where name=?";
Object[] obj={name};
data=(Customer) temp.findUnique(sql, obj, new Mapper());
return data;
}
三像鸡,就是返回的查詢結(jié)果,你可能需要一個(gè)數(shù)據(jù)也可能兩個(gè)數(shù)據(jù)哈恰,所以這里用到了內(nèi)部類來傳遞你想要的結(jié)果集
class Mapper implements Impper<Customer>{
@Override
public Customer map(ResultSet rs) {
Customer cust=new Customer();
try {
if(rs.next()){
long id = rs.getLong("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
String telephone = rs.getString("telephone");
String address = rs.getString("address");
cust=new Customer(id, name, gender, telephone, address);
System.out.println(id+"=="+name+"=="+gender+"=="+telephone+"=="+address);
}
} catch (Exception e) {
e.printStackTrace();
}
return cust;
}
}
3,創(chuàng)建service包
這里主要寫的是業(yè)務(wù)邏輯的代碼
-
創(chuàng)建接口 ICustomerService.java
下面是該接口中的方法
//注冊(cè)
void register(Customer customer) throws ServiceException;
//登陸
Customer login(String username,String passworld);
//更新信息
Customer updateInfo(Customer newCustomer);
-
創(chuàng)建該接口的實(shí)現(xiàn)類CustomerServiceImp.java
在這里調(diào)用dao層的方法實(shí)現(xiàn)數(shù)據(jù)的增刪改查只估。
下面是注冊(cè)邏輯的代碼實(shí)現(xiàn)
> @Override
public void register(Customer customer) throws ServiceException{
if(dao.findByName(customer.getName())==null){
dao.save(customer); //這里調(diào)用了dao層的保存數(shù)據(jù)到數(shù)據(jù)庫的方法
System.out.println("注冊(cè)成功");
}else{
throw new ServiceException("注冊(cè)失敗");
}
}
4,創(chuàng)建包c(diǎn)ommon
這里主要寫的就是一些封裝好的類可以直接調(diào)用
-
創(chuàng)建ConnectionFactory類
它是封裝了連接數(shù)據(jù)庫的類
1)靜態(tài)參數(shù)的配置
static{
driver="com.mysql.jdbc.Driver";
password="root";
user="root";
url="jdbc:mysql://127.0.0.1:3306/customer";
//從文件系統(tǒng)中獲取參數(shù),方便后期放在配置文件中志群,可以直接改為其他數(shù)據(jù)庫的參數(shù)
}
2)封裝的兩個(gè)靜態(tài)方法
第一個(gè)方法:其他數(shù)據(jù)庫也是這兩個(gè)部分必寫,只是傳遞進(jìn)來的參數(shù)不同蛔钙,因?yàn)橥獠啃枰玫絚onnection來進(jìn)行sql語句的執(zhí)行锌云,所以要返回出去
第二個(gè)方法:關(guān)閉數(shù)據(jù)庫的三個(gè)參數(shù)的鏈接,這是會(huì)多次調(diào)用的
public static Connection getConnection() throws Exception{
Class.forName(driver);
return DriverManager.getConnection(url, user, password);}
public static void close(ResultSet rs,PreparedStatement pstmt,Connection conn) throws Exception{}
創(chuàng)建類JDBCTemplate.java
這里封裝的是數(shù)據(jù)庫鏈接的代碼吁脱,因?yàn)樵赿ao層的方法中進(jìn)行增刪改查時(shí)桑涎,大量的代碼是類似的,只是傳遞的sql語句不同兼贡,查詢返回的結(jié)果不同攻冷,封裝后就可以進(jìn)行代碼的復(fù)用創(chuàng)建接口Impper.java
這個(gè)接口的目的是為了讓用戶可以傳遞用戶想要的結(jié)果集,畢竟你想查詢多少個(gè)屬性是不確定的紧显,屬性的類型也是不確定的讲衫,屬性的bean類也是不確定的就要用到泛型
public interface Impper<T> {
T map(ResultSet res);
}
6,最后還有一個(gè)異常類
這里主要是為了讓用戶在調(diào)用業(yè)務(wù)邏輯(ICustomerService)中代碼時(shí)會(huì)有一個(gè)提示孵班,比如用戶登陸成功時(shí)怎么處理涉兽,登陸失敗時(shí)怎么處理枷畏。
1)ServiceException繼承Exception類
2)ICustomerService接口中方法拋出異常ServiceException虱饿,因?yàn)橹挥薪涌趻伋隽水惓W宇惒趴梢話伋鲎约憾x的異常
3)實(shí)現(xiàn)類CustomerServiceImp.java在方法中拋出異常,并且在注冊(cè)失敗時(shí)渴肉,輸入失敗信息進(jìn)去
throw new ServiceException("注冊(cè)失敗");
測(cè)試:
Customer cust=new Customer();
cust.setData(3L,"fei","famel","12345678900","nanchang");
try {
customerService.register(cust);
} catch (ServiceException e) {
e.printStackTrace();
}