1. 面对有很多的插入操作时,要避免 主键重复
insert ignore into tableName(user_id,user_name) value ( #{id},#{name} )......
2. 对于sql语句中使用的 >= <=,必须使用转义
<![CDATA[ when starttime<='12:00' and endtime<='12:00' ]]>
3.Mapper.xml文件放在idea的resource中,
4.对于全局的config-mybatis.xml,还有几个需要注意的
<setting name="useGeneratedKeys" value="true"/> 获取插入后的记录的
<setting name="useColumnLabel" value="true"/> 列名转换的技巧
<setting name="mapUndercoreCame" value="true"/> 开启驼峰命名转换
5.秒杀项目中,难点就是 库存的减少和 插入订单信息或者 插入秒杀成功表
接口暴露-----执行更新-----相关查询
库存表A 秒杀成功表B形成的entity中,自己加一个 实体A的属性
那查询时,怎么把两个 表一同查询出来;
select b.shop_id...... ,a.shop_id as "A.shop_id" .....
from B b inner join A a on where
6.web.xml中的spring springMVC mybatis 配置问题
<servlet> <servlet-name>SpringServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 需要加载的 spring-dao spring-service spring-web mybatis spring springMVC --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringServlet</servlet-name> <!--默认匹配所有的请求--> <url-pattern>/</url-pattern>
7.Restful就是能通过url猜出它是干嘛的。
9.在xml中引入propertites文本
<!--1.配置数据库相关参数-->
<context:property-placeholder location="classpath:config.properties"/>
去参数 就是 ${jdbc.url} ${jdbc.username}
8.spring-dao.xml 加入配置文件:1.数据源(c3p0),
2.sqlSessionFactory(包含扫描entity包和加载 config-mybatis.xml和mapper.xml包)
3.扫描加载mapper类的dao层
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-4.2.xsd">
<!--1.配置数据库相关参数-->
<context:property-placeholder location="classpath:config.properties"/>
<!--2.数据库连接池,这里使用的是alibaba的开源连接池druid-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
</bean>
<!--3.配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置Mybatis全局配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--扫描entity包,使用别名-->
<!--多个包用分号隔开就可以了-->
<property name="typeAliasesPackage" value="com.seckill.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="com.seckill.Mapperdao"/>
</bean>
<!--redisDao-->
<bean id="redisDao" class="com.seckill.dao.cache.RedisDao">
<constructor-arg index="0" value="118.89.23.16"/>
<constructor-arg index="1" value="6379"/>
</beans>
-----------------------------------------
1 秒杀开启时,输出秒杀接口的地址,否则输出系统时间,
1.1 Dto exportSeckillUrl(Long id)
Dto就是数据传输层, Dto{ private boolean ,String md5 //加密, long shop_id ,
long now //系统时间 long star ,long end ,
自己组合 构造方法,}
与业务无关,只是方便service
1.3 重复秒杀异常,防止刷票软件
RepeatException extends RuntimeException{ }
2.pom.xml 中的配置;
spring-orm beans context tx jdbc web webmvc test ,
jackson, taglib+jstl servlet+jedis
slf4j+logback mysql+c3p0 mybatis+mybatis-spring
3. service---Mapperdao----sqlSessionFactory统一的管理,
4. xml + 注解+自己定义的 spring-service.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!--扫描service包及其子包下所有的使用注解的类型-->
<context:component-scan base-package="com.seckill.service"/>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置基于注解的声明式事务,默认使用注解来管理事务行为-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
-------------------------------------------------------------------------------------------------------
1. 事务: tx :advice+aop命名空间------根据 方法名字
声明式事务嵌套-注解 @Trancational----------------注解控制()
1. 抛出运行时异常才会回滚,否则不会回滚,所以需要手动的回滚
2. 小心不当的try{}catch(){}
3.如果是运行异常,那么抛出来 catch(自定义的RuntimeException e ){ throw e }
3.配置logback的文件 slf4j ---对应的 logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder 默认配置为PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
2.编写测试类:
@Runwith(springJUnit4Class.class)
@ContextConfiguration({ "classpath: spring-dao.xml " , "classpath: spring-dao1.xml " })
public class testService{
Logger log=LoggerFactory,getLogger(this.class);// 使用就是 log.info(" List={} ",list)
}
3.spring-Controller.xml的配置,将 springMVC进行的跳转进行配置,
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置SpringMVC-->
<!--1.开启SpringMVC注解模式-->
<!--这样配置简化了配置,具体表现如下:
(1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
(2)提供了一系列功能,如:数据绑定,数字和日期的format(@NumberFormat,@DateFormat),
对xml、json提供默认的读写支持
-->
<mvc:annotation-driven/>
<!--2.因为servlet-mapping的映射路径是:"/",所以需要做一下静态资源的配置-->
<!--好处有两个:
(1)加入对静态资源(js,png等)的处理
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!--3.配置Jsp,显示ViewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--4.扫描web相关的bean-->
<context:component-scan base-package="com.seckill.Controller"/>
</beans>
5. Restful风格
/seckill/list /seckill/{shop_id}/execution