通过单例设计模式使驱动加载时只加载一次
public class ConFigUtils2 {
//私有的属性
private static ConFigUtils2 conFigUtils;
//用来读取properties为后缀的文件
private static Properties pro;
//私有的构造方法
private ConFigUtils2(){
//在构造方法里读取配置文件的信息
try {
//获取文件地址
String fileName="database2.properties";
pro =new Properties();
//把文件的信息读取到输入流里
InputStream is= ConFigUtils2.class.getClassLoader().getResourceAsStream(fileName);
//把读取到流的信息放入到Prooerties对象里
pro.load(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//对外的接口
//用双重锁来保证多线程的安全
public static synchronized ConFigUtils2 singleTonConfigUtils(){
if(conFigUtils==null){
synchronized (ConFigUtils2.class) {
if(conFigUtils ==null){
conFigUtils =new ConFigUtils2();
}
}
}
return conFigUtils;
}
//通过建来获取值
public String getValues(String key){
return pro.getProperty(key);
}
}
然后像之前一样,创建BaseDao层连接驱动,但该类只要获取的方法获取连接对象;
接下来是创建DaoImpl类实现具体的增删查改方法;
该次增加了新语句,私有化 QueryRunner 对象,用于实现替换占位符,返回输出等功能。
public class PetDaoImpl implements PetDao {
private QueryRunner qr=new QueryRunner();
public List<Pet> SelectByName(String name) {
String sql="select * from pet where name=?";
Object[] object={name};
List<Pet>ulist =null;
try {
ulist=(List<Pet>) qr.query(BaseDao2.getConnection(), sql, name,new BeanHandler<Pet>(Pet.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ulist;
}
public List<Pet> SelectByPage(Integer currentpageNo,Integer pageSize) {
String sql="select * from `pet` limit ?,?";
Object[] object={(currentpageNo-1)*pageSize,pageSize};
List<Pet> olist=null;
try {
olist=qr.query(BaseDao2.getConnection(),sql,object,new BeanListHandler<Pet>(Pet.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return olist;
}
public Long selectCount() {
String sql="select count(*) from `pet`";
Long count=null;
try {
count=(Long)qr.query(BaseDao2.getConnection(), sql, new ScalarHandler());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public List<Pet> selectByNamePage(Integer currentpageNo, Integer pageSize,String name) {
String sql="select * from `pet`where name like ? limit ?,?";
Object[] object={"%"+name+"%",(currentpageNo-1)*pageSize,pageSize};
List<Pet> olist=null;
try {
olist=qr.query(BaseDao2.getConnection(),sql,object,new BeanListHandler<Pet>(Pet.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return olist;
}
public int addUpdate(Pet pet) {
String sql="insert into `pet`(name,health,love,strain)values(?,?,?,?)";
int num=0;
try {
num=qr.update(BaseDao2.getConnection(),
sql,pet.getName(),pet.getHealth(),pet.getLove(),pet.getStrain() );
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
public int deletDate(Pet pet){
String sql="delete from `pet` where id=?";
int num=0;
try {
num=qr.update(BaseDao2.getConnection(),sql,pet.getId());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
public int updatePet(Pet pet) {
String sql="update `pet` set name=?,health=?,love=?,strain=? where id=?";
int num=0;
try {
num=qr.update(BaseDao2.getConnection(),sql,pet.getName(),pet.getHealth(),pet.getLove(),pet.getStrain(),pet.getId());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
}
创建service层,用于未来复杂的逻辑判断操作,在该阶段暂时用不上。
public interface PetService {
List<Pet> SelectByName(String name);
// 分页查询
List<Pet> SelectByPage(Integer currentPageNo,Integer pageSize);
//总记录数
Long selectCount();
//模糊查询加分页
List<Pet> selectByNamePage(Integer currentpageNo,Integer pageSize,String name);
//增加
int addUpdate(Pet pet);
//删
int deletDate(Pet pet);
//change
int updatePet(Pet pet);
}
import java.util.List;
import com.offcn.dao.PetDao;
import com.offcn.dao.impl.PetDaoImpl;
import com.offcn.entity.Pet;
import com.offcn.service.PetService;
public class PetServiceImpl implements PetService{
private PetDao petDao= new PetDaoImpl();
public List<Pet> SelectByName(String name) {
return petDao.SelectByName(name);
}
public List<Pet> SelectByPage(Integer currentPageNo, Integer pageSize) {
return petDao.SelectByPage(currentPageNo, pageSize);
}
public Long selectCount() {
return petDao.selectCount();
}
public List<Pet> selectByNamePage(Integer currentpageNo, Integer pageSize,String name) {
return petDao.selectByNamePage(currentpageNo, pageSize, name);
}
public int addUpdate(Pet pet) {
return petDao.addUpdate(pet);
}
public int deletDate(Pet pet) {
return petDao.deletDate(pet);
}
public int updatePet(Pet pet) {
return petDao.updatePet(pet);
}
}
事务:指逻辑上的一组操作。
事务的四大特性:
1.原子性:最小的单位,不可以被分割,事务中的操作要么都发生,要么都不发生。
2.一致性:(要么是执行前的结果,要么是执行后的结果)
3.隔离性:各个事务都互不干扰
4.持久性:
指一个事务一旦被提交,它对数据库的改变就是永久性的。
在多线程中处理数据要实现mysql的数据隔离:
出现的问题会有
1.脏读
也就是读取到另一个事务未提交的数据
2.不可重读读
也就是一个事务读取同一行数据,产生的结果不一致。
3.虚读/幻读
在一个事务里面读取到了另一个事务的插入的数据,导致前后读取的结果不一致。
事务的隔离级别
设置事务的隔离级别能够防止以上情况出现。数据库共定义了4种隔离级别。
◆未授权读取(Read Uncommitted):允许脏读取,但不允许更新丢失。如果一个事务已经开始写数据,则另外一个数据则不允许同时进行写操作,但允许其他事务读此行数据。该隔离级别可以通过“排他写锁”实现。
◆授权读取(Read Committed):允许不可重复读取,但不允许脏读取。这可以通过“瞬间共享读锁”和“排他写锁”实现。读取数据的事务允许其他事务继续访问该行数据,但是未提交的写事务将会禁止其他事务访问该行。
◆可重复读取(Repeatable Read):禁止不可重复读取和脏读取,但是有时可能出现幻影数据。这可以通过“共享读锁”和“排他写锁”实现。读取数据的事务将会禁止写事务(但允许读事务),写事务则禁止任何其他事务。
◆序列化(Serializable):提供严格的事务隔离。它要求事务序列化执行,事务只能一个接着一个地执行,但不能并发执行。如果仅仅通过“行级锁”是无法实现事务序列化的,必须通过其他机制保证新插入的数据不会被刚执行查询操作的事务访问到。