什么是mybatis
https://mybatis.net.cn/index.html
mybatis是一个用Java编写的持久层框架,它使用ORM实现了结果集的封装。
ORM是Object Relational Mapping 对象关系映射。简单来说,就是把数据库表和实体类及实体类的属性对应起来,让开发者操作实体类就实现操作数据库表,它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等复杂过程。
ORM:Object-Relation-Mapping,也就是对象关系映射,是一种程序设计思想,mybatis就是ORM的一种实现方式,简单来说就是将数据库中查询出的数据映射到对应的实体中。
数据库层框架
1.com.mayikt.servlet或者com.mayikt.controller————控制层 springmvc
2.com.mayikt.service—-业务逻辑层
3.com.mayikt.dao——数据库访问层 hibernate或者mybatis、jpa
1.数据库连接相关配置
2.编写sql语句 jdbc 查询操作 单独取出每个值 在赋值给我们对象
mybatis、springmvc、springboot
使用mybatis orm java中 对象与数据库中表中 字段 对应
底层通过反射机制自动赋值
mybatis快速入门
数据库表结构
CREATE TABLE `mayikt_users` (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb3;
1.引入mybatis相关依赖 已经完成了
2.mybatis-config.xml(该配置文件名称是可以改) 存放就是我们数据库相关连接信息
3.定义mapper ——编写我们mybatis 相关 sql语句 每个表 对应一个mapper
4.定义java对象—需要注意下 类中的 成员属性与数据库表中字段 映射 默认 类中的 成员属性数据库表中字段名称对应的。
5.使用 mybatis api开始执行该 sql语句即可 得到结果
maven依赖
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency>
定义xml配置文件
存放数据库连接信息mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/mayikt?serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="mybatis/userMaaper.xml"/></mappers></configuration>
Maaper文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="userMapper"><select id="getByUsers" resultType="com.mayikt.entity.UserEntity">select * from mayikt_users</select></mapper>
测试代码
package com.mayikt.test;import com.mayikt.entity.UserEntity;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.InputStream;import java.util.List;/*** @author 余胜军* @ClassName Test01* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public class Test01 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xmlString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取到获取到SqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据 mapper id=getByUsers 执行该s ql 语句 通过 sql语句得到我们的对象 ormList<UserEntity> userEntitys = sqlSession.selectList("getByUsers", UserEntity.class);System.out.println(userEntitys);sqlSession.close();}}
package com.mayikt.test;import com.mayikt.entity.UserEntity;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.InputStream;import java.util.List;/*** @author 余胜军* @ClassName Test01* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public class Test01 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xmlString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取到获取到SqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据 mapper id=getByUsers 执行该s ql 语句 通过 sql语句得到我们的对象 ormList<UserEntity> userEntitys = sqlSession.selectList("getByUsers", UserEntity.class);System.out.println(userEntitys);sqlSession.close();}}
mapper代理开发模式
1.mapper接口方式开发整合就必须是对应的mapper接口的全限定类名
2.接口中的方法与映射文件中的SQL语句的ID
3.需要在mybatis-config.xml 新增 加载该userMaaper
<mappers><mapper resource="mybatis/userMapper.xml"/></mappers>
4.定义mapper 接口 需要考虑方法的名称与userMapper.xml的 sql id名称保持一致。
package com.mayikt.mapper;import com.mayikt.entity.UserEntity;import java.util.List;/*** @author 余胜军* @ClassName UserMapper* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public interface UserMapper {List<UserEntity> getByUsers();}<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.mayikt.mapper.UserMapper"><select id="getByUsers" resultType="com.mayikt.entity.UserEntity">select * from mayikt_users</select></mapper>
5.相关代码
package com.mayikt.test;import com.mayikt.entity.UserEntity;import com.mayikt.mapper.UserMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.InputStream;import java.util.List;/*** @author 余胜军* @ClassName Test01* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public class Test01 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xmlString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取到获取到SqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据 mapper id=getByUsers 执行该s ql 语句 通过 sql语句得到我们的对象 orm// List<UserEntity> userEntitys = sqlSession.selectList("getByUsers", UserEntity.class);UserMapper mapper = sqlSession.getMapper(UserMapper.class);System.out.println(mapper.getByUsers());// System.out.println(userEntitys);sqlSession.close();}}
mybatis增删改查
CREATE TABLE `mayikt_flight` (`id` int NOT NULL AUTO_INCREMENT COMMENT 'id列',`flight_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '航号',`company` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '航空公司',`departure_airport` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '出发机场',`arrive_airport` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '达到机场',`departure_time` datetime DEFAULT NULL COMMENT '出发时间',`arrive_time` datetime DEFAULT NULL COMMENT '到达时间',`model` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '机型',`is_delete` int DEFAULT NULL COMMENT '是否隐藏0显示 1隐藏',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb3;
查询所有数据
package com.mayikt.entity;import java.util.Date;/*** @author 余胜军* @ClassName FlightEntity* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public class FlightEntity {private Integer id;private String flightId;private String company;private String departureAirport;private String arriveAirport;private Date departureTime;private Date arriveTime;private String model;private Integer isDelete;@Overridepublic String toString() {return "FlightEntity{" +"id=" + id +", flightId='" + flightId + '\'' +", company='" + company + '\'' +", departureAirport='" + departureAirport + '\'' +", arriveAirport='" + arriveAirport + '\'' +", departureTime=" + departureTime +", arriveTime=" + arriveTime +", model='" + model + '\'' +", isDelete=" + isDelete +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getFlightId() {return flightId;}public void setFlightId(String flightId) {this.flightId = flightId;}public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}public String getDepartureAirport() {return departureAirport;}public void setDepartureAirport(String departureAirport) {this.departureAirport = departureAirport;}public String getArriveAirport() {return arriveAirport;}public void setArriveAirport(String arriveAirport) {this.arriveAirport = arriveAirport;}public Date getDepartureTime() {return departureTime;}public void setDepartureTime(Date departureTime) {this.departureTime = departureTime;}public Date getArriveTime() {return arriveTime;}public void setArriveTime(Date arriveTime) {this.arriveTime = arriveTime;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}public Integer getIsDelete() {return isDelete;}public void setIsDelete(Integer isDelete) {this.isDelete = isDelete;}}package com.mayikt.mapper;import com.mayikt.entity.FlightEntity;import java.util.List;/*** @author 余胜军* @ClassName FlightMapper* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public interface FlightMapper {/*** 查询* 1.查询所有* 2.根据条件查询* 3.动态查询方式*/List<FlightEntity> getByFlightAll();}package com.mayikt.service;import com.mayikt.entity.FlightEntity;import com.mayikt.mapper.FlightMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.InputStream;import java.util.List;/*** @author 余胜军* @ClassName FlightService* @qq 644064779* @addres www.mayikt.com* 微信:yushengjun644*/public class FlightService {private FlightMapper flightMapper;public FlightService() throws IOException {// 通过无参构造方法 初始化mybatis 得到flightMapper// mybatis-config.xml 目录位置String resource = "mybatis-config.xml";// 1.解析mybatis-config.xml 得到数据库相关的配置信息InputStream inputStream = Resources.getResourceAsStream(resource);//2.创建得到一个sqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//3.获取到sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();flightMapper = sqlSession.getMapper(FlightMapper.class);// sqlSession.close();}}<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><!-- 数据库相关的配置--><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/flight?serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="mybatis/flightMapper.xml"/></mappers></configuration><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace=--><mapper namespace="com.mayikt.mapper.FlightMapper"><select id="getByFlightAll" resultType="com.mayikt.entity.FlightEntity">select *from mayikt_flight;</select></mapper>
解决数据库与类中成员属性不一致性
方式1:
使用 sql语句 as的方法 代码会非常重复。
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace=--><mapper namespace="com.mayikt.mapper.FlightMapper"><select id="getByFlightAll" resultType="com.mayikt.entity.FlightEntity">select id as id,flight_id as flightId,company as company, departure_airport as departureAirport,arrive_airport as arriveAirport, departure_time as departureTime,arrive_time as arriveTime,model as model,is_delete asisDeletefrom mayikt_flight;</select></mapper>
方式2:
resultMap 定义数据库表中字段名称与类中成员属性名称 关联映射
数据库字段:flight_id——类中成员名称 flightId
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace=--><mapper namespace="com.mayikt.mapper.FlightMapper"><resultMap id="flightEntityMap" type="com.mayikt.entity.FlightEntity"><!-- 数据库中字段名称 column="" property="id" 类中成员属性名称--><id column="id" property="id"></id><result column="flight_id" property="flightId"></result><result column="departure_airport" property="departureAirport"></result><result column="departure_time" property="departureTime"></result><result column="arrive_time" property="arriveTime"></result><result column="model" property="model"></result><result column="is_delete" property="isDelete"></result></resultMap><select id="getByFlightAll" resultType="com.mayikt.entity.FlightEntity">select id as id,flight_id as flightId,company as company, departure_airport as departureAirport,arrive_airport as arriveAirport, departure_time as departureTime,arrive_time as arriveTime,model as model,is_delete asisDeletefrom mayikt_flight;</select><!-- 定义数据库中字段名称与我们 类中成员属性值 关联映射--><select id="getByFlightAll2" resultMap="flightEntityMap">select * from mayikt_flight where id=10;</select></mapper>
id查询数据
/*** 就是根据主键id查询数据*/FlightEntity getByIdFlight(Integer id);public FlightEntity getByIdFlight(Integer id) {return flightMapper.getByIdFlight(id);}<!--parameterType int string Double 自定义对象类型 有处理防止sql语句攻击功能--><select id="getByIdFlight" parameterType="int" resultMap="flightEntityMap">select * from mayikt_flight where id=#{id};</select>
插入数据
/*** 插入数据的结果 如果影响行数 大于0 成功的 否则 就是失败的** @param FlightEntity FlightEntity* @return*/int insertFlight(FlightEntity flightEntity);private FlightMapper flightMapper;private SqlSession sqlSession;public FlightService() throws IOException {// 通过无参构造方法 初始化mybatis 得到flightMapper// mybatis-config.xml 目录位置String resource = "mybatis-config.xml";// 1.解析mybatis-config.xml 得到数据库相关的配置信息InputStream inputStream = Resources.getResourceAsStream(resource);//2.创建得到一个sqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//3.获取到sqlSessionsqlSession = sqlSessionFactory.openSession();flightMapper = sqlSession.getMapper(FlightMapper.class);// sqlSession.close();}public List<FlightEntity> getByFlightAll() {return flightMapper.getByFlightAll();}public FlightEntity getByIdFlight(Integer id) {return flightMapper.getByIdFlight(id);}public int insertFlight(FlightEntity flightEntity) {int result = flightMapper.insertFlight(flightEntity);// 需要提交事务事务的sqlSession.commit();// 提交事务return result;}<!--select标签查询数据insert标签 插入数据--><insert id="insertFlight" parameterType="com.mayikt.entity.FlightEntity">INSERT INTO `flight`.`mayikt_flight` (`id`, `flight_id`, `company`, `departure_airport`, `arrive_airport`, `departure_time`, `arrive_time`, `model`, `is_delete`)VALUES (null, #{flightId}, #{company}, #{departureAirport},#{arriveAirport}, #{departureTime},#{arriveTime},#{model}, #{isDelete});</insert>
修改数据
int updateFlight(FlightEntity flightEntity);public int updateFlight(FlightEntity flightEntity) {int result = flightMapper.updateFlight(flightEntity);// 需要提交事务事务的 数据增加 update 删除sqlSession.commit();// 提交事务return result;}<update id="updateFlight" parameterType="com.mayikt.entity.FlightEntity">UPDATE `flight`.`mayikt_flight` SET `id`=#{id}, `flight_id`=#{flightId}, `company`=#{company},`departure_airport`=#{departureAirport},`arrive_airport`=#{arriveAirport}, `departure_time`=#{departureTime},`arrive_time`=#{arriveTime}, `model`=#{model}, `is_delete`=#{isDelete} WHERE (`id`=#{id});</update>
删除数据
int deleteByIdFlight(Integer id);public int deleteByIdFlight(Integer id) {int result = flightMapper.deleteByIdFlight(id);// 需要提交事务事务的 数据增加 update 删除sqlSession.commit();// 提交事务return result;}<delete id="deleteByIdFlight" parameterType="int">delete from mayikt_flight where id=#{id};</delete>
MyBatis - 映射文件标签
映射文件的顶级元素
select:映射查询语句
insert:映射插入语句
update:映射更新语句
delete:映射删除语句
sql:可以重用的 sql 代码块
resultMap:最复杂,最有力量的元素,用来描述如何从数据库结果集中加载你的对象
cache:配置给定命名空间的缓存
cache-ref:从其他命名空间引用缓存配置
select 标签的属性信息
<select<!--1. id(必须配置)id是命名空间中的唯一标识符,可被用来代表这条语句一个命名空间(namespace)对应一个dao接口这个id也应该对应dao里面的某个方法(sql相当于方法的实现),因此id应该与方法名一致-->id="selectUser"<!--2. parapeterType(可选配置,默认由mybatis自动选择处理)将要传入语句的参数的完全限定名或别名,如果不配置,mybatis会通过ParamterHandler根据参数类型默认选择合适的typeHandler进行处理paramterType 主要指定参数类型,可以是int, short, long, string等类型,也可以是复杂类型(如对象)-->parapeterType="int"<!--3. resultType(resultType 与 resultMap 二选一配置)用来指定返回类型,指定的类型可以是基本类型,也可以是java容器,也可以是javabean-->resultType="hashmap"<!--4. resultMap(resultType 与 resultMap 二选一配置)用于引用我们通过 resultMap 标签定义的映射类型,这也是mybatis组件高级复杂映射的关键-->resultMap="USER_RESULT_MAP"<!--5. flushCache(可选配置)将其设置为true,任何时候语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false-->flushCache="false"<!--6. useCache(可选配置)将其设置为true,会导致本条语句的结果被二级缓存,默认值:对select元素为true-->useCache="true"<!--7. timeout(可选配置)这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数,默认值为:unset(依赖驱动)-->timeout="10000"<!--8. fetchSize(可选配置)这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为:unset(依赖驱动)-->fetchSize="256"<!--9. statementType(可选配置)STATEMENT, PREPARED或CALLABLE的一种,这会让MyBatis使用选择Statement, PrearedStatement或CallableStatement,默认值:PREPARED-->statementType="PREPARED"<!--10. resultSetType(可选配置)FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为:unset(依赖驱动)-->resultSetType="FORWORD_ONLY"></select>
resultMap 标签的属性信息
<!--1. type 对应的返回类型,可以是javabean, 也可以是其它2. id 必须唯一, 用于标示这个resultMap的唯一性,在使用resultMap的时候,就是通过id引用3. extends 继承其他resultMap标签--><resultMap type="" id="" extends=""><!--1. id 唯一性,注意啦,这个id用于标示这个javabean对象的唯一性, 不一定会是数据库的主键(不要把它理解为数据库对应表的主键)2. property 属性对应javabean的属性名3. column 对应数据库表的列名(这样,当javabean的属性与数据库对应表的列名不一致的时候,就能通过指定这个保持正常映射了)--><id property="" column=""/><!--result 与id相比,对应普通属性--><result property="" column=""/><!--constructor 对应javabean中的构造方法--><constructor><!-- idArg 对应构造方法中的id参数 --><idArg column=""/><!-- arg 对应构造方法中的普通参数 --><arg column=""/></constructor><!--collection 为关联关系,是实现一对多的关键1. property 为javabean中容器对应字段名2. ofType 指定集合中元素的对象类型3. select 使用另一个查询封装的结果4. column 为数据库中的列名,与select配合使用--><collection property="" column="" ofType="" select=""><!--当使用select属性时,无需下面的配置--><id property="" column=""/><result property="" column=""/></collection><!--association 为关联关系,是实现一对一的关键1. property 为javabean中容器对应字段名2. javaType 指定关联的类型,当使用select属性时,无需指定关联的类型3. select 使用另一个select查询封装的结果4. column 为数据库中的列名,与select配合使用--><association property="" column="" javaType="" select=""><!--使用select属性时,无需下面的配置--><id property="" column=""/><result property="" column=""/></association></resultMap>
insert 标签的属性信息
<insert<!--同 select 标签-->id="insertProject"<!--同 select 标签-->paramterType="projectInfo"<!--1. useGeneratedKeys(可选配置,与 keyProperty 相配合)设置为true,并将 keyProperty 属性设为数据库主键对应的实体对象的属性名称-->useGeneratedKeys="true"<!--2. keyProperty(可选配置,与 useGeneratedKeys 相配合)用于获取数据库自动生成的主键-->keyProperty="projectId">
重用 sql 标签
完全限定名使用别名替代
每个 sql 映射文件的要元素中,都需要指定一个名称空间,用以确保每个映射语句的 id 属性不会重复。如
在 Java 代码中引用某个 sql 映射时,使用的亦是含有名称空间的全路径。如
session.update(“com.mayikt.mapper.UserMapper.udpateUser”, user);
mybatis多条件查询
mybatis多条件查询
第一种方法 传递map 型;
第二种方法:多个参数如果不封装成Map 参数值需要通过,多个参数的时候要使用 @Param 给指定参数,否则会出现找不到参数的错误
第三种方法:传递pojo ; 非常多参数 sql语句中获取参数值名称 与对象成员属性名称需要保持一致
List<FlightEntity> getByIdFlightParameterMap(Map<String, String> parameterMap);List<FlightEntity> getByIdFlightParameter(@Param("company") String company,@Param("departureAirport") String departureAirport,@Param("arriveAirport") String arriveAirport);List<FlightEntity> getByIdFlightPoJo(FlightEntity flightEntity);
<select id="getByIdFlightParameterMap" resultMap="flightEntityMap">SELECT * from mayikt_flight where company=#{company}and departure_airport=#{departureAirport} and arrive_airport=#{arriveAirport};</select><select id="getByIdFlightParameter" resultMap="flightEntityMap">SELECT * from mayikt_flight where company=#{company}and departure_airport=#{departureAirport} and arrive_airport=#{arriveAirport};</select><select id="getByIdFlightPoJo" parameterType="com.mayikt.entity.FlightEntity" resultMap="flightEntityMap">SELECT * from mayikt_flight where company=#{company}and departure_airport=#{departureAirport} and arrive_airport=#{arriveAirport};</select>
mybatis动态条件查询
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。
使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。
如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
mysql 加上输出日志
<settings><!-- 打印sql日志 --><setting name="logImpl" value="STDOUT_LOGGING" /></settings><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><!-- 数据库相关的配置--><configuration><settings><!-- 打印sql日志 --><setting name="logImpl" value="STDOUT_LOGGING" /></settings><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/flight?serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="mybatis/flightMapper.xml"/></mappers></configuration>

