Employee
package com.openlab.pojo;
//领域对象层
public class Employee {
private String id;
private String username;
private double salary;
private int age;
private String depart;
public Employee() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
@Override
public String toString() {
return "Employee [id=" + id + ", username=" + username + ", salary=" + salary + ", age=" + age + ", depart="
+ depart + "]";
}
}
工具类JDBCUtils
public List queryByAll(String sql){
List list = null;
try {
conn = getConnection();
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
list = rsToList(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jdbcClose(conn, st);
}
return list;
}
/**
* 单挑记录的查询 Object
*/
public Object queryByOne(String sql){
Object obj = null;
try {
conn = getConnection();
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
obj = rsToObj(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jdbcClose(conn, st);
}
return obj;
}
public abstract Object rsToObj(ResultSet rs);
public abstract List rsToList(ResultSet rs);
业务逻辑层 Service
package com.openlab.service;
// 业务层接口
import java.util.List;
import com.openlab.pojo.Employee;
public interface EmployeeService {
public Employee queryById(String id);
public List queryByUserName(String username);
}
数据链路层Dao
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//数据链路层
import com.openlab.pojo.Employee;
import com.openlab.utils.JDBCUtils;
public class EmployeeServcieDao extends JDBCUtils{
@Override
public Object rsToObj(ResultSet rs) {
Object obj = null;
try {
Employee emp = new Employee();
if(rs.next()){
emp.setId(rs.getString("id"));
emp.setUsername(rs.getString("username"));
emp.setSalary(rs.getDouble("salary"));
emp.setAge(rs.getInt("age"));
emp.setDepart(rs.getString("depart"));
obj = emp;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
@Override
public List rsToList(ResultSet rs) {
List list = new ArrayList();
try {
Employee emp = new Employee();
while(rs.next()){
emp.setId(rs.getString("id"));
emp.setUsername(rs.getString("username"));
emp.setSalary(rs.getDouble("salary"));
emp.setAge(rs.getInt("age"));
emp.setDepart(rs.getString("depart"));
list.add(emp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
业务层IMPL 具体业务逻辑的实现
package com.openlab.service.impl;
import java.util.List;
import com.openlab.dao.EmployeeServcieDao;
import com.openlab.pojo.Employee;
import com.openlab.service.EmployeeService;
public class EmployeeServiceImpl implements EmployeeService{
EmployeeServcieDao edao = new EmployeeServcieDao();
@Override
public Employee queryById(String id) {
String sql = "select * from tb7 where id='"+id+"'";
Object obj = edao.queryByOne(sql);
Employee em =(Employee) obj;
return em;
}
@Override
public List queryByUserName(String username) {
String sql ="select * from tb7 where username='"+username+"'";
List list = edao.queryByAll(sql);
return list;
}
}
测试类
package com.openlab.test;
import java.util.List;
import org.junit.Test;
import com.openlab.pojo.Employee;
import com.openlab.service.impl.EmployeeServiceImpl;
import junit.framework.TestCase;
public class EmployeeServiceImplTest {
EmployeeServiceImpl eimpl = new EmployeeServiceImpl();
@Test
public void testQueryById() {
Employee e = eimpl.queryById("006");
System.out.println(e);
TestCase.assertEquals("qiweifeng", e.getUsername());
}
@Test
public void testQueryByUserName(){
List list = eimpl.queryByUserName("qiweifeng");
System.out.println(list);
TestCase.assertEquals(2, list.size());
}
}
PrepareStatement 先由PrepareStatement 和数据库连接 对字符串的预处理,处理结束以后再去执行SQL ,可以防止SQL注入
package com.openlab.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class preJDBCTest {
public static void main(String[] args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
Connection conn = JDBCUtils.getConnection();
String sql = "select * from tb7 where username=?";
ps = conn.prepareStatement(sql);
ps.setString(1, "' or 1 or '");
rs = ps.executeQuery();
while(rs.next()){
System.out.println(
rs.getString("id")+"\t"+
rs.getString("username")+"\t"+
rs.getDouble("salary")+"\t"+
rs.getInt("age")+"\t"+
rs.getString("depart"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
面向对象是一种思想,是基于面向过程而言的,就是说面向对象是将功能等通过对象来实现,将功能封装进对象之中,让对象去实现具体的细节;这种思想是将数据作为第一位,而方法或者说是算法作为其次,这是对数据一种优化,操作起来更加的方便,简化了过程。面向对象有三大特征:封装性、继承性、多态性,其中封装性指的是隐藏了对象的属性和实现细节,仅对外提供公共的访问方式,这样就隔离了具体的变化,便于使用,提高了复用性和安全性。对于继承性,就是两种事物间存在着一定的所属关系,那么继承的类就可以从被继承的类中获得一些属性和方法;这就提高了代码的复用性。继承是作为多态的前提的。多态是说父类或接口的引用指向了子类对象,这就提高了程序的扩展性,也就是说只要实现或继承了同一个接口或类,那么就可以使用父类中相应的方法,提高程序扩展性,但是多态有一点不好之处在于:父类引用不能访问子类中的成员的特有方法和属性。 |
所谓封装,就是将属性和方法捆绑到一起,封装到一个对象中zhi去,简单的说,你是人,你有许多属性,比如说dao你的姓名,年龄,身高,体重,性别,性格,爱好等等,这是属性;而同时,你又会吃饭,睡觉,工作,学习,做事情,这些是方法,是你所具有的;同时将属性和方法封装到一个类中去,就能很完美的描述这个类的特征了,同时,它所具有的方法也就一起集成到类中,方便使用,这些是简单的描述了。官方一点说就是:封装是将事物的一些不便暴露给外界的一些属性隐藏起来.同时给外界提供一些接口,用来和外界进行交互,建立连接关系。 |
封装的优点:(1)隐藏类的实现细节;(2)让使用者只能通过事先定制好的方法来访问数据,可以方便地加入控制 逻辑,限制对属性的不合理操作;(3)便于修改,增强代码的可维护性;