mybatis概括
一:什么是mybatis
mybatis是一个基于java持久层的框架 (Dao层),它是一个半自动化ORM框架,O- object, R- relationship,M-mapping
二:持久化
数据从瞬时状态变为持久状态
三:持久层
完成持久化工作的代码块
四:为什么需要使用mybatis
传统的jdbc操作有很多重复的代码块,比如数据读取的封装。通过框架可以减少重复代码,提高开发效率
五:配置文件解析以及优化
mybatis.cfg.xml 核心配置文件
配置文件解析以及优化: mybatis.cfg.xml 核心配置文件 优化配置文件: 导入properties配置文件 1.在src下加入db.properties配置文件 driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/test username = root password = root 2.在mybatis.cfg.xml中添加properties标签 <configuration> <properties resource="db.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容--> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}"/> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- mapping文件路径配置 --> <mappers> <mapper resource="cn/sxt/entity/player.mapper.xml"/> </mappers> </configuration> 3.为指定类型指明 别名 使得在mapper映射文件中可以简化引用(在核心配置文件) <typeAliases> //为指定类型指名 别名 使得在mapper映射文件中可以简化引用 <typeAlias type="cn.sxt.entity.Player" alias="Player"/> //为某个包下的所有类指定别名,默认别名是对应的类名 <package name="cn.sxt.entity"/> </typeAliases>
六:解决属性名和列名不一致
mybatis会根据查询的列名(会将列名转换为小写) 1.为列名指定别名,别名和java实体类的属性名要一致 <select id="selectUser" resultType="User"> select id,name,pwd password(实体类的名字) from user where id=#{id} </select> 2.设置结果映射类型 <select id="selectUser" resultMap="UserMap"> select id,name,pwd from user where id=#{id} </select> <resultMap type="User" id="UserMap"> //id为主键 <id column="id" property="id"/> //column是数据库中表的列名 property是对应实体类的属性名 <result column="name" property="name"/> <result column="pwd" property="password"/> </resultMap>
七:分页的实现
1.分析mysql的分页语句:limit startIndex(开始页数) pageNum(一页容量)
mapper映射文件
查询用户:
<select id="selectAll" parameterType="Map" resultMap="User">
select * from user limit #{startIndex},#{pageNum}
</select>
Dao层
分页查询
public List<Player> getAll(int startIndex , int pageNum) throws IOException {
SqlSession session = MyBatisUtil.getSession();
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("startIndex",(startIndex-1)*pageNum);
map.put("pageNum",pageNum);
List<Player> list = session.selectList("cn.sxt.entity.mapper.selectAll",map);
return list;
}
注意:不用为参数设置类,可以采用map结构来解决这个问题
2.通过RowBounds来实现分页(我觉得这个更简单)
mapper文件不做改变
Dao中需要新建RowBounds对象
RowBounds rowBounds = new RowBounds(index,size);
index为下标,size为容量
public List<Player> getAll(int startIndex , int pageNum) throws IOException {
SqlSession session = MyBatisUtil.getSession();
RowBounds rowBounds = new RowBounds((startIndex-1)*pageNum,pageNum);
List<Player> list = session.selectList("cn.sxt.entity.mapper.getAll",null,rowBounds);
session.close();
return list;
}
八:使用注解实现mybatis
1.面向接口编程 好处:扩展性好,分层开发中,上层不用管具体的实现,大家都遵循共同的标准,使得开发变得容易,规范性好 2.注解的实现 1.编写dao接口 public interface player_Dao{ @Select("select * from player") public List<Player> list(); } 2.在核心配置文件中导入 <mappers> <mapper resource="cn.sxt.Dao.playerDao"/> </mappers>
九:动态sql根据不同的查询条件,生成不同的sql语句
mapper文件
<mappers namespace="cn.sxt.entity.PlayerMapper">
<select id="getPlayer" parameterType=""Map" resultType="Player">
select * from Player
<where>
<if test="name!= null">
name like CONCAT('%',#{name},'%')
</if>
</where>
</select>
</mappers>