Mybatis学习(一)--基础使用学习

mybatis是用来封装数据库操作的框架
1、总体配置,SqlMapConfig.xml
第一步是配置SqlMapConfig.xml,这是mybatis的总体配置,最基础的是配置environments,这个文件里面包括了数据库的信息和一些对框架基本的配置

<?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="default">
		<environment id="default">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>
    <mappers>
    		<mapper resource="mapper/FlowerMapper.xml" />
    </mappers>
</configuration>

2、配置mapper.xml文件
mapper文件是用来存储sql语句的文件,可以理解为是一个个的方法,只不过是用xml格式来写的。
mapper如下:

<mapper namespace="mapper.FlowerMapper">
	<select id="selectAll" resultType="fl">
		select * from flower
 	</select>
 	<select id="selectById" resultType="fl" parameterType="Map">
		select * from flower where id=#{id1} or id=#{id2}
 	</select>
 	<insert id="insertNew" parameterType="fl">
 		insert into flower values(default,#{name},#{price},#{production})
 	</insert>
</mapper>

namespace可以理解为包名,用于区分不同mapper文件中的方法;

<select>标签是表示查询的标签,id是这个方法的名字,resultType表示的是这个方法的返回值,parameterType表示的是这个方法的参数的数值类型,<select>中包裹的就是要运行的sql语句

在配置完mapper文件之后,需要将这个mapper文件添加到主配置文件里面,因此需要在SqlMapConfig.xml中添加以下代码:

<mappers>
		<mapper resource="mapper/FlowerMapper.xml" />
</mappers>

注意:SqlMapConfig.xml中的标签是有顺序的,不能随便插入

3、进行查询
代码:

		InputStream ins=Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(ins);
		SqlSession sqlsession=sqlSessionFactory.openSession();//sqlsession是用来执行sql语句的
		
		//返回一个List
		List<Flower> list=sqlsession.selectList("mapper.FlowerMapper.selectAll");
		for(Flower f:list){
			logger.info(f.toString());
		}
		
		
		//返回一个结果或一个对象
		int a=sqlsession.selectOne("mapper.FlowerMapper.selectAllCount");
		logger.info(a); 
		
		
		//返回一个Map,name作为key
		Map<Object,Object> map=sqlsession.selectMap("mapper.FlowerMapper.selectAllMap", "name");
		
		//只能传入一个对象作为参数,如果希望传入多个参数,使用对象或map
		Map<String,Object> map1=new HashMap<>();
		map1.put("id1", 1);
		map1.put("id2", 8);
		Flower f=sqlsession.selectOne("mapper.FlowerMapper.selectById",map1);
		logger.info(f.toString());
		
		
		
		//增加记录,必须记得提交事务,mybatis默认不自动提交事务
		Flower f1=new Flower();
		f1.setName("太阳花");
		f1.setPrice(9);
		f1.setProduction("ab");
		try {
			int index=sqlsession.insert("mapper.FlowerMapper.insertNew", f1);
			if(index>0) {
				System.out.println("successed!");
			}else {
				System.out.println("failed!");
			}
		} catch (Exception e) {
			//如果出现异常,事务回滚,只能回滚到上一次提交事务的地方
			sqlsession.rollback();
		}
		//这里即使输出成功,如果不提交事务,数据库中实际上也没有添加新的记录
		sqlsession.commit();
	}

sqlsession是用来执行sql语句的,也就是执行mapper文件中的方法,其中有三个用来执行查询的方法,

  1. selectOne:返回一个值,一般用于查询返回一个对象的方法
  2. selectList:返回一个list
  3. selectMap:返回一个map,同时需要传入另一个参数做key,这个参数是所返回的类的一个属性

由于mybatis默认是不自动提交事务的,所以在使用增删改的sql操作时,都需要在最后增加sqlsession.commit();,否则数据库中不会出现新增的数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值