1、maven支持
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
2、构建Mybatis-config.xml文件,构建SqlSessionFactory
<?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>
<property name="username" value="root" />
<property name="password" value="mysql"/>
</properties>
<settings>
<!--配置缓存的全局开关-->
<setting name="cacheEnabled" value="true"/>
<!--延迟加载的全局开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--是否允许单一语句返回多结果集-->
<setting name="multipleResultSetsEnabled" value="true"/>
<!--使用列标签代替列名-->
<setting name="useColumnLabel" value="true"/>
<!--允许JDBC支持自动生成主键-->
<setting name="useGeneratedKeys" value="false"/>
<!--指定Mybatis应如何自动映射列到字段或属性-->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!--默认的执行器,SIMPLE就是普通的执行器,REUSE执行器会重用预处理语句,BATCH执行器将重用语句并执行批量更新-->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!--设置超时时间,驱动等待数据库响应的秒数-->
<setting name="defaultStatementTimeout" value="25"/>
<!--允许在嵌套语句中使用分页-->
<setting name="safeRowBoundsEnabled" value="false"/>
<!--是否开启驼峰命名规则映射,即数据库A_COLUMN到JAVA属性aColumn-->
<setting name="mapUnderscoreToCamelCase" value="false"/>
<!--利用本地缓存机制防止循环引用和加速重复嵌套查询-->
<setting name="localCacheScope" value="SESSION"/>
<!--当没有为参数提供特定的JDBC类型时,为空值指定JDBC类型-->
<setting name="jdbcTypeForNull" value="OTHER"/>
<!--指定哪一个对象的方法触发一次延迟加载-->
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>
<!--类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余.-->
<typeAliases>
<typeAlias alias="User" type="com.pandawork.springmvc.common.entity.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!--决定事务范围和控制方式的事务管理器(TransactionManager)-->
<transactionManager type="JDBC" />
<!--数据库连接实例的数据源(DataSource)-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_springmvc"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/springmvc/mapper/user/user.mapper.xml"/>
</mappers>
</configuration>
3、使用Java代码构建SqlSessionFactory
(1)读取配置文件
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
(2)构建SqlSessionFactory
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
(3)从SqlSessionFactory中获取SqlSession
SqlSession session = sqlSessionFactory.openSession();4、映射的SQL语句
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
//Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
} finally {
session.close();
}
<?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">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>