eclipse搭建mybaties

本文介绍了如何在Eclipse中搭建MyBatis框架,包括创建Web工程、引入MySQL相关jar包、配置mybatis-config.properties和db.properties文件、创建JavaBean及对应的mapper.xml。通过这些步骤,你可以成功建立一个基础的MyBatis开发环境。
摘要由CSDN通过智能技术生成

前言:

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。
MyBatis的主要成员

  1. Configuration MyBatis所有的配置信息都保存在 Configuration对象之中,配置文件中的大部分配置都会存储到该类中
  2. SqlSession 作为MyBatis工作的主要顶层API,表示和数据库交互时的会话,完成必要数据库增删改查功能
  3. Executor MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护
  4. StatementHandler 封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数等
  5. ParameterHandler 负责对用户传递的参数转换成JDBC Statement 所对应的数据类型
  6. ResultSetHandler 负责将JDBC返回的ResultSet结果集对象转换成List类型的集合
  7. TypeHandler 负责java数据类型和jdbc数据类型(也可以说是数据表列类型)之间的映射和转换
  8. MappedStatement MappedStatement维护一条<select|update|delete|insert>节点的封装
  9. SqlSource 负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回
  10. BoundSql 表示动态生成的SQL语句以及相应的参数信息
一、创建web工程
二、引入jar包(数据库使用mysql)

在这里插入图片描述

三、在src目录下创建mybatis-config.properties配置文件和db.properties数据库配置文件

db.properties

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
user=root
password=123456

mybatis-config.properties

<?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="db.properties">
	</properties>
	<typeAliases>
		<!-- 给具体的JavaBean设置别名 
			<typeAlias type="com.hd.pojo.Student" alias="student"/>
		-->
		<!-- 设置默认的包,查找的时候会在该包下查找,名称不区分大小写!!! -->
		<package name="com.wy.pojo"/>
	</typeAliases>
	
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driverClass}"/>
        <property name="url" value="${jdbcUrl}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
 
  <mappers>
  <!--Mapper.xml的路径-->
    <mapper resource="com/wy/pojo/StudentMapper.xml"/>
  </mappers>
</configuration>
四、创建javaBean以及相对应的mapper.xml

在这里插入图片描述
StudentMapper.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文件路径-->
<mapper namespace="com.wy.pojo.StudentMapper">
  <!-- 查询所有的用户信息
  	  id:自定义 唯一 见名知意
  	  resultType:接收数据的类型
   -->
   <!--javaBean和数据库字段不一致时-->
  <!--  <resultMap type="userbean" id="baseMap">
		指定字段和属性的映射关系
		<id column="s_id" property="sid"/>
		<result column="s_name" property="sname"/>
		<result column="s_birth" property="sbirth"/>
		<result column="s_sex" property="ssex"/>
	</resultMap> -->
	
	<!-- sql块 :将常用的sql部分提取出来,公用 -->
<sql id="baseSql">
	s_id,s_name,s_birth,s_sex
</sql>
<!--resultType="Student" 如果mybatis-config.xml中没有指定默认查找的包这里需要改为resultType="com.wy.pojo.Studentcom.wy.pojo.Student" -->
<select id="select" resultType="Student">
  	select 
  	    <include refid="baseSql"></include> 
  	from 
  	   student s
</select> 
</mapper>
五、测试
@Test
	public void selectAll() throws IOException {
		// 1.加载配置文件
		InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

		// 2.获取对应的SQLSessionFactory对象
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		// 3.获取对应的session对象
		SqlSession session = factory.openSession();
		List<Student> list = session.selectList("com.wy.pojo.StudentMapper.select");
		for (Student student : list) {
			System.out.println(student);
		}
		// 5.关闭session
		session.close();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值