一、Mybatis开发环境搭建
1、什么是MyBatis
是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
2、MyBatis的优点及缺点
优点:
1、简单易学
2、灵活
3、解除sql与程序代码的耦合
缺点:
1、编写SQL语句时工作量很大,尤其是字段多、关联表多时,更是如此。
2、SQL语句依赖于数据库,导致数据库移植性差,不能更换数据库。
3、框架还是比较简陋,功能尚有缺失,
3、加载原理
MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置,配置来源于两个地方,一处是配置映射文件,一处是Java代码的注解,获取一个SqlSession。SqlSession包含了执行sql所需要的所有方法,可以通过SqlSession实例直接运行映射的sql语句,完成对数据的增删改查和事务提交等,用完之后关闭SqlSession。(图示)
4、核心接口对象
SqlSessionFactory 会话工厂对象
SqlSession 对应着一次数据库会话,用于执行持久化操作的对象,类似于jdbc中的Connection
5、环境搭建
1、添加相应的jar包 (mybatis jdbc)
2、src(maven下面是resources)添加Mybatis的配置文件conf.xml (数据库连接属性文件*.properties【可选】)
3、实体类映射
4、定义操作表的sql映射文件*Mapper.xml【mavens是在resources下面】 (返回结果可以使用resultType[指向具体类型] 或resultMap[指向一个resultMap的id号])
<!-- resultType="Map" resultType="hashmap" resultType="com.pojo.User" -->
5、在conf.xml文件中注册*Mapper.xml文件
6、测试(SqlSession实例来直接执行已映射的SQL语句)
6、输出sql语句设置(java工程可以测试,web工程要在servlet里面)
1、加入log4j.jar包(经过测试,如果有slf4j-api会冲突,导致不输出mybatis调试)
2、src目录(maven是在resources目录)加入log4j.properties属性文件
7、实现增删改查操作:
- 实现查询
- 根据主键查询返回一个Bank对象(resultType="com.pojo.Bank")
<!--根据主键查询,返回一个对象-->
<select id="selectbankuserbyid" resultType="com.pojo.BankUser" parameterType="int">
select id,name,tel,money from bankuser where id= #{id}
</select>
- 根据主键查询返回一个map对象(resultType="hashmap")
session.selectOne(sqlid, 4);
<!-- 查询结果返回的是一个map集合对象 -->
<select id="selmapById" resultType="map">
select id,username from bank
where id=1
</select>
3、返回一个Bank对象(使用resultMap="bankmap"指向一个resultMap的id)
<resultMap type="com.bean.Bank" id="selbankByIdresultMap">
<!-- column=""查询语句里面对应列的名字 -->
<!-- property="" 对应的是type="com.bean.Bank"对象里面属性的名字 -->
<!-- bank.setId(rs.getInt("id")) -->
<id column="id" property="id" />
<result column="username" property="username"/>
</resultMap>
<!-- 查询结果使用resultMap封装一个对象 -->
<!-- resultMap="" 必须是一个resultMap标签的id,对应的也是查询结果一行的类型 -->
<select id="selbankByIdrm" resultMap="selbankByIdresultMap">
select id,username from
bank
where id=1
</select>
4、返回一个Map对象(使用resultMap="bankmap"指向一个resultMap的id)
<!-- **************开始************* -->
<resultMap type="map" id="selbankByIdrmmapresultMap">
<!-- map.put("sdfghjkl",rs.getIn("id")) -->
<id column="id" property="sdfghjkl" />
<result column="username" property="usernamemap" />
</resultMap>
<!-- 查询结果使用resultMap封装一个map -->
<select id="selbankByIdrmmap" resultMap="selbankByIdrmmapresultMap">
select id,username from
bank
where id=1
</select>
<!-- **************结束************* -->
5、返回list集合对象(集合里面可以是hashmap 或bank对象)[映射文件select语句及配置和返回一个对象一致,只是查询的方法不一致]
session.selectList
6、查询参数设置为基本类型查询
<!-- 查询条件为基本类型 -->
<select id="selbankwherea" resultType="com.bean.Bank" parameterType="int">
<!-- #{id},#{ldjlfjdl} 如果是一个基本类型参数,可以任意写 -->
select id,username from bank where id < #{id}
</select>
7、参数设置为对象查询
<!-- 查询条件如果有多个,封装为对象参数进行查询 -->
<select id="selbankwhereb" resultType="com.bean.Bank"
parameterType="com.bean.Bank">
<!-- #{id},#{username} 必须是parameterType="com.bean.Bank"对象里面属性,且有get、set -->
select id,username from bank where id < #{id} and username= #{username}
</select>
8、参数map集合
<!-- 查询条件如果有多个,封装为map参数进行查询 -->
<select id="selbankwherec" resultType="com.bean.Bank"
parameterType="map">
<!-- #{mapid},#{mapmoney} 必须是parameterType="map"里面的键的名字 -->
select id,username from bank where id < #{mapid} and money <
#{mapmoney}
</select>