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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© aaa386376407 初级黑马   /  2019-1-24 22:56  /  974 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

连接池
解决高并发
当拿出后 顺序发生改变
重新排序
归还后 去末尾

容器就是一个集合对象 线程安全 一对一 先进先出

mybatis中的连接池
3种方式配置:
sqlmapconfig.xml 中 datasource标签,type 表示何种连接池方式
type
pooled     获取 - 用 - 还   
空闲池 if true,get one;
if false,go 活动池
活动池 check if max,
use oldest;

unpooled 创 - 用

jndi (了解)  服务器提供的jndi技术获取datasource,不同的服务器所拿到的datasource不一样, only web/maven-war        

mybatis中的事务
通过sqlseesion的
commit方法和roolback方法
opensession(true)自动提交

多个参数 用map来封装
Map map = new Map
map.put(id,1)
map.put(...)
getMapper(map);

xml中与之前相同

select * from user where 1=1//可以省略and<if test="userName!=null">    username = #{username}</if>


select * from user//不可以省略and<where><if test="userName!=null">    username = #{username}</if></where>

select * from user//不可以省略and<where><if test="userName!=null">    username = #{username}</if><if test="userSex!=null">    usersex = #{usersex}</if></where>

select * from user<where><if test="ids!=null and ids.size()>0">                集合          开始部分                   每个元素   用?分隔    <foreach collection="ids" open="and id in("close") item="id" separator=",">        #{id}//与item对应    </foreach></if></where>
抽取重复sql语句:<sql id="defaultUser">
<include refiled = "defalutuser">

mybatis的多表查询
一对多
一对一
多对多

one user have many bankcrad
one bankcard only belong one user

user
bankcard  外键

用户配置文件 账户配置文件

<resultType>    |    类            集合<association>/<collection>

user ->computers


关于一对多需要注意的事项

SqlMapConfig.xml中需要多添加typeAliases标签!!!
<typeAliases>
    <package name="com.itheima.domain"/>
</typeAliases>



domain中 User和Computer 都需要写对方的List成员变量!!!
private List<Computer> computers;
private List<User> users;



UserDao.xml中 需要添加resultMap标签!!!若关联对象是集合用collection,类用association
column中的值不能重复

<resultMap id="u_c" type="user">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
        <collection property="computers" column="id" ofType="computer">
            <id property="id" column="cid"></id>
            <result property="name" column="cname"></result>
            <result property="uid" column="uid"></result>
        </collection>
</resultMap>


resultMap与上面对应
<select id="findAll"  resultMap="u_c">
    select u.*,c.id cid ,c.name cname, c.uid from user u,computer c where u.id = c.uid
</select>



关于多对多需要注意的事项

需要创建第三张表!
D:/youdao/weixinobU7VjuozRmRZ-Yc6_UjnKavZoP0/7e6b053026b4404f9a61084d63468d1b/clipboard.png

注意标签中的重命名
<mapper namespace="com.itheima.dao.TeacherDao">

    <resultMap id="t_s" type="teacher">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <collection property="students" column="id" ofType="student">
            <id property="id" column="s_id"></id>
            <result property="name" column="s_name"></result>
        </collection>
    </resultMap>

    <select id="findAll"  resultMap="t_s">
      select t.* , s.id s_id , s.name s_name from teacher t , student s , teacher_student
       where t.id = teacher_student.tid
       and s.id = teacher_student.sid
    </select>
</mapper>



JNDI数据源
目的:模仿windows中的注册表
map结构

通过配置-服务器-数据库


0 个回复

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