工程搭建
Mybatis的核心包和依赖包
MySQL的驱动包
Junit 单元测试包
全局配置文件
在src/main/resources目录下,创建mybatis-config.xml文件
全局配置文件中配置了事务管理器和数据库连接池的配置。
事务使用jdbc事
连接池使用mybatis提供的默认连接池
<?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="development">
<environment id="development">
<!-- 使用jdbc管理事务mybatis -->
<transactionManager type="JDBC"/>
<!-- 数据库连接池 mybatis内置的一个数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/i-soufang?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
</mappers>
</configuration>
加载全局配置文件
public class SQLSessionFactoryTest {
@Test
public void test() throws IOException {
//1.读取mybatis的全局配置文件 读取的是编译后target的配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.构建SQL会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//3.从SQL会话工厂获取SQL会话
SqlSession sqlSession = factory.openSession();
// 具体的CRUD操作,通过sql会话向数据库发起增删改查的请求
//4.关闭会话
sqlSession.close();
}
}
编写House 类
编写映射文件
HouseMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离-->
<mapper namespace="com.imcode.mybatis.mapper.HouseMapper">
<!--查询所有的酒店信息-->
<select id="getById" parameterType="java.lang.Integer"
resultType="com.imcode.mybatis.model.House">
</select>
</mapper>
namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离
1.id属性: 标识映射文件中的sql,将sql语句封装到mapped statement对象中,所以称为
statement的id
2.parameterType:指定输入参数类型
3.#{}:表示一个占位符
4.#{id}:其中的id表示接收的输入参数,如果输入参数是简单类型
5.#{}中的参数名可以任意,可以为value或者其他
6.resultType:指定sql输出结果的所映射的java对象类型,
select指定resultType表示将单条记录映射成的java对象
Mapper 代理开发方式
需要定义Mapper(dao)接口,定义的接口要遵循如下规范:
开发规范
mapper 接口的全限定名要和 mapper 映射文件的 namespace 值一致。
mapper 接口的方法名称要和 mapper 映射文件的 statement 的 id 一致。
mapper 接口的方法参数类型要和 mapper 映射文件的 statement 的 parameterType 的值一致。
mapper 接口的方法返回值类型要和 mapper 映射文件的 statement 的 resultType 的值一致。
创建mapper包存放 mapper接口
HouseMapper
public interface HouseMapper {
House getById(Integer id);
}
crud操作可以改成:
HouseMapper mapper = sqlSession.getMapper(HouseMapper.class);
House house = mapper.getById(1);
这个语句中house是接口,是不能直接调用方法的类,所以 :
在运行期 mybatis 帮助我们创建了 HouseMapper 接口的一个实现类,并且初始化出来该实现类的一个对象 (代理实现类) 反射 用到JDK动态代理
输入简单数据类型映射
Mapper 接口
package com.imcode.mybatis.mapper;
import com.imcode.mybatis.model.House;
import java.io.IOException;
import java.util.List;
public interface HouseMapper {
House getById(Integer id) throws IOException;
/**
* 通过房源标题模糊查询满足条件的房源
* @param title
* @return
*/
List<House> findByTitle(String title);
}
/**
* 通过房源标题和街道名称模糊查询
* 如果是多个参数 需要在参数前面使用@Param注解
* @return
*/
List<House> findByTitleAndDistrict01(
@Param("houseTitle") String title,
@Param("district") String district);
Mapper映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离-->
<mapper namespace="com.imcode.mybatis.mapper.HouseMapper">
<select id="getById" parameterType="java.lang.Integer" resultType="com.imcode.mybatis.model.House">
select * from house where id = #{id}
</select>
需求:房屋标题模糊查询
思路:模糊查询返回结果可能是多条纪录
<select id="findByTitle" parameterType="java.lang.String" resultType="com.imcode.mybatis.model.House">
select * from house where title like #{title}
</select>
<select id="findByTitleAndDistrict01"
resultType="com.imcode.mybatis.model.House">
SELECT
id,
title,
logo,
price,
area,
city,
district,
create_time createTime,
update_time updateTime
FROM house
WHERE title LIKE CONCAT('%',#{houseTitle},'%')
AND district LIKE CONCAT('%',#{district},'%')
</select>
</mapper>
resultType:指定单条记录所映射成java对象类型就可以
${}
: 表示拼接sql,将接收到参数的内容不加任何修饰拼接在sql中
使用${}
容易引起sql注入
${value}
:接受输入参数的内容,如果传入类型是简单类型,${}中只能用value
对于新增、修改、删除操作,操作完成后要调用sqlSession.commit();提交事务
如果出现异常,调用sqlSession.rollback(); 回顾事务
MyBatis默认将事务设置成了手动提交