高并发秒杀系统--mybatis整合技巧

mybatis实现DAO接口编码技巧

1.XML文件通过namespace命名空间关联接口类

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.azcode.dao.SeckillDao">

2.接口传入的参数类型可以通过@Param("paramName")告知mybatis

/**
* 根据偏移量查询秒杀商品列表
* @param offset
* @param limit
* @return
*/
List<Seckill> queryAll(@Param("offset") int offset, @Param("limit") int limit);

3.通过ognl表达式可以映射实体类的同时映射实体类中的其他实体对象

<select id="queryByIdWithSeckill" resultType="SuccessKilled">
<!-- 根据id查询SuccessKilled并携带Seckill -->
<!-- mybatis技巧 如何把结果映射到SuccessKilled同时映射Seckill -> ongl表达式 el表达式 -->
<!-- mybatis的优势,可以*的控制sql -->
select
sk.seckill_id,
sk.user_phone,
sk.state,
sk.create_time,
s.seckill_id "seckill.seckill_id",
s.name "seckill.name",
s.number "seckill.number",
s.start_time "seckill.start_time",
s.end_time "seckill.end_time",
s.create_time "seckill.create_time"
from success_killed sk
inner join seckill s on sk.seckill_id = s.seckill_id
where sk.seckill_id = #{seckillId} and sk.user_phone = #{userPhone}
</select>

mybatis 整合 Spring理论

1.目标:[更少的代码]  [更少的配置]  [足够的灵活性]

2.更少的代码:只写接口不写实现类(接口本身可以传递足够多的信息)

3.更少的配置:

  3.1别名(包扫描org.azcode.dao.Seckill -> Seckill) typeAliasesPackage

  3.2配置扫描(自动XML配置文件扫描 Mybatis配置文件+Mapper的XML) configLocation mapperLocations

  3.3DAO接口的实现(1.自动实现DAO接口2.自动注入Spring容器) MapperScannerConfigurer -> basePackage

4.足够的灵活性:自己定制SQL *传参 结果集自动赋值resultMap

5.整合方式:XML提供SQL DAO提供Mapper

[Mybatis全局配置文件 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>
<!-- 配置全局属性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys 获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用别名代替列明 默认true -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换:Table(create_time) -> Entity(createTime) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>

[Mybatis-Spring整合 spring-dao.xml]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring整合Mybatis的过程 -->
<!-- 1:配置数据库相关的参数 properties参数使用:${property_name} -->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/> <!-- 2: 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接池后不自动Commit默认false -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间避免一直等待 -->
<property name="checkoutTimeout" value="1000"/>
<!-- 连接失败之后的重复次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean> <!-- 约定大于配置 -->
<!-- 3:配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- Entity包扫描使用别名 org.azcode.entity.Seckill -> Seckill -->
<property name="typeAliasesPackage" value="org.azcode.entity"/>
<!-- 扫描sql配置的文件:Mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean> <!-- 4:配置扫描Dao接口包,动态实现Dao接口,并注入到Spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory属性 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出扫描Dao接口的包 -->
<property name="basePackage" value="org.azcode.dao"/>
</bean>
</beans>
上一篇:easyui combo自动高度(下拉框空白问题)


下一篇:Django App(五) load static files