mybatis框架笔记2

SQL映射文件开发:

<!-- 
		id:必须和接口中的方法名完全一致        注意:Mapper接口不支持方法重载的
		parameterType:是参数类型   全类名
		resultType:返回值类型
		SQL映射文件中,sql语句获取参数的语法:  #{}   ${}
		#{}:没有SQL注入的问题   推荐使用
		${}:有SQL注入的问题    
		
		增加数据
		修改数据
		删除数据(删除一条数据   批量删除)
		查询数据(单表查询  多表连接   组合查询   分页查询   有参数  没参数   多个参数  返回值是一条  返回值是多条)
	 -->

7.测试mybatis配置是否成功,进行数据库的查询

public static void main(String[] args) throws IOException {
		
		//加载mybatis的核心配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
        //获取连接池对象
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
		//打开连接
		SqlSession session = factory.openSession();
		
		UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
		UserInfo u = mapper.getInfoById(2);
		System.out.println(u);
	}

返回值是多条:

<!--  
		resultType:
		如果查询返回的是一条数据,则直接写返回值类型
		如果查询返回的是多条数据,则写集合中的泛型
	 -->
	<select id="getAll"  resultType="UserInfo"  >
		select *  from  userinfo
	</select>

多个参数:

<!-- 
		如果没有参数,则省略parameterType属性
		如果有一个参数,是基本数据类型,则SQL语句中获取参数时,用什么名字都能获取到
			但是基于编码规范,建议使用接口中形参的名字
		如果有多个参数,解决方案:
		1.省略parameterType属性,SQL语句中通过参数的索引值获取参数,索引值从0开始     不建议使用
		2.使用实体类的类型传入,SQL语句中获取参数时,使用的是实体类中的属性名
		3.使用map类型传入,SQL语句中获取参数时,使用的是map中元素的key
	 -->
<!-- <select id="getUsers"  resultType="UserInfo" >
		select *  from userinfo where username=#{0} and password=#{1}
	</select> -->
	<!-- <select id="getUsers" parameterType="UserInfo"   resultType="UserInfo" >
		select *  from userinfo where username=#{username} and password=#{password}
	</select> -->
	<select id="getUsers" parameterType="map"   resultType="UserInfo" >
		select *  from userinfo where username=#{k_username} and password=#{k_password}
	</select>

组合查询

<!-- 组合查询
		where   if
		字符串类型判空:username!=null and username!=''
		int类型判空:id!=0
		Date类型:date!=null
	 -->
	<select id="getUser" parameterType="UserInfo" resultType="UserInfo"  >
		select *  from  userinfo 
		<where>
			<if test="username!=null and username!='' ">
				and username  like #{username}
			</if>
			<if test="email!=null and email!='' ">
				and email=#{email}
			</if>
		</where>
	</select>
**分页查询:**
<!--  分页查询 -->
	<select id="getPageUser" resultType="UserInfo">
		select * from userInfo limit #{k_index},#{k_pagesize}
	</select>

存储数据:

