核心配置文件也就是sqlMapConfig.xml,我把它的代码先写下面
源代码:
xxxxxxxxxx
<configuration>
<!--配置数据源环境。default表示默认情况下使用的是哪个环境,例如development,环境名是自定义的-->
<!--transactionManager指的是你要用哪种事务管理器,例如原生JDBC。dataSource指的是你要用哪种数据源,例如连接池POOLED-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--下面那行的'?useSSL=false&useUnicode=true&characterEncoding=UTF-8'可写可不写-->
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="228675"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="com/huanf/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
通过上面的代码可以看到
xxxxxxxxxx
<configuration配置>
<properties属性>
<settings设置>
<typeAliases类型别名>
<typeHandlers类型处理器>
<objectFactory对象工厂>
<plugins插件>
<environments环境>
<environment环境变量>
<transactionManager事务管理器>
<dataSource数据源>
<databaseIdProvider数据库厂商标识>
<mappers映射器>
说明的标签一定要按照上下顺序来写,不然报错。例如如果存在
下面会详细介绍比较常用的几个标签
数据库环境的配置,支持多环境配置
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--下面那行的'?useSSL=false&useUnicode=true&characterEncoding=UTF-8'可写可不写-->
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="228675"/>
</dataSource>
</environment>
</environments>
例如我们写的如上,分析如下
1、指定默认的环境名称。例如default="development"
2、指定当前环境名称。例如id="development"
3、指定事务管理类型是JDBC。例如type="JDBC" 事务管理器(transactionManager)类型有两种: (1)JDBC: 指的是直接使用JDBC的提交和回滚设置,依赖于从数据源得到的连接来管理事务作用域 (2)MANAGED: 指的是不提交或回滚连接,而是让容器来管理事务的整个生命周期。弊端是默认情况会关闭连接,然而一些容器并不希望这样,因此 将closeConnection属性设置为false来阻止它默认的关闭行为
4、指定当前数据源类型是连接池。例如type="POOLED" 数据源(dataSouces)类型有三种: (1)UNPOOLED: 这个数据源的实现只是每次被请求时打开和关闭连接 (2)POOLED: 这种数据源的实现利用"池"的概念将JDBC连接对象组织起来 (3)JNDI: 这个数据源的实现是为了能在如EJB或应用服务器这类容器中使用,容器可以集中或在外部配合数据源,然后放置一个JNDI上下文的引用
5、数据源配置的基本参数。例如
该标签的作用是加载映射的,加载方式有如下几种:
1、使用相对于类路径(resources目录)的资源引用。比较常用,例如:
2、使用完全限定资源定位符(url),即在本地磁盘的哪个位置有哪个文件。用的不多,例如:
3、使用映射器接口实现类的完全限定类名。后续学MyBatis注解之后才用的上,例如:
4、将包内的映射器接口实现全部注册为映射器。用的不多,例如:
实际开发中,习惯将数据源的配置信息单独抽取成一个properties文件,该标签可以加载额外配置的xxx.properties文件
xxxxxxxxxx
<properties resource="jdbc.properties"></properties>
注意在Resources目录下必须要有jdbc.properties文件
这里需要我们敲一下代码,具体操作如下:
第一步: 在src/main/resources目录下,新建File,文件名为jdbc.properties,里面写入如下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=228675
第二步: 把sqlMapConfig.xml修改为如下
xxxxxxxxxx
<configuration>
<!--通过properties标签加载外部(resources目录)配置的properties文件-->
<properties resource="jdbc.properties"></properties>
<!--配置数据源环境。default表示默认情况下使用的是哪个环境,例如development,环境名是自定义的-->
<!--transactionManager指的是你要用哪种事务管理器,例如原生JDBC。dataSource指的是你要用哪种数据源,例如连接池POOLED-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<!--下面那行的'?useSSL=false&useUnicode=true&characterEncoding=UTF-8'可写可不写-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="com/huanf/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
第三步: 在MyBatisTest类执行test1,如果能在控制台查询出数据,就说明配置外部文件成功了 其实跟之前学的在spring的配置文件把jdbc的配置抽取到外部的jdbc.properties文件是一样的,在mybatis里也可以这样做
类型别名是Java类型设置一个短的名字。原来的类型名称配置如下
<select id="findAll" resultType="com.huanf.domain.User">
select * from user
</select>
我们可以使用typeAliases标签优化上面的写法,由于resultType的值太长了,我们可以使用typeAliases标签为resultType的值设置一个别名,这样用的时候resultType的值就直接写别名。如下
xxxxxxxxxx
<typeAliases>
<typeAlias type="com.huanf.domain.User" alias="bieming"></typeAlias>
</typeAliases>
这样我们写的时候就直接如下
xxxxxxxxxx
<select id="findAll" resultType="bieming">
select * from user
</select>
除我们自己可以设置别名,其实mybatis框架已经为我们设置了一些别名。在写resultType值的时候,我们可以使用标准写法,也可以使用别名,如下
标准写法 | 别名 |
---|---|
String | string |
Long | long |
Integer | int |
Double | double |
Boolean | boolean |
List | list |
以我们自己设置别名为例,注意标签是
第一步: 在sqlMapConfig,xml里面添加如下
<!--为UserMapper.xml里面的resultType属性,设置别名-->
<typeAliases>
<typeAlias type="com.huanf.domain.User" alias="bieming"></typeAlias>
</typeAliases>
第二步: 在UserMapper.xml里面添加如下
xxxxxxxxxx
<!--查询操作。上面的resultType值太长了,我们可以在sqlMapConfig.xml里面配置<typeAliases>标签,作用是为resultType值设置别名,例如设置为bieming-->
<select id="findAll2" resultType="bieming">
<!--查询结果会放到上面那行的resultType路径下-->
select * from user
</select>
第三步: 在MyBatisTest类里面添加如下,并运行test1_2测试
//快速入门,查询数据,测试别名(别名是在sqlMapConfig.xml里面配置的,在UserMapper.xml里面写的)
public void test1_2() throws IOException {
//获得核心配置文件。注意写的是相对路径,也就是src/main/resources/sqlMapConfig.xml
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作,参数其实就是UserMapper.xml里面的<mapper>标签的namespace值,还有<select>标签的id值。语法namespace值.id值
List<User> userList = sqlSession.selectList("zidingyi_userMapper.findAll2");
//测试test1_2
System.out.println(userList);
//释放资源
sqlSession.close();
}