歡迎查看Java開發之上帝之眼系列教程,若是您正在爲Java後端龐大的體系所困擾,若是您正在爲各類繁出不窮的技術和各類框架所迷茫,那麼本系列文章將帶您窺探Java龐大的體系。本系列教程但願您能站在上帝的角度去觀察(瞭解)Java體系。使Java的各類後端技術在你心中模塊化;讓你在工做中能將Java各個技術瞭然於心;可以即插即用。本章咱們來一塊兒瞭解ORM(對象關係映射關係)框架之Mybatis(Ibatis)。html
ORM(Object Relational Mapping)對象關係映射,是 一種爲了解決面向對象與關係型數據庫不匹配而出現的技術,使開發者可以用面向對象的方式使用關係型數據庫。java
/** * @Author:jimisun * @Description: * @Date:Created in 08:37 2018-09-24 * @Modified By: */ public class Main { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); TestUserMapper mapper = sqlSession.getMapper(TestUserMapper.class); TestUser testUser = mapper.selectOne(1); System.out.println(testUser.toString()); } }
@Select(" SELECT * from user where id = #{id};")
MyDto selectOne(Integer id);
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.jimisun.dao.TestUserMapper"> <!--空--> </mapper>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="10"/> </bean> <!-- mapper配置 --> <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據庫鏈接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加載mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 掃描entity包 使用別名 --> <property name="typeAliasesPackage" value="com.jimisun.domain"/> <!-- 掃描sql配置文件:mapper須要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 配置掃描Dao接口包,動態實現Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 給出須要掃描Dao接口包 --> <property name="basePackage" value="com.jimisun.dao"/> </bean> </beans>
Spring和Myabtis整合的有兩個關注點git
<resultMap id="MyDto" type="com.jimisun.domain.dto.MyDto"> <result property="myid" column="id"></result> <result property="myusername" column="username"></result> </resultMap>
<!--開啓二級緩存--> <cache/>
<select id="selectOne" parameterType="java.lang.Integer" resultType="com.jimisun.domain.TestUser" useCache="false"> SELECT * from user where id = #{id} </select>
咱們進行簡單的測試 , 觀察Mybatis二級緩存是否開啓github
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); TestUserMapper testUserMapper = (TestUserMapper) context.getBean("testUserMapper"); /*測試緩存:先查詢此時username爲jimisun*/ TestUser testUser = testUserMapper.selectOne(1); /*測試緩存:修改username爲lisi*/ Integer integer = testUserMapper.updateOne(1); /*測試緩存:最後查詢查看是否從數據庫獲取仍是從緩存獲取*/ TestUser resultUser = testUserMapper.selectOne(1); System.out.println(resultUser.toString()); }