<!-- 存储数据  -->
	<!-- <insert id="insertEmp" parameterType="Emp"    >
		insert into emp(ename,job,mgr,hiredate,sal,comm,deptno)  
		values (#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
	</insert> -->
	<!-- 存储数据:主键采用最大值+1的方式生成   -->
	<insert id="insertEmp" parameterType="Emp" >
		<!-- 
		keyProperty属性:查询到的数据赋值给Emp中的哪个属性
		resultType属性:查询的返回值类型
		order属性:设置查询是在insert之前执行还是之后执行
		   -->
		<selectKey keyProperty="empno" resultType="int"  order="BEFORE" >
			select max(empno)+1 from emp
		</selectKey>
		
		insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)  
		values (#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
	</insert>

修改数据:

<update id="updateEmp" parameterType="Emp"  >
		update emp
		set sal=#{sal},comm=#{comm}
		where empno=#{empno}
</update>

删除功能:

<!-- 删除功能 -->
	<delete id="deleteEmpByNo"  parameterType="int"    >
		delete from  emp  where empno=#{empno}
	</delete>
	
	<!-- 
		foreach标签:实现循环遍历
		collection属性:要遍历的集合的类型
		item属性:每次遍历到的元素的名称
		open属性: 遍历的结果以什么字符开头
		close属性:遍历的结果以什么字符结束
		separator属性:遍历到的数据以什么字符作为分隔符
	 -->
	<delete id="deleteEmps"    >
		delete from  emp  where empno in 
		<foreach collection="array" item="item" open="("  close=")"  separator=","   >
			#{item}
		</foreach>
	</delete>

查询结果集处理:

<!--  
		查询功能,结果集映射的原理:
		mybatis在处理查询结果时,默认将查询结果列和实体类中的属性去对应,如果同名,则进行赋值
		如果查询结果列名和实体类中属性不同名,解决方案:
		1. 给查询结果列起别名
		2. resultMap的使用
	 -->
	<!-- <select id="getAll" resultType="Emp"  >
		select empno,ename eneme,job,mgr,sal,comm,deptno,hiredate  from emp
	</select> -->
	<select id="getAll" resultMap="empresult" >
		select * from emp
	</select>
	<!-- type属性:查询最终的返回值类型
		result标签:手动实现结果列的映射
		column属性:结果列列名
		property属性:实体类中的属性名
	   -->
	<resultMap type="Emp" id="empresult">
		<result column="ename"  property="eneme"  />
	</resultMap>

多表连接查询
实体之间的关联关系
一对一

<!-- 一对一查询   -->
	<select id="getEmpInfo" parameterType="int" resultMap="eresult"  >
		select e.*, d.deptno ddeptno,d.dname,d.loc
		from emp e,dept d 
		where empno=#{empno} and e.deptno=d.deptno
	</select>
	<resultMap type="Emp" id="eresult">
		<result property="empno" column="empno"   />
		<result property="ename" column="ename"   />
		<result property="job" column="job"   />
		<result property="mgr" column="mgr"   />
		<result property="sal" column="sal"   />
		<result property="hiredate" column="hiredate"   />
		<result property="comm" column="comm"   />
		<result property="deptno" column="deptno"   />
		<!-- 
			association 一对一查询      嵌套结果集处理
			property属性:嵌套的resultMap封装的结果要赋值给当前实体的哪个属性
			column属性:外键列
			javaType属性:嵌套的resultMap返回值类型
			resultMap属性:对应要嵌套的resultMap
		 -->
		<association property="dept"  column="deptno"  javaType="Dept"  resultMap="dresult"  />
	</resultMap>
	<resultMap type="Dept" id="dresult">
		<result property="dname" column="dname"   />
		<result property="loc" column="loc"   />
		<result property="deptno" column="ddeptno"   />
	</resultMap>

一对多

<select id="getDept" parameterType="int"  resultMap="dresult"  >
		select e.*,d.deptno ddeptno,d.dname,d.loc 
		from emp e,dept d 
		where ddeptno=#{deptno} and e.deptno=d.deptno
	</select>
	
	<resultMap type="Dept" id="dresult">
		<result property="deptno"  column="ddeptno"   />
		<result property="dname"  column="dname"   />
		<result property="loc"  column="loc"   />
		<!--  
			collection  一对多查询
			property属性:构建的集合对象赋值给最终返回值类型中哪个属性
			ofType属性:集合中元素的类型
		 -->
		<collection property="emps"  ofType="Emp">
			<result property="empno" column="empno"   />
			<result property="ename" column="ename"   />
			<result property="job" column="job"   />
			<result property="mgr" column="mgr"   />
			<result property="sal" column="sal"   />
			<result property="hiredate" column="hiredate"   />
			<result property="comm" column="comm"   />
			<result property="deptno" column="deptno"   />
		</collection>
		
	</resultMap>

多对多
SQL片段:

<!-- sql片段 -->
	<sql id="sid">
		select  *  from emp
	</sql>
	<select id="getEmpByName" parameterType="string" resultType="Emp"  >
		<include refid="sid"/>
		  where ename like #{ename}
	</select>

Mybatis缓存:

 * mybatis缓存:
		 * 针对查询类需求,提高程序性能,减轻数据库访问压力,提供的缓存技术
		 * 一级缓存:默认开启的
		 * 针对同一个SqlSession,如果基于某一个需求查询到一份数据,则mybatis会将查询结果存储在
		 * 缓存中,接下来如果再一次做同样的查询,则mybatis会直接从缓存中拿数据,而不访问数据库,
		 * 从而提升查询的性能
		 * 手动不开启缓存:flushCache="true" 
		 * 如果过程中执行了该表的DML操作,则缓存数据自动消失
		 * 
		 * 二级缓存:
		 * 不限定SqlSession的缓存的使用    不是默认开启的  需要手动开启(在SqlMapConfig.xml中)

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是MyBatis框架的学习笔记MyBatis是一种基于Java的持久层框架,它通过XML描述符或注解将对象映射到数据库中。它提供了自定义SQL、存储过程和高级映射等功能,使得开发人员可以更加灵活地控制SQL语句的执行过程。 MyBatis的核心组件包括SqlSessionFactory、SqlSession和Mapper。其中,SqlSessionFactory是MyBatis的核心接口,它负责创建SqlSession对象。SqlSession是MyBatis的核心类,它提供了执行SQL语句、获取Mapper接口等功能。Mapper是MyBatis的映射器接口,它定义了SQL语句和Java方法之间的映射关系。 MyBatis的优点包括: 1. 灵活性高:MyBatis提供了自定义SQL、存储过程和高级映射等功能,使得开发人员可以更加灵活地控制SQL语句的执行过程。 2. 易于使用:MyBatis的API简单易用,开发人员可以快速上手。 3. 易于维护:MyBatis的SQL语句和Java方法之间的映射关系清晰明了,易于维护。 4. 性能高:MyBatis采用了预编译和缓存等技术,可以提高SQL语句的执行效率。 以下是一个使用MyBatis框架的Java代码示例: ```java // 创建SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); try { // 获取Mapper接口 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 执行SQL语句 User user = userMapper.selectUserById(1); // 输出结果 System.out.println(user); } finally { // 关闭SqlSession对象 sqlSession.close(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值