频道栏目
首页 > 资讯 > 其他 > 正文

SSM框架整合(一)

17-04-19        来源:[db:作者]  
收藏   我要投稿

最近实在太忙,之前写的《rxjava系列文章》告一段落,还有两篇会在一月中旬补上,感谢大家。这篇文章讲述mybatis搭建DAO层,另外这系列文章是我看完《Java高并发秒杀系列》的课程整理的,所以文章采用的例子也来源于这系列课程。

一.环境/工具

1.开发工具idea

2.数据库MySQL

3.javaweb容器 tomcat

4.构建项目工具maven

关于这几个工具下载安装,环境的搭建就不介绍,自行百度。

二.采用maven 构建项目

采用命令构架,当然也可以采用ide 直接构建。

构建命令:

mvn archetype:generate -DgroupId=org.forezp -DartifactId=minnkill -DarchetypeArtifactId=maven-archetype-webapp

低版本maven需要将generate 改为create.

三.补全SSM框架相关依赖

下面是SSM框架所需的所有框架


  4.0.0
  org.seckill
  seckill
  war
  1.0.0
  seckill Maven Webapp
  http://maven.apache.org
  
    
      
      junit
      junit
    4.11
      test
    
    
    
    
      org.slf4j
      slf4j-api
      1.5.6
    
    
      ch.qos.logback
      logback-core
      1.1.1
    
    
      ch.qos.logback
      logback-classic
      1.1.1
    
    
    
    mysql
    mysql-connector-java
    5.1.35
    runtime
  
    
      c3p0
      c3p0
      0.9.1.2
    
    
      commons-dbcp
      commons-dbcp
      1.4
    
    
    
      org.mybatis
      mybatis
      3.3.0
    
    
    
      org.mybatis
      mybatis-spring
      1.2.3
    
    
    
      taglibs
      standard
      1.1.2
    
    
      jstl
      jstl
      1.2
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.5.4
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    

    
    
    
      org.springframework
      spring-core
      4.1.7.RELEASE
    
    
      org.springframework
      spring-beans
      4.1.7.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      4.1.7.RELEASE
    
    
      org.springframework
      spring-tx
      4.1.7.RELEASE
    
    
    
      org.springframework
      spring-web
      4.1.7.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.1.7.RELEASE
    
    
    
      org.springframework
      spring-test
      4.1.7.RELEASE
    

    
    
      commons-collections
      commons-collections
      3.2
    


  
  
    seckill
  

四.建数据库

仿java高并发秒杀系列》的视频,建了两张表,一张上库存表,一张上秒杀表,我直接贴sql语句了,比较简单。

--创建数据库
CREATE DATABASE seckill;
--使用数据库
use seckill;

--库存表
CREATE TABLE seckill(
`seckill_id` bigint NOT NULL AUTO_INCREMENT COMMENT '商品库存id',
`name` VARCHAR(120) NOT NULL COMMENT '商品名称',
`number` int  NOT NULL COMMENT '库存数量',
`start_time` timestamp NOT NULL  DEFAULT "2016-05-07 13:28:00",
`end_time` timestamp NOT NULL  DEFAULT "2016-05-07 13:28:00",
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP  COMMENT '创建时间',
 PRIMARY KEY (seckill_id),
 key idx_start_time(start_time),
key idx_end_time(end_time),
 key idx_create_time(create_time)
)EnGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT='秒杀库存表';

---初始化数据
insert into seckill(name,number,start_time,end_time)
VALUES
 ('1000元秒杀iphone6',100,'2015-11-01 00:00:00','2015-11-02 00:00:00'),
 ('500元秒杀ipad2',200,'2015-11-01 00:00:00','2015-11-02 00:00:00'),
 ('300元秒杀小米4',300,'2015-11-01 00:00:00','2015-11-02 00:00:00'),
 ('200元秒杀红米note',400,'2015-11-01 00:00:00','2015-11-02 00:00:00');

 --秒杀成功明细表
 --用户登录认证相关的信息
 create table success_killed(
 `seckill_id` bigint NOT NULL COMMENT '秒杀商品id',
 `user_phone` bigint NOT NULL COMMENT '用户手机号',
`state` tinyint NOT NULL DEFAULT 0 COMMENT '状态标示:-1:无效 0:成功 1:已付款',
`create_time` TIMESTAMP NOT NULL COMMENT '创建时间',
PRIMARY  KEY (seckill_id,user_phone),/*联合主键*/
KEY idx_create_time(create_time)
 )EnGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='秒杀明细表';

