# # 在SpringBoot项目中创建mybatis项目
及多表查询
I.在pom.xml
中添加依赖需要添加mysql
和mybatis
的依赖
1).mysql的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2).mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
# II.在src/main/resources
中编写mybatis
项目的配置文件(只需要一次编写)
<?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>
<!-- 环境配置 -->
<environments default="test">
<environment id="test">
<!-- 事务:JDBC -->
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/jtdb-small?characterEncoding=utf8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root" />
<property name="password" value="qpk737421" />
</dataSource>
</environment>
</environments>
<!-- 告诉mybatis,映射文件存在 -->
<mappers>
<mapper resource="mappers/ItemMapper.xml"/>
</mappers>
</configuration>
# III.在src/main/resources
创建文件夹mappers
来存放mybatis的xml文件
# IV.在src/main/resources/mappers
文件夹中创建ItemMapper.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 namespace="cn.tedu.jt.mapper.ItemMapper">
<!-- id属性,必须在这个命名空间下唯一 resultType 返回值类型,如果集合List,只写集合元素中元素类型 -->
<!-- 定义映射规则,创建ResultMap标签,id
type类型 pojo对象
id就是一个命名在这个文件中唯一
-->
<resultMap type="cn.tedu.jt.pojo.Item" id="itemRM">
<!-- 写映射规则
property 值POJO对象id属性
column 为数据库属性
告诉mybatis转换时,把数据库字段换成pojo字段
-->
<id property="id" column="id"/>
<result property="sellPoint" column="sell_point"/>
</resultMap>
<!-- 需要一个参数:parameterType参数 string s小写或者java.lang.String-->
<select id="find" resultMap="itemRM" parameterType="string">
select
*
from
tb_item
where
title
like
#{title}
</select>
<!--
获取一条记录(id)
resultMap返回一条,写元素类型itemRM
-->
<select id="get" parameterType="long" resultMap="itemRM">
select
*
from
tb_item
where
id=#{id}
</select>
<!-- <select id="find" resultType="cn.tedu.jt.pojo.Item">
SELECT t.sell_point sellPoint,t.* from tb_item t
</select> -->
<!-- 获取记录总数,结果是一个值 -->
<select id="count" resultType="int">
select
count(*)
from
tb_item
</select>
<!-- 新增 -->
<!-- 如果有多个值要传递进来,传入pojo对象,参数的类型就是pojo对象
对象方式接收参数时,它的名称必须和pojo的属性名称一样
-->
<insert id="insert" parameterType="cn.tedu.jt.pojo.Item">
insert
into
tb_item
(id,title,sell_point,price,num,cid,status,created,updated)
values
<!-- 这个是java是添写的,跟数据库无关,必须跟pojo实体类一样 -->
(null,#{title},#{sellPoint},#{price},#{num},${cid},1,now(),now())
</insert>
<update id="update" parameterType="cn.tedu.jt.pojo.Item">
update
tb_item
set
title=#{title},sell_point=#{sellPoint},
price=#{price},num=#{num},
cid=#{cid},status=#{status},
updated=now()
where
id=#{id};
</update>
<delete id="delete" parameterType="long">
delete
from
tb_item
where
id=#{id};
</delete>
</mapper>
# V.在cn.tedu.jt.mapper
文件夹创建接口ItemMapper
来和步骤IV中的ItemMapper.xml
相应
package cn.tedu.jt.mapper;
import java.util.List;
import cn.tedu.jt.pojo.Item;
public interface ItemMapper {
// <select id="find" resultMap="itemRM">
// select * from tb_item
//</select>
// title跟xml文件要一样
public List<Item>find(String title);
public Item get(Long id);
public Integer count();
public Integer insert(Item item);
public Integer update(Item item);
public void delete(Long id);
}
# # 问题
I).xml文件中的命名空间
<mapper namespace="cn.tedu.jt.mapper.ItemMapper">
的namespace要和接口的路径一样
II).查询到的数据列为null是什么原因
问题:要是数据库表字段和pojo实体类不一样怎么办
eG:数据库中为sell_poin
而pojo实体类字段为sellPoin
驼峰命名
解决:
<resultMap type="cn.tedu.jt.pojo.Item" id="itemRM">
<!-- 写映射规则
property 值POJO对象id属性
column 为数据库属性
告诉mybatis转换时,把数据库字段换成pojo字段
-->
<id property="id" column="id"/>
<result property="sellPoint" column="sell_point"/>
</resultMap>
把原本的参数resultType='cn.tedu.jt.pojo.Item'
改写为resultMap='item'
<!-- 需要一个参数:parameterType参数 string s小写或者java.lang.String-->
<select id="find" resultMap="itemRM" parameterType="string">
select
*
from
tb_item
where
title
like
#{title}
</select>
III).参数对象parameterType
中参数int long要需要首字母小写,数据库规范
或者写java类的全路径名cn.tedu.jt.pojo.Item
IV).<where>
会自动自动拼接和删除<if>
前面的or
和and
V).<if>
是判断传入的参数是否为null时使用,通常为<if test="sellPoint !=null">
使用
Ⅵ).<set>
会自动删除最后一个,
Ⅶ).<foreach>
的使用
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
@Test
public void deleteMuch() {
SqlSession session=factory.openSession();
ItemMapper mapper=session.getMapper(ItemMapper.class);
Map<String,Object> map=new HashMap<String, Object>();
Long [] para= {1L,2L,3L};
map.put("ids",para);
mapper.deleteMuch(map);
session.commit();
System.out.println("删除成功");
}
ids就是map(key) id为元素,item是参数,open是开始,separator是分割符
Ⅷ).<sql>
和<include>
的使用
<sql id="cols">
id,title,sell_point,price,num,cid,status,created,updated
</sql>
select
<include refid="cols"/>
from
Ⅸ).<typeAliases>
的使用(在sqlMapConfig.xml)配置文件中配置全局变量
<!-- 别名,package配置一个通用的,这个目录下的所有POJO都设置别名,类名(大写) -->
<typeAliases>
<package name="cn.tedu.jt.pojo" />
</typeAliases>
# mybatis的多表连用
- 一对多连接(collection)
- 一对一连接(association)
# I.一对多连接
<resultMap type="cn.tedu.pojo.ItemCat" id="ItemCatItemRM">
<!-- 第一部分:是自己的数据,property POJO的属性,column 结果集字段名 -->
<id property="id" column="cid"/>
<result property="name" column="name"/>
<!-- 第二部分:关联商品表的数据(对多),集合,元素类型
java对象关联ItemCat和Item关联,私有属性
private List<Item> items;
ofType和collection是固定搭配
-->
<collection property="items" ofType="cn.tedu.pojo.Item">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="price" column="price"/>
</collection>
</resultMap>
<select id="findItemCat" resultMap="ItemCatItemRM">
SELECT
c.id as cid, c.name,
t.id, t.title, t.price
FROM
tb_item_cat c I
NNER JOIN
tb_item t
ON
c.id = t.cid
</select>
# II.一对一连接
<resultMap type="Item" id="ItemItemDescRM">
<!-- 自身属性配置 -->
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="price" column="price"/>
<!-- 关联关系:对一,assocaition POJO的关联对象名称
private ItemDesc itemDesc;
-->
<association property="itemDesc">
<id property="itemId" column="item_id"/>
<result property="itemDesc" column="item_desc"/>
</association>
</resultMap>
<select id="findItem" resultMap="ItemItemDescRM">
SELECT
t.id,t.title,t.price,
d.item_id,d.item_desc
FROM
tb_item t
JOIN
tb_item_desc d
ON
t.id = d.item_id
</select>