标签:drive bubuko ssm 开发 实体 提升 data- spring alias
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系。本系列教程希望您能站在上帝的角度去观察(了解)Java体系。使Java的各种后端技术在你心中模块化;让你在工作中能将Java各个技术了然于心;能够即插即用。本章我们来一起了解ORM(对象关系映射关系)框架之Mybatis(Ibatis)。
ORM(Object Relational Mapping)对象关系映射,是 一种为了解决面向对象与关系型数据库不匹配而出现的技术,使开发者能够用面向对象的方式使用关系型数据库。
/** * @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整合的有两个关注点
<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二级缓存是否开启
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()); }
标签:drive bubuko ssm 开发 实体 提升 data- spring alias
原文地址:https://www.cnblogs.com/jimisun/p/9717124.html