五.DAO层实体编写和接口编写

1.创建org.forezp.entity包,根据数据库字段,编写实体。
下面是Seckill库存实体:

public class Seckill {
    private long seckillId;
    private String name;
    private int number;
    private Date startTime;
    private Date endTime;
    private Date createTime;
    //这里省去了写getter setter方法,在实际项目补上 
}
创建org.forezp.dao包
编写Seckill接口
public interface SeckillDao {
    //减库存
    int reduceNumber(@Param("seckillId")long seckillId,@Param("killTime")Date killTime);
    //根据Id查询秒杀对象
    Seckill queryById(long seckillId);
    //根据偏移量查询秒杀列表
    List queryAll(@Param("offset")int offset,@Param("limit")int limit);

采用mapper实现接口

在resources目录下创建maaper文件夹,所有的接口实现类放在这里 ,另外项目采用xml的方式实现接口。采用这种方式的好处有很多,比如可以采用包扫描,更少的配置,所有的sql语句都放在这里,有利于统一规范,代码的可维护性和可读性也大大提高。








    
        update
        seckill
        set
        number = number-1
        where seckill_id = #{seckillId}
        and
        start_time 
        #{killTime}
        and end_time >= #{killTime}
        and number > 0;
    

     


   



六. mybatis的配置

创建mybatis-config.xml 全局配置:





     


        
        
        
        
        
        

    

创建jdbc.properties文件
数据库配置文件和连接数据库驱动。

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf8
username=root
password=123

七.mybatis和spring 整合

直接贴配置文件,文件已经写好了注释。




    

    
    

         
    

        
        
        
        

    


    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

就这样mybatis和spring就整合好了,spring 提供了的ioc容器,为我们管理bean,我们不需要管理bean的生命周期.

八.单元测试

采用junit单元测试,spring 对junit做了很好的支持。只需要配置注解久可以了,

/**
 * 配置spring和junit整合,junit启动时加载springIOC容器
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring/spring-dao.xml"})
public class SeckillDaoTest {


    @Test
    public void reduceNumber() throws Exception {
        Date killTIme=new Date();
        int updateCount =seckillDao.reduceNumber(1000L,killTIme);
        System.out.println(updateCount);
        //结果第一次执行为1 后面为0 

    }
    //注入Dao实现类依赖
    @Resource
    private SeckillDao seckillDao;
    @Test
    public void queryById() throws Exception {
        long id=1001;
        Seckill seckill=seckillDao.queryById(id);
        System.out.println(seckill.getName());
        System.out.println(seckill);
        //运行结果
        /** 500元秒杀ipad2*/
      /**  Seckill{seckillId=1001, name='500元秒杀ipad2',number=200, startTime=Sun Nov 01 00:00:00 CST 2015, endTime=Mon Nov 02 00:00:00 CST 2015, createTime=Sun Nov 27 10:44:30 CST 2016}*/
    }

    @Test
    public void queryAll() throws Exception {
        //java没有保存形参的记录,queryAll(int offeset,int limit)->queryAll(arg0,arg1),通过@param注解来解决

        List seckills=seckillDao.queryAll(0,100);
        for(Seckill seckill:seckills){
            System.out.println(seckill);
        }
    }

}

到此为止,mybatis实现数据库的连接,和dao层代码的编写以及mybatis 和spring的整合 。我觉得学习路上虽然很孤单,但如果有一个分享的心态和持续学习的动力,生活也算是充实的,自己成长了,技术也提高了,回报也高了,生活品质也随之提高。所以无论做什么,自己快乐,并且能提高自己的快乐,且以此为目的就够了,祝自己好运。

源码下载

优秀文章推荐:

史上最简单的 SpringCloud 教程 | 终章史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
相关TAG标签
上一篇:Android媒体库使用分组查询
下一篇:Linux学习篇第二章~unit2 控制对文件的访问
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站