聊聊、Mybatis XML

导航: 

聊聊、Mybatis API 

聊聊、Mybatis集成Spring XML 方式

聊聊、Mybatis集成Spring 注解方式 

聊聊、手写Mybatis XML配置方式  

聊聊、手写Mybatis 注解配置方式   

聊聊、手写Mybatis SpringBoot Starter 

 

项目结构


 

 

引入依赖 


<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.5</version>
</dependency>

 

 

Mybatis-config.xml 


 <?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>
<properties resource="datasource-dev.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<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>
<mappers>
<mapper resource="mappers/AccountMapper.xml"/>
<mapper class="org.rockcode.mappers.AccountMapper" />
</mappers>
</configuration>

 

AccountMapper.xml 


 <!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="accountMapper">
<select id="queryAll" resultType="java.util.Map">
select * from account
</select>
</mapper>

 

AccoutMapper 


package org.rockcode.mappers;

import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;

public interface AccountMapper {
@Select("select * from account")
public List<Map<String,Object>> queryAll();
}

datasource-dev.properties


driver = com.mysql.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/test?useSSL=false
username = root
password = root

 

Main方法 


public static void main(String[] args) {

String source = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(source);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Object> list = sqlSession.selectList("accountMapper.queryAll");
System.out.println(list);
System.out.println("====================================");
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
List<Map<String, Object>> mapList = mapper.queryAll();
System.out.println(mapList);
} catch (IOException e) {
e.printStackTrace();
}

}

 

上面就是 xml 相关的代码,在 mybatis-config.xml 配置了下面这行:

<mappers>

  <mapper resource="mappers/AccountMapper.xml"/>

  <mapper class="org.rockcode.mappers.AccountMapper" />

</mappers>

mybatis 中总共有 4 种配置方式,resource、class、url、package。其中,class 和 package 会走代理逻辑。而 resource 和 url 不会走代理逻辑。这个后面会详细写到。

AccountMapper mapper = sqlSession.getMapper(AccountMapper.class) 对应 class 配置,也就是走 jdk动态代理。

List<Object> list = sqlSession.selectList("accountMapper.queryAll") 对应 resource 配置,直接 AccountMapper.xml 里面获取 sql 执行逻辑,不会走动态代理。

但是像 class、resource、url 配置不够灵活,如果有多个配置,则要写很多。 

<mappers>

  <mapper resource="mappers/AccountMapper.xml"/>

  <mapper resource="mappers/AMapper.xml"/>

  <mapper resource="mappers/BMapper.xml"/>

  <mapper class="org.rockcode.mappers.AccountMapper" />

 <mapper class="org.rockcode.mappers.AMapper" />

 <mapper class="org.rockcode.mappers.BMapper" />

</mappers>

这个时候就可以用 package,<package name="org.rockcode.mappers"/>,则 org.rockcode.mappers 包下面的 Mapper 接口,都会处理。不用全部写在配置文件中。

xml 的配置就到这里了,下一篇会聊聊 mybatis 如何与 spring 集成的呢?用到了 spring 哪些扩展点呢?

posted @ 2019-01-23 23:05  香农随笔  阅读(251)  评论(0编辑  收藏  举报