04-在SpringBoot项目中创建mybatis项目

9 篇文章 0 订阅

# # 在SpringBoot项目中创建mybatis项目多表查询

I.在pom.xml中添加依赖需要添加mysqlmybatis的依赖

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&amp;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>前面的orand
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程简介这是一门使用Java语言,SpringBoot框架,从0开发一个RESTful API应用,接近企业级的项目(我的云音乐),课程包含了基础内容,高级内容,项目封装,项目重构等知识,99%代码为手写;因为这是项目课程;所以不会深入到源码讲解某个知识点,以及原理,但会粗略的讲解下基础原理;主要是讲解如何使用系统功能,流行的第三方框架,第三方服务,完成接近企业级项目,目的是让大家,学到真正的企业级项目开发技术。适用人群刚刚毕业的学生想提高职场竞争力想学从零开发SpringBoot项目想提升SpringBoot项目开发技术想学习SpringBoot项目架构技术想学习企业级项目开发技术就是想学习SpringBoot开发能学到什么从0开发一个类似企业级项目学会能做出市面上90%通用API快速增加1到2年实际开发经验刚毕业学完后能找到满意的工作已经工作学完后最高涨薪30%课程信息全课程目前是82章,155小时,每节视频都经过精心剪辑。在线学习分辨率最高1080P课程知识点1~11章:学习方法,项目架构,编码规范,Postman使用方法,Git和Github版本控制12~16章:搭建开发环境,快速入门SpringBoot框架17~20章:快速入门MySQL数据库21~30章:MyBatis,登录注册,找回密码,发送短信,发送邮件,企业级接口配置31~41章:实现歌单,歌单标签,音乐,列表分页,视频,评论,好友功能42~48章:阿里云OSS,话题,MyBatis-plus,应用监控49~53章:Redis使用,集成Redis,SpringCache,HTTP缓存54~58章:Elasticsearch使用,集成Elasticsearch,使用ES搜索59~61章:商城,集成支付宝SDK,支付宝支付62~64章:常用哈希和加密算法,接口加密和签名65~67章:实时挤掉用户,企业级项目测试环境,企业级接口文档68~69章:SpringBoot全站HTTPS,自签证书,申请免费证书70~73章:云MySQL数据库,云Redis数据库使用,轻量级应用部署环境,域名解析74~80章:Docker使用,生产级Kubernetes集群,域名解析,集群全站HTTPS81~82章:增强和重构项目,课程总结,后续学习计划

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值