关于mybatis的理解


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.例子
增删改+分页

猜你喜欢

转载自www.cnblogs.com/bdqnjs/p/9188693.html