A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 键盘有花生油 初级黑马   /  2018-12-10 11:44  /  668 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

数据库类型
       1. 关系型          三种mysql   oracle   b2   
mysql 6.0 收费
dbms 数据库管理系统
       2.非关系型
  


主键位数
primary key
非空
noy null
唯一
unique
外建
forign key

create table stu(
       id int , primary key auto_
   namee varchar(32)  not null   
)


创建完表格之后建立
alter table stu modify namee varchar(32) not null
删除
atter table stu modify namee varchar(32);




unique的特性
unique 可以有null 但是只有一个


create table stu(
   id int,
  phone_number varchar(32) unique
)
建完表之后创建unique
alter table stu modify phone_number varchar(32) unique
删除
alter table stu dorp index phone_number ;


主键约束
primary key auto_increment
含义
1.非空 且唯一
2.一张表只能有一个主键
3.主键就是表中记录的唯一标识


删除主键
alter table stu drop primary key;
完成表后创建主键
alter table stu  modify id int primary key;


添加自动增长
alter table stu modify id int auto_increment;
删除
alter table stu modify id int ;


外建约束
cod forign key
constraint 外建名称 forign key (外建名称列名称) references 主表名称  主表列名
create   table  stu (

   外建列
     dep_id  int


constraint    外建名自己起的 forign key()  references 主表名称  (主表列名)
  
)
删除外建
alter table stu drop forign key 外建名称
创建表之后创建外建


alter table stu add constraint 外建名称 forign key (dep_id) references 主表名(主表列名)


添加级联操作
alter table stu  add constraint 外建名称
  forign key (外建名称) recerences 主表名称(主键列) on update cascade on
删除级联
on delete cascade
刷新级联
on update cascade


事务的概念
如果一个包含多个步骤的业务操作,被事务管理,要么同时成功要么 同时失败.
开始事务
start transation
回滚
rollback
提交
commit
事务提交的方式有两种
自动提交1
手动提交0
masql默认是自动提交
查看默认提交
select
修改提交方式
set @@autocommiit
原子四大特征
1.原子性  是不可分割的最小单位要么同时成功,要么同时失败
2.持久性 事物提叫或者回滚后会持久化的保存数据
3.隔离性 多个事物相互独立
4.一致性  事务操作前后 数据总量不变
隔离级别越高越安全,效率越低
四个隔离界别
1.read uncommitted 产生的问题 读未提交  脏读  不可重复读 幻读
2. read committed 产生的问题 读已提交 不可重复读  幻读
3.repeatable read 可重复读(mysql 默认) 产生分问题  幻读
4.serializable:串行化* 可以解决所有的问题
连接池
        C3P0
                接入步骤:
                        1.导jar
                                1.c3p0  2个
                                2.mysql驱动
                        2.导入配置文件(src目录下)
                                c3p0-config.xml
                        3.使用
                                DataSource dataSource = new ComboPooledDataSource();
                                Connection con = dataSource.getConnection();
               
        DRUID(德鲁伊)
                接入步骤
                                1.导jar
                                        1.druid
                                        2.mysql驱动
                                2.导入配置文件(src)
                                        druid.properties
                                3.使用
                                        Properties pro = new Properties();
                                        pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"))
                                        DataSource dataSource = DruidDataSourceFactory.createDataSoure(pro)
                                        Connection con = dataSource.getConnection();
                                       
                                       
        JdbcUtils实现思路
                1.获取数据源(连接池)        DataSource
                2.获取连接对象                         Connection
                3.释放资源                                重载




                思路:
                        1,2:
                                DataSource在类加载的时候就应该初始化,为了保证对象能够及时创建,在 static{} 静态代码块中创建的对象
                                        Properties pro = new Properties();
                                        pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"))
                                        dataSource = DruidDataSourceFactory.createDataSoure(pro)
                                
                                提供返回数据源的方法 getDataSource()   ---> return dataSource
                                提供返回连接                 getConnection()   ---> return dataSource.getConnection()
                                
                        3:
                                在关闭之前做非空判断!!!




JdbcTemplate
        SPRING        (基于jdbc来简化数据库的操作)
        
        !!!如果有占位符需要传入参数
        
        DataSource dataSource = new ComboPooledDataSource();
        JdbcTemplate template = new JdbcTemplate(dataSource);
               
        增删改
                template.update("sql",[具体参数]);
               
        查询
                单条结果
                        Map<String,Object> map = template.queryForMap("sql",[具体参数]);
                        Bean bean = template.queryForObject("sql",new BeanPropertyRowMapper<Bean>(Bean.class),[具体参数]);
                多条结果
                        List<Map<String,Object>> list = template.queryForList("sql",[具体参数]);
                        List<Bean> list = template.query("sql",new BeanPropertyRowMapper<Bean>(Bean.class),[具体参数]);
               


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马