1,单元测试
黑盒测试:
技术含量低,按照模板需求,输入参数,查看输出的结果是否满足范围,不需要编写代码
白盒测试:
根据某个功能,完成功能性测试,编写测试用例,写代码
操作步骤:
1)导包junit核心包以及hmrecrest-core 依赖包
2)测试某个功能
编写测试用例----编写一个单元测试方法
这个方法没有返回值类型,没有参数
3)需要标记这个方法为单元测试方法,需要在方法上面加入@Test注解
4)在junit包下一个断言Assert里面很多的方法来断言某个功能的结果是否和预期结果一致,如果一致,说明断言成功;否则失败!(如果测试自己的功能,不用断言,可以直接接口多态测试数据是否存在!)
eg:
@Test
public void testAdd(){
//创建计算类的对象
Clacuator cal = new Clacuator() ;
//调用求和功能
int result = cal.add(10, 20);
//使用Assert断言测试结果和预期结果是否一致
Assert.assertEquals(30,result);
}
/**
* 使用单元测试测试的查询所有
* @throws SQLException
*/
@Test
public void testFindAllEmp() throws SQLException {
//创建接口对象
EmployeeDao employeeDao = new EmployeeDaoImpl() ;
List<Employee> list = employeeDao.findAll();
if(list!=null){
for(Employee emp:list){
System.out.println(emp);
}
}
}
2,使用PrepareStatement预编译对象执行sql语句(DML/DQL)
DML语句:
eg:
public static void main(String[] args) throws SQLException {
//添加员工数据
Connection connnection = JdbcUtils.getConnnection();
//sql---参数化的sql语句
//sql语句中的值不是写死的,是在执行sql执行之前,可以多次赋值
String sql = "insert into emploee(name,age,gender,address,salary) values(?,?,?,?,?)" ;
//通过连接对象获取预编译对象PreparedStatement
//Connection---->PreparedStatement prepareStatement(String sql) throws SQLException
//将参数化的sql发送给数据库,并且存储到PreparedStatement对象中
PreparedStatement ps = connnection.prepareStatement(sql);
//PreparedStatement预编译对象中,需要给 ? (占位符号)进行赋值
//通用方法:void setXxx(int parameterIndex, Xxx x) :给占位符号赋值,
//参数1:第几个占位符号(从1开始)
//参数2:实际参数
ps.setString(1,"亓桑") ;
ps.setInt(2,20);
ps.setString(3,"男");
ps.setString(4,"西安市") ;
ps.setDouble(5,10000.00) ;
//PreparedStatement预编译对象 :执行sql
//通用方法:int executeUpdate() :DML语句
//ResultSet executeQuery() :DQL语句
int count = ps.executeUpdate();
System.out.println("影响了"+count+"行");
//释放资源
JdbcUtils.close(ps,connnection);
}
DQL语句:
eg:
public static void main(String[] args) throws SQLException {
//添加员工数据
Connection connnection = JdbcUtils.getConnnection();
//sql---参数化的sql语句
//sql语句中的值不是写死的,是在执行sql执行之前,可以多次赋值
String sql = "select * from emploee where id = ?" ;
//通过连接对象获取预编译对象PreparedStatement
//Connection---->PreparedStatement prepareStatement(String sql) throws SQLException
//将参数化的sql发送给数据库,并且存储到PreparedStatement对象中
PreparedStatement ps = connnection.prepareStatement(sql);
//给参数赋值
ps.setInt(1,7) ;
//执行查询
//ResultSet executeQuery() :DQL语句
ResultSet rs = ps.executeQuery();
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String address = rs.getString("address");
double salary = rs.getDouble("salary");
System.out.println(id+"\t"+name+"\t"+age+"\t"+gender+"\t"+address+"\t"+salary);
}
//释放资源
JdbcUtils.close(ps,connnection);
}
3,.使用Statement/PreparedStatement分别完成模拟登录功能
eg:
public class LoginTest {
public static void main(String[] args) {
//调用登录 操作
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
//提示并录入数据
System.out.println("请您输入用户名:") ;
String username = sc.nextLine() ; //录入字符串的方法
System.out.println("请您输入密码:") ;
String password = sc.nextLine() ;
boolean flag = isLogin(username,password) ;
if(flag){
System.out.println("恭喜您,登录成功!! ");
}else{
System.out.println("登录失败!");
}
}
/**
* 登录的功能
* @param username 用户名
* @param password 密码
* @return 返回值true,登录成功;否则失败
*/
public static boolean isLogin(String username,String password){
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
try {
//获取数据库的链接对象
conn = JdbcUtils.getConnnection() ;
//准备好sql
String sql = "select * from user where username = '"+username+"' and password = '"+password+"'" ;
System.out.println(sql) ;
//获取Statement对象
stmt = conn.createStatement();
//执行查询rs
rs = stmt.executeQuery(sql);
return rs.next() ;
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
JdbcUtils.close(rs,stmt,conn);
}
return false ;
}
}
4,Statement对象和PreparedStatement对象的区别
1)执行sql效率区别
Statement每一次需要将静态化的sql进行发送给数据库, 写好一条sql,发送一次,相对于PreparedStatement预编译对象来说,执行sql效率非常低!
PreparedStatement将编译参数化的sql(占位符号?)发送给数据库,数据库会校验它的列索引值以及对应的类型,将sql保存到预编译对象中
预编译对象可以赋值不同的参数,执行对应sql,执行效率相对Statement高很多!
2)是否会造成"SQL注入"
Statement对象发送的sql属于静态sql语句,存在"硬编码",而且还有字符串拼接,就会造成sql注入,非常不安全的一种行为!
PreparedStatement对象发送的sql都是参数化的sql,不存在字符串拼接,不会造成sql注入,相对Statement是一种安全行为!
注意事项:
后期框架(半成品的东西:一堆通用代码+一堆配置文件)底层对象性jdbc的封装使用的就是PreparedStatement
5,数据库的连接池
高并发情况下,同时很多人都需要去访问服务器("登录"操作),每次通过jdbc的方式,频繁的获取连接对象,完成用户的登录操作,在内存中需要不断的创建连接对象,耗费内存较大,可能造成内存溢出--引入连接池
连接池中加入threadlacal,每一个线程都有一个自己的Connection对象;
1)编写一个.properties文件将需要的属性编译进去:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day13
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000
2)编写工具类:
改进优化:
1)从DataSource获取连接对象getConnection(),DataSource替代了DriverManager (连接池获取连接对象)
2)读取的连接池的配置文件
3)提供静态代码块----加载当前类的时候,直接读取连接池的配置文件,
获取的连接池对象---DruidDataSourceFactroy工厂类获取数据源对象
eg:
public class DruidJdbcUtils {
//成员变量位置:提供ThreadLocal<Connection>:模拟线程,每一个线程使用自己的Connection
private static ThreadLocal<Connection> t1 = new ThreadLocal<>() ;
//声明一个DataSource类型的变量
private static DataSource ds ;
//无参构造私有化:目的外界不能new对象了
private DruidJdbcUtils(){}
//静态代码块
static{
try {
//创建属性集合列表
Properties prop = new Properties() ;
//直接读取连接池的配置文件
InputStream inputStream = DruidJdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
//将字节输入流的内容加载属性列表中
prop.load(inputStream) ;
//DruidDataSourceFactroy工厂类获取数据源对象
ds = DruidDataSourceFactory.createDataSource(prop);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//封装一个功能:获取数据源
public static DataSource getDataSource(){
return ds;
}
//封装一个功能:获取数据库的连接对象
public static Connection getConnection(){
//1)首先要从当前线程中获取连接对象
try {
Connection conn = t1.get();
//2)判断conn是空的
if(conn==null){
//当前线程中没有开始绑定连接对象
//3)从数据源连接池获取连接对象
conn = ds.getConnection() ;
//4)将当前连接对象绑定给自己的线程中
t1.set(conn);
}
return conn ;
} catch (SQLException e) {
e.printStackTrace();
}
return null ;
}
//封装释放资源
public static void close(PreparedStatement ps,Connection conn){
close(null,ps,conn);
}
public static void close(ResultSet rs, PreparedStatement ps,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
//需要将自己线程中的连接对象解绑
t1.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// DataSource dataSource = DruidJdbcUtils.getDataSource();
// System.out.println(dataSource);
//获取连接对象
Connection connection = DruidJdbcUtils.getConnection();
System.out.println(connection);
}
}
3)测试:
public class PreparedStatementDemo {
public static void main(String[] args) throws SQLException {
//获取连接对象
Connection connection = DruidJdbcUtils.getConnection();
//sql :参数化的sql
String sql = "insert into emploee(name,age,gender,address,salary) values(?,?,?,?,?)" ;
//获取预编译对象,发送sql
PreparedStatement ps = connection.prepareStatement(sql);
//赋值
ps.setString(1,"刘桑") ;
ps.setInt(2,20);
ps.setString(3,"男");
ps.setString(4,"南窑国际");
ps.setDouble(5,15000.00);
int i = ps.executeUpdate();
System.out.println(i);
DruidJdbcUtils.close(ps,connection);
}
}