1、MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。
2、环境搭建
(1)第一步:pom.xml文件引入
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<!--如需spring结合需要引入 mybatis/spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
(2)创建mybatis配置文件
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456
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="jdbc.properties"></properties>
<!-- 设置驼峰匹配 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 设置包扫描(别名) -->
<typeAliases>
<package name="cn.demo.model"/>
</typeAliases>
<!-- 配置环境:可以配置多个环境,default:配置某一个环境的唯一标识,表示默认使用哪个环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置连接信息 -->
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
示例:userMapper.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">
<!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
-->
<mapper namespace="com.demo.dao.userMapper">
<!-- 根据id查询得到一个user对象 -->
<select id="getUser" parameterType="int"
resultType="com.demo.model.User">
select * from users where id=#{id}
</select>
<!-- 创建用户(Create) -->
<insert id="addUser" parameterType="com.demo.model.User">
insert into users(name,age) values(#{name},#{age})
</insert>
<!-- 删除用户(Remove) -->
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<!-- 修改用户(Update) -->
<update id="updateUser" parameterType="com.demo.model.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<!-- 查询全部用户-->
<select id="getAllUsers" resultType="com.demo.model.User">
select * from users
</select>
</mapper>
测试代码
@Test
public void test() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
User list = sqlSession.selectOne("com.demo.dao.userMapper",1L);
System.out.println(list.getUsername());
}
2、typeAliases 类型别名
实例:
<typeAliases>
<typeAlias alias="User" type="com.demo.model.User"/>
</typeAliases>
3、mappers 映射器
示例:
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
4、MyBtais缓存
(1)、一级缓存默认开启,不能关闭,属于sqlSession级别
SqlSession执行insert、update、delete等操作commit后会清空该SQLSession缓存。
(2)、二级缓存默认关闭,可以开启,属于mappers级别
开启二级缓存步骤:
(3)、配置mybtais-config.xml
<settings>
<!-- 二级缓存总开关 默认关闭,true:开启 -->
<setting name="cacheEnabled" value="true"/>
</settings>
(4)、配置demoMapper.xml
<mapper namespace="com.ssm.dao.xxxx">
<cache/>
</mapper>