1mybatsi配置文件
1.mybatis 配置属性的顺序不能颠倒必须从上到下
2.配置别名
<!-- 配置类型别名
必须写在environments 标签之上
-->
<typeAliases>
<!-- 为com.qfedu.entity.Student 取别名为 student-->
<!-- <typeAlias type="com.qfedu.entity.Student" alias="student"></typeAlias>-->
<!-- 为当前包下面所有实体类 取别名
Student 别名 Student student 不区分大小写
-->
<package name="com.qfedu.entity"/>
</typeAliases>
3.mapper的声明
<mappers>
<!-- 告诉mybatis 要根据IStudentDao.xml 的业务创建一个对应的实现类 -->
<!-- <mapper resource="com/qfedu/dao/IStudentDao.xml"></mapper>-->
<!-- 也是告诉 mybtis 创建 一个IStudentDao 实现类,并且业务按照IStudentDao.xml-->
<!-- <mapper class="com.qfedu.dao.IStudentDao"></mapper>-->
<!--告诉mybatis com.qfedu.dao所有的接口都要 创建对应的实现类对象 -->
<package name="com.qfedu.dao"/>
</mappers>
4.开启驼峰写法
<!--
开启驼峰写法 自动将数据库表字段 s_address 映射为Java属性 sAddress
<setting name="mapUnderscoreToCamelCase" value="true"/>
-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
5.声明配置文件
1.在resource创建mysql.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/qf2006
jdbc.username=root
jdbc.password=123456
2.在配置文件使用
<!-- 声明配置文件路径 *****-->
<properties resource="mysql.properties"></properties>
<!--
开启驼峰写法 自动将数据库表字段 s_address 映射为 sAddress
<setting name="mapUnderscoreToCamelCase" value="true"/>
-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置别名-->
<typeAliases>
<package name="com.qfedu.entity"/>
</typeAliases>
<!-- 选区mysql 环境-->
<environments default="mysql">
<!-- mybtis 环境 -->
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<!-- 配置连接池数据源-->
<dataSource type="POOLED">
<!-- 通过配置文件初始化参数 -->
<property name="driver" value="${jdbc.driver}"/>
<!-- 配置url-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 告诉mybatis 要根据IStudentDao.xml 的业务创建一个对应的实现类 -->
<!-- <mapper resource="com/qfedu/dao/IStudentDao.xml"></mapper>-->
<!-- 也是告诉 mybtis 创建 一个IStudentDao 实现类,并且业务按照IStudentDao.xml-->
<!-- <mapper class="com.qfedu.dao.IStudentDao"></mapper>-->
<!--告诉mybatis com.qfedu.dao所有的接口都要 创建对应的实现类对象 -->
<package name="com.qfedu.dao"/>
</mappers>
2.mybatis事务
mybatis默认情况下是开启事务,关闭自动提交
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
如果sqlSession 执行增 删 改 动作时,需要手动提交事务 或者 设为自动提交
int num = iStudentDao.deleteStudentById(102);
System.out.println("num:"+num);
// 提交事务
sqlSession.commit();
// 默认情况 关闭自动提交-----------》开启事务
sqlSession = sqlSessionFactory.openSession(true);
3.动态sql
<if
<!-- 如果 <if test="name !=null and name!=''"> name 不是null 或者 空串 进行sql 连接-->
<select id="findStudentByNameAndSex" resultType="Student">
select id,name,age,sex,height,s_address from student_tb where 1=1
<if test="name !=null and name!=''">
and name like #{name}
</if>
<if test="sex !=null and sex!=''">
and sex = #{sex}
</if>
</select>
<where
<!--
where 1=1 作用就是 当条件成立时 where 后面不能直接根 and 需要在and前 加 true 或者 1=1
<where 标签作用就是 将 内部的 if进行拼接 并且 在sql增加 where 字段 ,删除 where 后的and ,where标签所有的if 都不成立则 不添加where
-->
<select id="findStudentByNameAndSex" resultType="Student">
select id,name,age,sex,height,s_address from student_tb
<where>
<if test="name !=null and name!=''">
and name like #{name}
</if>
<if test="sex !=null and sex!=''">
and sex = #{sex}
</if>
</where>
</select>
<foreach
<select id="findAllStudentByIds" resultType="Student">
select id,name,age,sex,height,s_address from student_tb
<where>
<if test="ids!=null and ids.size()!=0">
<foreach collection="ids" open="id in (" close=") " separator="," item="id" >
#{id}
</foreach>
</if>
</where>
</select>
// arameter 'ids' not found. Available parameters are [collection, list] 参数不能是 list
List<Integer> ids = new ArrayList<Integer>();
ids.add(2);
ids.add(4);
// ids.add(103);
IdsData idsData = new IdsData();
idsData.setIds(ids);
List<Student> studentList = iStudentDao.findAllStudentByIds(idsData);
for (Student studentItem:studentList){
System.out.println("studentItem:"+studentItem);
}
<sql
<!--声明一个 sql-->
<sql id="sql1">
select id,name,age,sex,height,s_address from student_tb
</sql>
<!--
<include refid="sql1"></include> 引用sql 将重复的sql 进行抽离
-->
<select id="findAllStudentByIds" resultType="Student">
<include refid="sql1"></include>
<where>
<if test="ids!=null and ids.size()!=0">
<foreach collection="ids" open="id in (" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
4.多表查询
1对多
1对1
一对一
查找学生成绩并包含成绩对应学生相关的信息
SELECT a.*,b.name,b.age FROM score_tb a LEFT JOIN student_tb b ON a.studentid = b.id;
SELECT * FROM score_tb a , student_tb b WHERE a.studentid = b.id;
实现
1.在Score中添加Student属性,并设置setter getter方法
2.编写业务实现
/**
* 查询所有的成绩并包含对应的学生信息
* @return
*/
List<Score> getAllScoreWithStudent2();
<!--定义 resultType 来接受getAllScoreWithStudent2 查询结果-->
<!--
一对一查询
<association 解决一对一问题
property="student" 对应Score 中 student属性
column="studentid" 对应Score_tb 与 student_tb关联的外键 列名
javaType="Student" 将其他非Score 中的字段写入Student 类
<result property="name" column="name"></result> 写入student对应的属性
-->
<resultMap id="scoreMap1" type="Score">
<id column="scoreid" property="scoreid"></id>
<result column="couresename" property="couresename"></result>
<result column="score" property="score"></result>
<result column="studentid" property="studentid"></result>
<association property="student" column="studentid" javaType="Student" >
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="height" column="height"></result>
<result property="sAddress" column="s_address"></result>
</association>
</resultMap>
<select id="getAllScoreWithStudent2" resultMap="scoreMap1">
SELECT a.*,b.* FROM score_tb a LEFT JOIN student_tb b ON a.studentid = b.id;
</select>
3.测试
@Test // 测试一对一
public void getAllScoreWithStudent2Test(){
List<Score> scoreList = iScoreDao.getAllScoreWithStudent2();
for (Score score:scoreList){
System.out.println("score:"+score);
}
}
一对多
查询所有学生的并包含成绩列表
SELECT * FROM score_tb a , student_tb b WHERE a.studentid = b.id;
1.在Student 添加List scoreList;
2.编写业务
/**
* 查询所有学生并包含对应的 成绩
* @return
*/
List<Student> findAllStudentWithScore();
<!--
<collection 解决一对多问题
property="scoreList" 将collection 组成的成绩集合放置到 student对象中
column="id" 根据相同 的学生id组成集合
ofType="Score" 将学生id组成集合 每一个item 生成一个 Score
-->
<resultMap id="studentWithScoreMap" type="Student">
<id column="id" property="id"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="height" column="height"></result>
<result property="sAddress" column="s_address"></result>
<collection property="scoreList" column="id" ofType="Score">
<id property="scoreid" column="scoreid"></id>
<result property="couresename" column="couresename"></result>
<result property="score" column="score"></result>
<result property="studentid" column="id"></result>
</collection>
</resultMap>
<!-- 查询所有的学生并包含成绩-->
<select id="findAllStudentWithScore" resultMap="studentWithScoreMap">
SELECT * FROM student_tb a LEFT JOIN score_tb b ON a.id = b.studentid
</select>
3.测试
@Test// 一对多 查询
public void findAllStudentWithScoreTest(){
List<Student> studentList = iStudentDao.findAllStudentWithScore();
for (Student student:studentList){
System.out.println("student:"+student);
}
}