5个框架以及用途
1 Mybatis 用途:ORM框架/持久层框架
2 Spring 用途:各种业务bean的管理,整合其他框架的一个框架
3 Springmvc 用途:流程控制,流程调度 ,request
4 Hibernate 用途:ORM框架/持久层框架
5 Struts2 请求调度框架 和Springmvc有的一拼
框架是什么?
框架是一个半成品,他可以给开发提供基础架构,提升开发效率,缩短开发周期。而且可以方便的去扩展或者集成其他框架。
开发MyBatis程序的步骤
1.下载jar
2. 配置jar
3. 大配置
4.实体类
5.dao层写接口
6.映射文件
7.单测
1.删除部门
注意点:要传id ,要提交事务
2.别名
<typeAliases><!--类型 别名-->
<!--<typeAlias type="cn.happy.entity.Dept" alias="Dept"></typeAlias>-->
<!--方式二:可以将被扫描包的简单类名作为 别名-->
<package name="cn.happy.entity"></package>
</typeAliases>
可以在小配置中直接使用我们定制的简单名称进行数据操作
3.resultMap
resultMap和resultType 区别
解析:resultType:只能和领域模型一致,受到限制,进行多个实体属性映射的时候,做不到,需要resultMap的参与
resultMap的映射级别
NONE 不做映射
PARTIAL 部分映射 ,外层属性可以映射,关联对象的属性不做映射 Default
FULL 都映射
我们碰到一件事情,DB中的Table中的Field和 Java对象中的属性名称不一致。
我们需要手工映射。
<resultMap id="myDeptMapper" type="Dept">
<id property="deptno" column="deptno"></id>
<result property="deptname2" column="deptname">
</resultMap>
<!--1.查询所有resultMap-->
<select id="findAll" resultMap="myDeptMapper">
select * from dept
</select>
高级使用:当底层有多列,但是界面上只需要显示部分列的时候,我们可以在mybatis-config.xml中做一个全局配置。
<settings>
<setting name="autoMappingBehavior" value="NONE"/>
</settings>
4.工具类
都是静态的
public class MyBatisUtil {
static String resouce="mybatis-config.xml";
static InputStream is ;
static SqlSessionFactory factory;
static {
try {
is= Resources.getResourceAsStream(resouce);
factory= new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
return factory.openSession(true);
}
}
5.openSession到底做了什么
1).openSession是SqlSessionFactory的方法,SqlSessionFactory是一个接口
2).SqlSessionFactory的实现类,Ctrl+H 找到DefaultSqlSessionFactory实现类 openSession方法
3).在openSession方法中,调度了一个有三个参数的方法 openSessionFromDataSource(ExecutorType ,TransationISOLationLevel level,autoCommit)
4).转到了openSessionFromDataSource,5行核心代码
将配置文件中的节点变成了程序中的对象进行分析解析
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; //干净 内存中的数据 ================>数据表中的数据一致。
}
结论:openSession解析核心配置文件的节点,给会话对象的成员变量赋初值。
6.insert和delete底层是什么
public int delete(String statement) {
return update(statement, null);
}
public int delete(String statement, Object parameter) {
return update(statement, parameter);
}
public int insert(String statement) {
return insert(statement, null);
}
public int insert(String statement, Object parameter) {
return update(statement, parameter);
}
7.模糊查询
1).select * from studentinfo where stuname like '%' #{stuname} '%' ? +++++++++ sql 数据库服务器
2).select * from studentinfo where stuname like concat('%',#{stuname},'%')
3).select * from studentinfo where stuname like '%${stuname}%' 客户端的查询内容连同%%符号 就被发送到了服务器
8.多条件查询
select * from student
where 1=1
多条件查询Map
1)dao
public List<Student> getStudentByMultiCondition(Map<String,Object> map);
2) xml中
<select id="getStudentByMultiCondition" resultType="Student">
where stuname like concat('%',#{stuname},'%')
</select>
3)测试类
Map<String,Object> map=new HashMap<String,Object>();
map.put("stuname","发哥");
多条件查询,索引号
05.查询姓名中包含“雨”,并且年龄>20的学生信息
public List<StudentInfo> findStudentsByConditionMutliArgs(String stuName,int stuAge);
<!--多条件查询使用索引-->
<select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}
</select>
sessionJava程序和数据库交互的入口,他的提交可以让事务提交。
9.为什么session.commit()引起事务的提交
1.session.commit(); SqlSession接口的
2.我们知道的是 SqlSession接口的实现类是DefaultSqlSession
3.找到实现类的commit
public void commit() {
commit(false);
}
4.
public void commit(boolean force) {
try {
executor.commit(isCommitOrRollbackRequired(force)); //去调度执行器的commit,入参进来的结果为true
dirty = false; //事务结束后,恢复dirty的值
}
5.
public void commit(boolean required) throws SQLException { required 为真
if (required) {
transaction.commit();//引起事务提交
}
}
session.commit();引起事务的提交
Transaction.commit();
关于dirty: factory.openSession(); dirty=false
CUD : 底层都是update,在try中共第一行code dirty=true
直接影响到了,方法判定 dirty||force
if(required){
transaction.commit();
dirty=false;
}
10. 智能标签
//06.智能标签if
public List<StudentInfo> findByIf(StudentInfo stu);
//06.智能标签choose
public List<StudentInfo> findByChoose(StudentInfo stu);
//07.智能标签foreach
public List<StudentInfo> findByForeachArray(int[] ids);
//07.智能标签foreach List<Integer>
public List<StudentInfo> findByForeachList(List<Integer> list);
//07.智能标签foreach List<StudentInfo>
public List<StudentInfo> findByForeachListStudent(List<StudentInfo> list);
智能标签if
<where>
<if test="stuno!=null">
and stuno=#{stuno}
</if>
</where>
<where>
<choose>
<when test="empname!=null">
sql
</when>
<otherwise>
</otherwise>
</choose>
</where>
2.使用索引号进行多条件查询
dao:方法
public List<Emp> findByCondition(String name,int age);
映射文件
select * from emp where name like '%' #{0} '%' and age>#{1}
3.智能标签 forach array
dao:
public List<Emp> findCondition(int[] ids);
映射文件
<select id="findCondition" resultType="Emp">
select * from emp
<where>
<if test="array.length>0">
empno in
<foreach open="(" close=")" seperator="," item="myid">
#{myid}
</foreach>
</if>
</where>
</select>