入门步骤:
<!-- mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<build>
<!-- 资源插件:处理src/main/java目录中的xml-->
<resources>
<resource>
<directory>src/main/java</directory><!-- 所在目录-->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
执行sql语句需要mybatis中的对象来执行,可以通过主方法来执行代码,也可以通过单元测试来执行。因为需要执行好几个方法,为了执行代码方便通过单元测试,写独立的方法,来测试访问数据库的功能
执行sql语句需要mybatis中的对象来执行,获取mybaits对象
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration 实例来构建出 SqlSessionFactory 实例。
从 XML 文件中构建 SqlSessionFactory 的实例非常简单,建议使用类路径下的资源文件进行配置。 但也可以使用任意的输入流(InputStream)实例,比如用文件路径字符串或 file:// URL 构造的输入流。MyBatis 包含一个名叫 Resources 的工具类,它包含一些实用方法,使得从类路径或其它位置加载资源文件更加容易。
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。例如
try (SqlSession session = sqlSessionFactory.openSession()) {
Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
}
//调用mybatis某个对象的方法,执行mapper文件中的sql语句
//mybatis核心类:SqlSessionFactory
//定义mybatis主配置文件的位置,从类路径开始的相对路径
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactory对象,使用SqlSessionFactoryBuidler类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.指定要执行sql语句的id
//sql语句的id=namespace+“.”+<select>|update|insert|delete标签的id属性值
String sqlid="cim.bjpowernode.dao.StudentDao"+"."+"selectStudentById";
//6.通过SqlSession的方法,执行Sql语句 selectOne():有一个参数的方法
Student student=sqlSession.selectOne(sqlid);
System.out.println("使用mybatis要查询的学生:"+student);
//7.关闭SqlSession对象
sqlSession.close();
<!-- 查询一个学生Student
<select>:表示查询操作,里面是select语句
id:要执行的sql语句的唯一标识,是一个自定的字符串
推荐使用dao接口中的方法名称
resultType:用来告诉告诉mybaits,执行sql语句,把数据赋值给那个类型的java对象
值现在使用java对象的全限定名称
cim.bjpowernode.domain.Student告诉 框架要转换的对象
当执行sql语句之后,把执行的结果转成student对象,由框架由反射机制创建student类的对象,把同名的列赋给同名的属性
#{student}:占位符,表示从java程序中传入过来的数据
-->
<select id="selectStudentById" resultType="cim.bjpowernode.domain.Student">
-- select id,name,email,age from student where id=1001
select id,name,email,age from student where id=#{studentId}
</select>
当方法参数较多时,使用对象,对象只要有属性,每个属性有set,get方法
属性名必须和表的列名相同,否则返回的对象的属性为空
<select id="selectByObject" resultType="com.bjpowernode.domain.Student">
select id,name ,email,age from student where
name =#{name,javaType=java.lang.String,jdbcType=VARCHAR}
or
age=#{age,javaType=java.lang.Integer,jdbcType=INTEGER}
</select>
这个方式不推荐使用,
:可读性不好,arg0和arg1不清晰是什么值
:List<Student> selectByPosition(String name,Integer age);
方添加了一个参数,结果错误
:select id,name ,email,age from student where name =#{arg0} or age=#{arg1}
当arg1和arg0换位置,会影响结果
不推荐使用,Map集合,
:List<Student> selectStudentByMap(Map<String,Object> map);
并不清楚Map的参数有几个,也不知道参数的类型
:select id,name ,email,age from student where name =#{myname} or age=#{myage}
通过key获取值,key值不具有统一性
List<Student> students=dao.queryStudent("李四");
==> Preparing: select id,name ,email,age from student where name =李四
数据是原样使用的,查询sql语句的时候会出错
需要加上‘ ’
List<Student> students=dao.queryStudent("’李四’");
ResultMap:用于类的属性和表的列名不一样时返回结果属性为空,通过ResultMap来定义类的属性和表的类的映射关系,就可以解决属性名和列值不同的问题
推荐使用这种方式,运用方便,只需要在java程序中提供不同的值就可以了
这种方式不够灵活,做变换查询的话,需要系应该mapper文件
If语句有时候会有问题,从上往下的判断,会导致某些条件可能成立,某些条件不成立,导致or或and多一个或者少一个之类的,从而导致sql语句错误
为了弥补sql语句语法错误一般在where 后面加上1=1 (id=-1)
手工实现循环
{
代码片段:
主配置文件
MyBatis分页: