MyBatis入门篇
MyBatis简介
- mybatis是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动,创建连接,创建statement等繁杂的过程。
- mybatis支持普通sql查询,存储过程和高级映射。
- mybatis通过xml或者注解的方式配置和定义映射关系,将java中的POJO映射成数据库中的记录。
MyBatis的快速入门
MyBatis开发步骤:
1.添加MyBatis坐标
2.创建user数据表
3.编写User实体类
4.编写映射文件 UserMapper.xml(编写sql文件)
5.编写核心配置文件SqlMapConfig.xml(编写配置)
6.编写测试类
获取SqlSession对象
String config="SqlMapConfig.xml";
SqlSession session=null;
@Before
public void before() throws IOException {
//加载配置文件
Reader reader = Resources.getResourceAsReader(config);
//创建SessionFaction对象
SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = ssfb.build(reader);
//创建SqlSession对象
session=ssf.openSession();
}
映射文件
MyBatis增删改查操作
插入操作
<!--插入操作-->
<insert id="save" parameterType="cn.yunhe.java.pojo.Students">
insert into students (name,password,phone,gender,hobby,age,address) values(#{name},#{password},#{phone},#{gender},#{hobby},#{age},#{address})
</insert>
@Test
public void save() throws IOException {
Students stus=new Students();
stus.setName("白起");
stus.setPassword("123456");
stus.setGender("女");
stus.setAge(18);
stus.setHobby("吃饭");
stus.setAddress("河南许昌");
stus.setPhone("99999999999");
int insert = session.insert("stu.save",stus);
session.commit();
if(insert>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
session.close();
}
插入操作注意问题
- 插入语句使用insert标签
- 在映射文件中使用parameterType属性指定要插入的数据类型
- Sql语句中使用#{实体属性名}方式引用实体中的属性值
- 插入操作使用的API是sqlSession.insert(“命名空间.id”,实体对象)
- 插入操作涉及到数据库数据变化,所以要使用sqlSession对象显示的提交事务即sqlSession.commit()
修改数据
<!--修改操作-->
<update id="update" parameterType="cn.yunhe.java.pojo.Students">
update students set name=#{name},password=#{password},phone=#{phone},gender=#{gender},hobby=#{hobby},age=# {age},address=#{address} where sid=#{sid}
</update>
@Test
public void update() throws IOException {
Students stus=new Students();
stus.setSid(6);
stus.setName("孙尚香");
stus.setPassword("123456");
stus.setGender("男");
stus.setAge(19);
stus.setHobby("吃饭");
stus.setAddress("河南许昌");
stus.setPhone("55555555555");
int update = session.update("stu.update", stus);
session.commit();
if(update>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
session.close();
}
修改操作注意问题
- 修改语句使用update标签
- 修改操作使用的API是sqlSession.update(“命名空间.id”,实体对象)
删除数据
<!--删除操作-->
<delete id="delete" parameterType="cn.yunhe.java.pojo.Students">
delete from students where sid=#{sid}
</delete>
@Test
public void delete() throws IOException {
Students stus=new Students();
stus.setSid(6);
int delete = session.delete("stu.delete", stus);
session.commit();
if(delete>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
session.close();
}
删除操作注意问题
- 删除语句使用delete标签
- 删除操作使用的API是sqlSession.delete(“命名空间.id”,实体对象)
- Sql语句使用#{任意字符串}方式引用传递的单个参数
MyBatis核心配置文件
- configuration配置
- properties属性
- setting 设置
- typeAliases类型别名
- typeHandlers类型处理器
- objectFactory对象工厂
- plugins插件
- environments环境
- enviroment环境变量
- transactionManager事务管理器
- dataSource数据源
- databaseldProvider数据库厂商标识
- mappers映射器 加载映射文件 可以有多个
environmenrs标签
数据库环境的配置,支持多环境的配置
properties标签
实际开发中,习惯将数据源的配置信息单独抽取成一个properties文件,该标签可以加载额外配置的properties(jdbc.properties)
<!--加载外部properties-->
<properties resource="jdbc.properties"></properties>
java.lang.Integer 可以写成int
Mybatis的Dao层实现
代理开发模式
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),有MyBatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边
Dao接口实现类方法。
Mapper接口开发需要遵循一下规范:
1.Mapper.xml文件中的namespace与mapper接口的全限定名相同
2.Mapper接口中的方法名和mapper.xml中定义的每个statement的id相同
3.Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4.Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType 的类型相同