了解mybatics基础

1.什么是mybatics?

MyBatis是一个简化和实现了 Java 数据持久化层的开源框架,它抽象了大量的JDBC冗余代码,并提供了一个简单易用的API和数据库交互。
MyBatis流行的主要原因在于它的简单性和易使用性。在Java应用程序中,数据持久化层涉及到的工作有:将从数据库查询到的数据生成所需要的Java对象;将Java对象中的数据通SQL持久化到数据库中。

2.MyBatis相关网址

MyBatis的前身是iBATIS,来源于internet和abatis的组合。 ibatis的官网:http://ibatis.apache.org/
在这里插入图片描述
但是上面所说的那个网址 http://www.mybatis.org/ 好像是打不开的。。。

但是可以在github上面找到mybatis的相关下载,
mybatis在github中的地址:
https://github.com/mybatis/mybatis-3
最新版本的mybatis的下载地址:
https://github.com/mybatis/mybatis-3/releases
doc文档地址:
http://www.mybatis.org/mybatis-3/

3.mybatics框架中所需的两个文件(mybatis-config.xml、XxxMapper.xml)

  • 3.1 mybatis-config.xml这个名字一般默认就是这样子,自己也可以改,但是一般不会去改。会将其直接放在src下。
    作用:连接数据库、加载Xxxmapper.xml文件
    示例:
<?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> 
	<typeAliases> 
		<typeAlias alias="Student" type="com.briup.pojo.Student" /> 
	</typeAliases> 
	<environments default="development"> 
		<environment id="development"> 
			<transactionManager type="JDBC" /> 
			<dataSource type="POOLED">
				<!--
					<property name="driver" value="com.mysql.jdbc.Driver" /> 
					<property name="url" value="jdbc:mysql://localhost:3306/test" /> 
				-->
				<property name="driver" value="oracle.jdbc.driver.OracleDriver" /> 
				<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" /> 
				<property name="username" value="test" /> 
				<property name="password" value="test" /> 
			</dataSource> 
		</environment> 
	</environments> 
	<mappers> 
		<mapper resource="com/briup/pojo/StudentMapper.xml" /> 
	</mappers> 
</configuration>  
  • 3.2 关于XxxMapper.xml文件,这个xml文件中包括Xxx类(比如上面的StudentMapper.xml)所对应的对数据库表的各种增删改查sql语句
    作用:做相关数据库的增删改查的sql语句
    示例:
<?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.briup.pojo.StudentMapper"> 
	<resultMap type="Student" id="StudentResult"> 
		<id property="id" column="id" /> 
		<result property="name" column="name" /> 
		<result property="email" column="email" /> 
	</resultMap> 
	<select id="findAllStudents" resultMap="StudentResult"> 
		SELECT * FROM STUDENTS 
	</select> 
	<select id="findStudentById" parameterType="int" resultType="Student"> 
		SELECT ID AS STUDID, NAME, EMAIL  
		FROM STUDENTS WHERE ID=#{Id} 
	</select> 
	<insert id="insertStudent" parameterType="Student"> 
		INSERT INTO STUDENTS(ID,NAME,EMAIL)  
		VALUES(#{id },#{name},#{email}) 
	</insert> 
</mapper> 

4.XxxMapper.xml对应的映射接口XxxMapper.java

mybatis中的映射接口XxxxMapper.java,是对XxxMapper.xml中的sql语句进行映射。
mybatis中除了必须的jar包、各种xml配置文件之外,一般还需要有调用sql语句执行的接口XxxxMapper.java
示例:

public interface StudentMapper{
	List<Student> findAllStudents(); 
	Student findStudentById(Integer id); 
	void insertStudent(Student student); 
}

注意:接口中的方法的名字和XML文件定义的SQL映射语句的名称要完全相同,同时我们不需要去实现该接口,因为mybatis中提供了相应的方式在运行期间动态生成该接口的实现类对象。

5.mybatis中的SqlSession接口和sqlSessionFactory接口

1.sqlSessionFactory接口的实现类对象是一个工厂对象,专门负责来产生SqlSession对象的
2.SqlSession接口的实现类对象是mybatis中最重要的一个对象,我们可以使用该对象动态获得XxxMapper.java接口的实现类对象,然后就可以调用到XxxMapper.java接口中方法所映射的sql语句(在xml文件中配置的sql语句)。

例如:

//加载文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 
//通过SqlSessionFactoryBuilder()构建工厂类SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂类SqlSessionFactory获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();

/*拿到sqlSession有以下两种执行sql语句的方式*/
//(推荐的方式)第一种执行sql语句的方式:通过XxxMapper接口的实现类对象来调用
//动态获得XxxMapper接口的实现类
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
studentMapper.insertStudent(new Student(1,"tom","123@.qq.com"));

//(不推荐该方式)第二种执行sql语句的方式:执行调用XxxMapper.xml中写好的sql语句
//(不通过Mapper接口执行映射的SQL,而是直接调用xml文件中的sql
//com.briup.pojo.StudentMapper就代表xml文件
//findStudentById代表xml文件中的id名,传参
sqlSession.selectOne("com.briup.pojo.StudentMapper.findStudentById",1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值