1、Mybatis是什么
1、mybatis是一个ROM(对象关系映射)持久层框架。
2、Mybatis是对JDBC代码的封装,低层还是JDBC代码。
2、Mybatis的优势
1、sql语句与代码分离,存放于xml文件中,方便维护。
2、封装了JDBC的代码,减少代码量。
3、不用手动对结果集进行处理(LIst集合等)
3、Mybatis快速入门
3.1、添加依赖(pom.xml)
< dependencies>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.11</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.5</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.16</ version>
</ dependency>
</ dependencies>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
< include> **/*.yml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
3.2、文件目录:
3.2、基于XML文件开发
3.2.1、创建主配置文件:(resource文件下的xxx.xml))
< configuration>
< properties resource = " db.properties" />
< typeAliases>
< package name = " org.example.domain" />
</ typeAliases>
< environments default = " mysql" >
< environment id = " mysql" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url"
value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " org\example\dao\UserMapper.xml" />
</ mappers>
</ configuration>
3.2.2、配置映射文件(和接口类名一致):
<?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 = " org.example.dao.UserMapper" >
< select id = " SelectByCondition" resultType = " User" >
select * from Student
< where>
< if test = " id!=null and id!=' ' " >
and id=#{id}
</ if>
< if test = " email!=null and email!=' ' " >
and email=#{email}
</ if>
< if test = " name!=null and name!=' ' " >
and name=#{name }
</ if>
< if test = " age!=null and age!=' ' " >
and age =#{age}
</ if>
< if test = " score!=null and score!=' ' " >
< choose>
< when test = " score==' A' " >
and score >= 80
</ when>
< when test = " score==' B' " >
and score >= 60 and 80 > score
</ when>
< when test = " score==' C' " >
and 60 > score
</ when>
</ choose>
</ if>
</ where>
</ select>
< select id = " select" resultType = " User" >
select * from student
< where>
email like concat("%",trim(#{email}),"%")
</ where>
</ select>
</ mapper>
3.2.3、测试
InputStream in = Resources. getResourceAsStream ( "MybatisConfig.xml" ) ;
SqlSessionFactory build = new SqlSessionFactoryBuilder ( ) . build ( in) ;
SqlSession sqlSession = build. openSession ( false ) ;
UserMapper mapper = sqlSession. getMapper ( UserMapper. class ) ;
System. out. println ( mapper. SelectByCondition ( new User ( null, null, null, null, 'A' ) ) ) ;
sqlSession. commit ( ) ;
3.3、基于注解文件开发
3.3.1、创建主配置文件:(resource文件下的xxx.xml))
< configuration>
< properties resource = " db.properties" />
< typeAliases>
< package name = " org.example.domain" />
</ typeAliases>
< environments default = " mysql" >
< environment id = " mysql" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url"
value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper class = " org.example.dao.UserMapper" />
</ mappers>
</ configuration>
3.3.2、注解
@Insert : 插入sql , 和xml insert sql语法完全一样@Select : 查询sql, 和xml select sql语法完全一样
@Update : 更新sql, 和xml update sql语法完全一样
@Delete : 删除sql, 和xml delete sql语法完全一样
@Param : 入参
@Results : 设置结果集合@Result : 结果
@ResultMap : 引用结果集合
@SelectKey : 获取最新插入id
3.2.3、测试
InputStream in = Resources. getResourceAsStream ( "MybatisConfig.xml" ) ;
SqlSessionFactory build = new SqlSessionFactoryBuilder ( ) . build ( in) ;
SqlSession sqlSession = build. openSession ( false ) ;
UserMapper mapper = sqlSession. getMapper ( UserMapper. class ) ;
System. out. println ( mapper. SelectByCondition ( new User ( null, null, null, null, 'A' ) ) ) ;
sqlSession. commit ( ) ;