Mybatis

本文详细介绍了如何从零开始搭建 MyBatis,包括安装、配置数据库连接、创建映射类和表、编写 XML 映射文件、配置 conf.xml。深入讲解了 MyBatis 的动态 SQL、存储过程、延迟加载、缓存机制以及一对一和一对多关联查询。还涵盖了参数传值方式、别名配置、类型转换器等内容,为实际项目开发提供了全面的指导。
摘要由CSDN通过智能技术生成

Mybatis搭建

1、Mybatis 安装

2、第一个mybatis搭建
1、导入jar包
Mybatis-3.46.Jar
mysql-connector
2、创建映射类Student
3、创建映射表Student
4、创建personMapper.xml
细节1导入该配置xml路径:
细节2:查询到返回类型路径,和根据什么查询的类型
5、创建conf.xml
细节1:写入数据库连接
细节2:加载personMapper.xml查询文件
6、读入Reader reader=Resources.getResourceAsReader(“conf.xml”);
7、查询SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader) ;
//session - connection

SqlSession session = sessionFactory.openSession() ;
String statement = "org.lsj.entity.personMapper.queryPersonById" ;

3、conf.xml和personMapper.xml了解
数据库信息default设置

4、Statement方式的增删改查
1、personMaper.xml配置


select * from Student where id = #{id}



insert into student(id,name,age) values(#{id},#{name},#{age}} )



delete from student where id = #{id}



update student set age=#{age} ,name=#{name} where id=#{id}



select * from student

5、mapper动态的增删改查
1、StudentMapper.java 和studentMapper.xml
public interface StudentMapper与一致5
2、

6、mapper优化
1、属性文件db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/MyBatis
username=root
password=
在con.xml加入
2、全局参数(缓存)
3、别名(conf.xml里面配置)



4、类型转换器
1、studentMapper.xml



2、实现类继承
@Override
public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
// TODO Auto-generated method stub
int sexNum = cs.getInt(columnIndex) ;//rs.getInt(1)
return sexNum == 1?true:false ;
}
3、写增加数据库行
4、设置主键
5、测试
7、如果数据属性和eclipse属性没有相同
解决方法:

select * from student where id = #{id}







8、数据库传值#{} 和’ ′ p a r a m e t e r T y p e 解 决 方 法 : s e l e c t i d , n a m e , a g e f r o m s t u d e n t w h e r e n a m e = ′ {}'parameterType 解决方法:select id,name, age from student where name = ' parameterType:selectid,name,agefromstudentwherename={value}’

9、数据库传值#{} 和’ ′ 1 、 {}' 1、 1{} 好处降序排序

10、级联写法输入值

select id,name,age,sex from student
where homeAddress = #{address.homeAddress} or schoolAddress =’${address.schoolAddress}’

11、hashmap

select id,name,age from student
where age= #{age} or name like ‘%${name}%’

12、mybatis 存储过程
输入:1、查询个年龄的学生总数
输出:查询年纪的学生总数
create procedure pre_student( IN aage int, OUT scount INT)
begin
select count(1) into scount from student where age=aage;
end //

delimiter //
create procedure pre_name1( IN hadr varchar(10),OUT scount INT)
begin
select count(1) into scount from student where homeaddress=hadr ;
end //
解决方法:

{CALL pre_name1(#{hadr,jdbcType=VARCHAR,mode=IN},#{scount,jdbcType=INTEGER,mode=OUT}) }

13、根据Id删除学生

delimiter //
create procedure pre_deid( IN iid int)
begin
delete from student where id =iid;
end //
delimiter ;
解决方法:

	<!-- 通过存储过程实现删除 -->
<delete id="deleteStuBynoWithProcedure" statementType="CALLABLE" parameterType="HashMap">
	{
		CALL pre_deid(
			#{iid,jdbcType=INTEGER,mode=IN}
		) 
	}	
</delete>

14、输出参数resultType 和resultMap


select * from student where id = ${value}



select count(*) from student

15、动态sql

select * from student



#{id}




16、数组、集合、open: item : close :的使用

select * from student



#{student.id}



17、SQL片段





#{student.id}



<!-- 25、将多个元素值 放入对象数组中Student[] students = {student0,student1,student2}  每个studentx包含一个学号属性 -->
<select id="queryStudentsWithObjectArray"  parameterType="Object[]" resultType="student">
  	select * from student 
  	 <include refid="objectArrayStunos"></include>
</select>

18、关联查询
1、一对一 a、业务扩展类

解决方法
1、建表、建立外键
	 create table studentcard(carid int(20) primary key,cardinfo varchar(20));
 	alter tables student add carid varchar(20);
	alter table studentcard change carid cardid int(20);
	alter table studentcard modify cardid varchar(20);
	alter table student modify cardid varchar(20);
	alter table student add constraint fk_student_card foreign key(cardid) references studentcard(cardid);
	select s.*,c.* from student s join studentcard c on s.cardid =c.cardid  where s.id=#{id}
	<!-- 27、关联查询也就是多表查询 -->
	<select id="queryStudentByNoWithOO" 	parameterType="int"  	resultType="StudentBusiness" >
	select student.*,studentcard.* from student ,studentcard where student.cardid=studentcard.cardid and student.id=#{id}
	</select>	
利用resultMap
<select id="queryStudentByNoWith002" 	parameterType="int"  	resultMap="studentcardmap" >
		<!-- select * from student where id=#{id} -->
		<!-- select student.*,studentcard.* from student,studentcard where student.cardid =student.cardid and student.id=#{id} -->
		select student.*,studentcard.* from student ,studentcard where student.cardid=studentcard.cardid and student.id=#{id}
	</select>
	<resultMap type="student" id="studentcardmap">
		<!-- 学生信息 -->
		<id property="id"  column="id"  />
		<result property="name"  column="name" />
		<result property="age"  column="age" />
		<!-- 一对一映射, -->
		<association property="card" javaType="StudentCard">
			<id property="cardid"  column="cardid"  />
			<result property="cardinfo"  column="cardinfo" />
		</association>
</resultMap>
2、一对多
alter table student add constraint fk_student_class foreign key(classid) references studentclass(classid);
select student.*,studentclass.* from student ,studentclass where student.classid=studentclass.classid and studentclass.classid=1;

19、延迟加载
1、配置



select * from studentCard where cardid = #{cardId}

2、加入.conf.xml配置
<mapper resource="org/lsj/mapper/studentCardMapper.xml"/>
3、开启延迟加载
<settings>
	<!-- 开启延迟加载 -->
	<setting name="lazyLoadingEnabled" value="true"/>
	
	<!-- 关闭立即加载 -->
	<setting name="aggressiveLazyLoading" value="false"/>
</settingfs>
4、
	<!-- 30、延迟加载 -->
<select id="queryStudentWithOO2LazyLoad" 	parameterType="int"  	resultMap="student_card_lazyLoad_map" >
	<!-- 先查学生 -->
	select * from student 
</select>
<resultMap type="student" id="student_card_lazyLoad_map">
		<!-- 学生的信息 -->
		<id  property="id" column="id"/>
		<result property="name" column="name" />
		<result property="age" column="age" />
	
		<!-- 学生证  ,通过select 在需要的时候再查学生证 -->
		<association property="card" javaType="StudentCard"  select="org.lsj.mapper.StudentCardMapper.queryCardById"  column="cardid"  >
			<!-- <id property="cardId" column="cardId"/>             
				<result property="cardInfo" column="cardInfo"/>   -->	
		</association>
	</resultMap>

20、查询一级缓存、二级缓存

21、禁用缓存

22、整合Ehcache缓存

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金石不渝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值