文章目录
前言
记一下mybatis的基础信息
一、Mybatis传统与代理开发
编写UserDao接口
public interface UserDao {
List<User> findAll() throws IOException;
}
编写UserDaoImpl实现
public class UserDaoImpl implements UserDao {
public List<User> findAll() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("userMapper.findAll");
sqlSession.close();
return userList;
}
}
测试传统方式
@Test
public void testTraditionDao() throws IOException {
UserDao userDao = new UserDaoImpl();
List<User> all = userDao.findAll();
System.out.println(all);
}
代理开发方式:
mybatis框架根据接口m定义创建接口的动态代理对象,代理对象的方法体同上边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的类型相同
测试代理方式
@Test
public void testProxyDao() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获得MyBatis框架⽣成的UserMapper接⼝的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findById(1);
System.out.println(user);
sqlSession.close();
}
二、Mybatis配置⽂件
1.核心配置文件SqlMapConfig.xml
1.1 常用配置解析
1.1.1 environments标签(数据库环境的配置,支持多环境配置)
其中:
事务管理器(transactionManager)类型有两种:
- JDBC:这个配置就是直接使用了JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务作
用域。 - MANAGED:这个配置几乎没做什么。它从来不提交或回滚⼀个连接,而是让容器来管理事务的整个生
命周期(比如JEE应用服务器的上下文)。 默认情况下它会关闭连接,然而⼀些容器并不希望这样,因
此需要将 closeConnection 属性设置为 false 来阻止它默认的关闭行为。
数据源(dataSource)类型有三种:
- UNPOOLED:这个数据源的实现只是每次被请求时打开和关闭连接。
- POOLED:这种数据源的实现利用"池"的概念将 JDBC 连接对象组织起来。
- JNDI:这个数据源的实现是为了能在如 EJB 或应用服务器这类容器中使用,容器可以集中或在外部配
置数据源,然后放置⼀个 JNDI 上下文的引用。
1.1.2 mapper标签(作用是加载映射)
加载方式:
•使用相对于类路径的资源引用,例如:
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
•使用完全限定资源定位符(URL),例如:
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
•使用映射器接口实现类的完全限定类名,例如:
<mapper class="org.mybatis.builder.AuthorMapper"/>
•将包内的映射器接口实现全部注册为映射器,例如:
<package name="org.mybatis.builder"/>
1.1.3 Properties标签
实际开发中,习惯将数据源的配置信息单独抽取成⼀个properties文件,该标签可以加载额外配置的properties文件
1.1.4 typeAliases标签(类型别名,为Java 类型设置⼀个短的名字)
mybatis框架已经设置好的⼀些常用的类型的别名:
别名 | 映射的类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | ArrayList |
collection | Collection |
iterator | Iterator |
2.映射配置文件mapper.xml
if判断
<select id="findByCondition" parameterType="user" resultType="user">
select * from User
<where>
<if test="id!=0">
and id=#{
id}
</if>
<if test="username!=null">
and username=#{
username}
</if>
</where>
</select>
循环sql的拼接
<select id="findByIds" parameterType="list" resultType="user">
select * from User
<where>
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{
id}
</foreach>
</where>
</select>
foreach标签用于遍历集合:
- collection:代表要遍历的集合元素,注意编写时不要写#{}
- open:代表语句的开始部分
- close:代表结束部分
- item:代表遍历集合的每个元素,生成的变量名
- sperator:代表分隔符
SQL片段抽取
Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。
<!--抽取sql片段简化编写-->
<sql id="selectUser" select * from User</sql>
<select id="findById" parameterType="int" resultType="user">
<include refid="selectUser"></include> where id=#{
id}
</select>
<select id="findByIds" parameterType="list" resultType="user">
<include refid="selectUser"></include>
<where>
<foreach collection="array" open="id in(" close=")" item="id" separator=",">
#{
id}
</foreach>
</where>
</select>