欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis框架2

程序员文章站 2022-03-10 17:18:08
...

配置文件

//mybatis配置文件
<?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>
	<properties resource="resource/jdbc.properties"></properties>

	<typeAliases>
		<package name="com.zhiyou.pojo"/>
	</typeAliases>


  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${jdbc.Driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.name}"/>
        <property name="password" value="${jdbc.password}"/>
      </dataSource>
    </environment>
  </environments>
  <!-- 加载映射文件
  	1.使用resource 属性,配置映射文件的路径
  	2.使用class属性,配置映射文件所对应的接口,接口的完整名称
  		【映射文件和接口放在同一个路径下
  		 映射文件名称和接口名称一模一样】
  	3.使用package标签,配置包名
  		【映射文件和接口放在该包下
  		 映射文件名称和接口名称一模一样】
   -->
  <mappers>
   <!--  <mapper resource="com/zhiyou/mapper/UserMapper.xml"/> -->
    <!-- <mapper class="com.zhiyou.mapper.UserMapper"/> -->
    <package name="com.zhiyou.mapper"/>
  </mappers>
</configuration>





通过手动设置结果集映射

MyBatis框架2

 

//UserMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:映射文件所对应的接口 -->
<mapper namespace="com.zhiyou.mapper.UserMapper">
 
 <!-- 数据库中的字段名称和类中属性名称不一致,可以使用别名处理 -->
 
 <select id="getUserById" resultType="user">
 	select id,name,password,u_age age
 	 from user 
 	 where id = #{id}
 </select>
 
 <!-- 动态sql,模糊查询 -->
 <select id="getUsersWithNameAndPwd" resultType="user">
 	select id,name,password,u_age age
 	from user
 	<where>
 	<if test="name != null">
 		and name like "%"#{name}"%"
 	</if>
 	<if test="pwd != null">
 		and password like "%"#{pwd}"%"
 	</if>
 	</where>
 
 </select>
 
 <!--  
 id in (1,2,3)
 
 	collection:如果要遍历的参数是数组 array(参数类型)
 			如果要遍历的参数是集合 collection(参数类型)
 	item:给集合中每个元素起的标记
  -->
 
 <delete id="deleteUsers">
 	delete from user
 	where id in 
 	<foreach collection="array" item="id" 
 	open="(" close=")" separator=",">
 		#{id}
 	</foreach>
 </delete>
 
 <!-- concat拼接字符串 -->
 <!--小于号的写法<![CDATA[<=]]>  -->
 <select id="searchUser" resultType="user">
 	select id,name,password,u_age age
 	from user
 	where ${field} like concat('%',#{key},'%')
 	and u_age >= 20 and u_age <![CDATA[<=]]> 30
 
 </select>
 
 
 
 
 
</mapper>
//接口文件 UserMapper.java

package com.zhiyou.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.zhiyou.pojo.User;

public interface UserMapper {

	User getUserById(@Param("id")String id);
	
	List<User> getUsersWithNameAndPwd(@Param("name")String name,@Param("pwd")String pwd);
	
	//批量删除
	void deleteUsers(int[] ids);
	
	//搜索用户
	//按照名称,或者密码搜索
	/**
	 * 搜索用户
	 * @param field  要进行匹配的字段
	 * @param key 关键词
	 * @return
	 */
	List<User> searchUser(@Param("field")String field,@Param("key")String key);
	
	
}

<!--映射文件Teacher.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhiyou.mapper.TeacherMapper">

	<!-- 从数据库中查询到的数据和对象属性不一致
		1.给字段起别名
		2.手动设置映射规则,设置resultMap
	 -->



	<!-- 自定义结果集映射时
		如果属性是对象类型,使用association标签
		如果属性是集合类型,使用collection标签
	 -->


	<resultMap type="teacher" id="t1">
		<id column="tid" property="id"/>
		<result column="tname" property="name"/>
		<result column="ttel" property="tel"/>
		<!-- property:属性名称
			ofType:集合中元素的类型 -->
		<collection property="students" ofType="student">
			<id column="sid" property="id"/>
			<result column="sname" property="name"/>
		</collection>
	</resultMap>


	<select id="getOneTeacherWithId" resultMap="t1">
	 	select t.id tid,
	 	t.name tname,
	 	t.tel ttel,
	 	s.id sid,
	 	s.name sname
		FROM  teacher t,student s
		where s.t_id = t.id
		and t.id=#{id}
	
	</select>

</mapper>
  
  
//接口文件 Teacher.java


package com.zhiyou.mapper;

import com.zhiyou.pojo.Teacher;

public interface TeacherMapper {
	
	//根据id查询老师的信息,以及其学生
	Teacher getOneTeacherWithId(int id);

}
<!--配置硬射文件RoomMapper-->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <!--与接口相对应的路径 -->
	<mapper namespace="com.zhiyou.mapper.RoomMapper">
	
	<!-- 通过标签手动设置结果集映射
		type:类型
		id:映射规则的名称
	  -->
	<resultMap type="room" id="room1">
	<!-- column:结果集中的列名(字段名)
	     property:类中的属性名
	     
	     只有id字段使用id标签,其他字段都是用result标签
	 -->
		<id column="rid" property="id"/>
		<result column="rname" property="name"/>	
		
		<!-- property:类中属性名称
			javaType: 属性的类型 -->
		<association property="teacher" javaType="Teacher">
			<id column="tid" property="id"/>
			<result column="tname" property="name"/>
			<result column="ttel" property="tel"/>
		</association>
		
	</resultMap>
	
	
	<select id="getRoomById" resultMap="room1">
	SELECT r.id  rid,r.name  rname,
	t.id tid,t.name tname,t.tel ttel
	from room r,teacher t
	where r.t_id = t.id
	and r.id = #{id}
	</select>
	
	<select id="getAllRooms" resultMap="room1">
		SELECT r.id  rid,
		r.name  rname,
		t.id tid,
		t.name tname,
		t.tel ttel
		from room r
		left join teacher t
		on r.t_id = t.od
	
	</select>


</mapper>
  
  
//接口文件  RoomMapper.java


package com.zhiyou.mapper;

import java.util.List;

import com.zhiyou.pojo.Room;

public interface RoomMapper {

	Room getRoomById(int id);
	
	//查询所有教室信息,以及在该教室上课的老师信息
	List<Room> getAllRooms();
	
}

 

相关标签: Mybatis