1.IJ Intellij Idea 智能的想法
2.MyEclipse 启动后占用的内存是 300mb---500mb
Idea 的内存 400---700mb
MyEclipse 中的一个工作区 (workspace) 可以有 N 个 project
idea 一个 Project 有多个 Module
3. 创建一个 Project
选择的是 Maven--->archetype(骨架)
4. 创建工程的时候, 有一个快速的设置
设置如下
archetypeCatalog = internal
5. 保存 code 的时候一定要保存在非系统盘
6. 一定要注意右下角的提示, auto import
7. 需要创建一个模块
8.pom.xml
pom Project Object Model 项目对象模型
9.maven 的标准目录
- src main java
- src main resources
- src test java
- src test resources
仓库位置: c:\users\**\.m2\repository
10.maven 的三种仓库
本地仓库 中央仓库 远程仓库
11.SSM Spring(春天, 泉水) 管理各种业务 bean IOC 和 AOP 思想
SpringMVC 子框架 请求的调度和派发 mvc 框架
Mybatis 半自动化 ORM 框架, 手动建表, 手动写 SQL
SSH Spring java 不死就是因为 Spring 的存在 Spring 新颖的理念: Spring Boot SpringCloud
Struts2 (Struts1) 请求的调度和派发 mvc 框架
Hibernate 全自动化 ORM 框架 自动建表 HQL 自动的生成 SQL
12.ORM Object Relational Mapping 对象 (java 对象 实体类) 关系 (MySQL Oracle SQL Server 关系型 二维表) 映射(xml)
- NoSQL
- Redis
- Mongodb
巨杉数据库
13.Mybatis 配置文件
名称 Mybatis-Config.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <!-- 核心配置文件的根节点 -->
- <configuration>
- <!-- 节点是有顺序的 -->
- <!-- 让 xml 识别到 jdbc.properties 配置文件 -->
- <properties resource="jdbc.properties"></properties>
- <!-- 数据库的连接信息 -->
- <environments default="development">
- <environment id="development">
- <!-- 事务管理 JDBC
- tx.commit() tx.rollback()
- 取值: JDBC MANAGED
- -->
- <transactionManager type="JDBC" />
- <!-- 数据源:
- POOLED: 连接池
- UNPOOLED: 不启用连接池
- JNDI: Java Naming And Directory Interface java 命名与目录接口
- -->
- <dataSource type="POOLED">
- <!-- 数据库的连接属性 -->
- <property name="driver" value="${jdbc.driver}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </dataSource>
- </environment>
- </environments>
14. 实体类, 创建接口, 接口对应的配置文件, 主配置中去关联小配置
注意点: 如果大家将小配置配置到了 dao 层, 你必须手动改变 build 节点.
build 节点的重新设置, 就是让 src 下的 xml 文件参与编译到 Target 目录.
- <build>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- </resource>
- </resources>
- </build>
15. 测试类
SqlSession:java 程序和 DB 通信的入口.
- SqlSessionFactory
- SqlSessionBuilder:
- @Test
- public void findAll() throws IOException {
- //1. 根据大配置, 装载数据库的连接信息
- String resource="mybatis-config.xml";
- //xxxxxxxxxxxxxxxxxxxxxxxxxx 流
- //2. 将硬盘上的一个文件和输入流进行绑定
- InputStream is= Resources.getResourceAsStream(resource);
- //3. 构建 Session 工厂
- SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
- //4. 根据工厂去获取会话对象
- SqlSession session = factory.openSession();
- //5. 执行 SQL 语句, 获得结果
- List<Dept> list = session.selectList("cn.happy.dao.IDeptDAO.findAll");
- for (Dept dept:list)
- {
- System.out.println(dept.getDeptname());
- }
- //6. 关闭会话
- session.close();
- }
16. 日志集成
1. 需要在 resources 目录下添加一个名称为 log4j.properties 的物理文件
2.pom.xml 文件中必须有一个节点, 日志的配置
3. 日志的 5 大级别; Fatal>Error>Info>Debug>Trace
17.getMapper()
18. 添加和修改
添加和修改一定要在事物环境中运行
19. 作业
1. 预习作业:
resultMap
删除
别名 alias
resultMap
过程: 1. 现在 Mybatis-config.xml 设置一个
<settings>
<setting name="autoMappingBehavior" value="NONE"/>
</settings>
2. 在小配置中启用
- <resultMap id="myEmpMapper" type="Emp">
- <id property="empno" column="empno"></id>
- <result property="empname" column="empname"></result>
- </resultMap>
- <select id="findAll" resultMap="myEmpMapper">
- select * from emp
- </select>
结果:
- 23:58:44,830 DEBUG findAll:139 - ==> Preparing: select * from emp
- 23:58:44,908 DEBUG findAll:139 - ==> Parameters:
- 23:58:44,930 DEBUG findAll:139 - <== Total: 5
1 微冷的雨 null
2 老原 null
3 群主 null
4 房山一哥 null
5 拳王春晖 null
结果说明: 即使使用 * 检查所有列, 那么最终展示的结果仍然是在 resultMap 中用户手动映射的列.
工具类
20. openSession 底层到底做了什么
1.openSession 是 SqlSessionFactory 的方法, SqlSessionFactory 是一个接口
2. 寻址 SqlSessionFactory 实现类: DefaultSqlSessionFactory
3. 寻找 DefaultSqlSessionFactory.openSession()里面调度了
openSessionFromDatasource(ExecutorType,TransactionIsolationLevel level,autoCommit)
4. 读取大配置文件, 将配置文件的节点, 变成对象进行分析.
- final Environment environment =configuration.getEnvironment();
- final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment)
- tx= transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);
- final Executor executor = configuration.newExecutor(tx,execType,autoCommit);
- return new DefaultSqlSession(configuration,executor);
- 5.
- public DefaultSqlSession(Configuration configuration, Executor executor) {
- this.configuration = configuration;
- this.executor = executor;
- this.dirty = false;
- }
结论: 对 sqlSession 接口的实现类 DefaultSqlSession 的成员变量做初始化工作, 并且 dirty=false; 默认
java 程序内存中 Data 和 DB 中的 Data 保持一致.
21. insert 和 delete 底层到底是什么
- public int insert(String statement) {
- return insert(statement, null);
- }
- public int insert(String statement, Object parameter) {
- return update(statement, parameter);
- }
- public int delete(String statement) {
- return update(statement, null);
- }
- public int delete(String statement, Object parameter) {
- return update(statement, parameter);
- }
为什么 session.commit()能引起事物的提交
session.close()为什么可以回滚事务
2. 所有源代码 电子版 ----- 形成博客 idea...Mybatis 案例 log4j 集成的
3. 手写第一张上机练习 ++++++ 课后简答代码
- ***// 框架学习网站(内附有框架的各种流程图)
- https://github.com/
所有人需要从 U3 开始维护自己的一个项目
审批流转
权限管理
Redis
作业: 1. 上午讲解的内容博客 + 代码
2. 模糊查询 + 多条件查询
3. 智能标签, 手写一遍
4. 调试成功
5. 自己的项目, 必须雏形, easyui 前端框架登录, 调整好
1. 模糊查询 按照雇员姓名进行模糊查询
2. 多条件查询: 雇员姓名(模糊匹配) 部门名称(完全匹配)
重点内容回顾:
1.SqlSession 生命周期和作用域
1.1 单次会话, 说白了就是和数据库服务器的一个连接.
2.SqlSessionFactory 接口, 实现类是 DefaultSqlSessionFactory
openSession() 可以构建出一个 Session 对象(和数据库进行交互的上下文入口).
3.resultMap 和 resultType
resultMap 作用: 解决属性名和字段名不匹配的问题
- <settings> 默认的 value 值是 PARTIAL 是否自动映射
- <property name="autoMappingBehavior" value="NONE"></property>
- </settings>
4. 模糊查询
where studentName like '%' #{studentName} '%' 能防止 SQL 注入
where studentName like concat('%',#{studentName},'%') 能防止 SQL 注入
where studentName like '%${studentName}%' 不能防止 SQL 注入 不推荐使用
5. 多条件查询: 多个参数封装成 Map
- dao:
- public List<Emp> findListByManyCondition(Map<String,Object> map);
- xml:
<select id="findListByManyCondition" resultType="Emp">
select Emp.* from emp,dept where emp.deptno=dept.deptno and empname like '%' #{empname} '%' and deptname=#{deptname}
</select>
6.delete 和 insert 底层 使用的都是 update
7. 别名的配置
- <typeAliases>
- <typeAlias>
- <package name="con.happy.entity"></package>
- <typeAlias>
- </typeAliases>
- -------------------------------------------------------------------------
1. 多条件查询的第二种方式, 索引号
- <!-- 多条件查询索引版 -->
- <select id="findListByManyConditionIndex" resultType="Emp">
- select * from emp where empname like '%' #{0} '%' and deptno>#{1}
- </select>
- List<Emp> list = mapper.findListByManyConditionIndex("雨",2);
2. 添加完成后获取 ID 自增列值 手段
- <!-- 添加完成后, 返回自增列的值 -->
- <insert id="returnAutoIncrementValue">
- insert into emp(empname,deptno) values(#{empname},#{deptno})
- <selectKey resultType="int" keyProperty="empno" order="AFTER">
- select @@IDENTITY
- </selectKey>
- </insert>
3. 智能标签
1. if
根据员工的姓名和员工的部门编号, 查找符合条件的记录
- Preparing: select * from emp WHERE empname like '%' ? '%' and deptno=?
- Preparing: select * from emp WHERE empname like '%' ? '%'
- Preparing: select * from emp
- 2. where
- 3.choose
java switch 选择结构 ==== 只走一个分支
4.foreach
数组
List Integer
自定义 List 泛型
5. 例子
增删改 + 分页
来源: http://www.bubuko.com/infodetail-2645796.html