1、基础的搭建和测试

来源:互联网 发布:幼儿英语教学软件 编辑:程序博客网 时间:2024/06/06 09:03

1、新建springboot初始化项目,启动,确认不报错

2、微信点餐系统需要有五张表的支持:

-- 类目create table `product_category` (    `category_id` int not null auto_increment,    `category_name` varchar(64) not null comment '类目名字',    `category_type` int not null comment '类目编号',    `create_time` timestamp not null default current_timestamp comment '创建时间',    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    primary key (`category_id`));-- 商品create table `product_info` (    `product_id` varchar(32) not null,    `product_name` varchar(64) not null comment '商品名称',    `product_price` decimal(8,2) not null comment '单价',    `product_stock` int not null comment '库存',    `product_description` varchar(64) comment '描述',    `product_icon` varchar(512) comment '小图',    `product_status` tinyint(3) DEFAULT '0' COMMENT '商品状态,0正常1下架',    `category_type` int not null comment '类目编号',    `create_time` timestamp not null default current_timestamp comment '创建时间',    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    primary key (`product_id`));-- 订单create table `order_master` (    `order_id` varchar(32) not null,    `buyer_name` varchar(32) not null comment '买家名字',    `buyer_phone` varchar(32) not null comment '买家电话',    `buyer_address` varchar(128) not null comment '买家地址',    `buyer_openid` varchar(64) not null comment '买家微信openid',    `order_amount` decimal(8,2) not null comment '订单总金额',    `order_status` tinyint(3) not null default '0' comment '订单状态, 默认为新下单',    `pay_status` tinyint(3) not null default '0' comment '支付状态, 默认未支付',    `create_time` timestamp not null default current_timestamp comment '创建时间',    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    primary key (`order_id`),    key `idx_buyer_openid` (`buyer_openid`));-- 订单商品create table `order_detail` (    `detail_id` varchar(32) not null,    `order_id` varchar(32) not null,    `product_id` varchar(32) not null,    `product_name` varchar(64) not null comment '商品名称',    `product_price` decimal(8,2) not null comment '当前价格,单位分',    `product_quantity` int not null comment '数量',    `product_icon` varchar(512) comment '小图',    `create_time` timestamp not null default current_timestamp comment '创建时间',    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    primary key (`detail_id`),    key `idx_order_id` (`order_id`));-- 卖家(登录后台使用, 卖家登录之后可能直接采用微信扫码登录,不使用账号密码)create table `seller_info` (    `id` varchar(32) not null,    `username` varchar(32) not null,    `password` varchar(32) not null,    `openid` varchar(64) not null comment '微信openid',    `create_time` timestamp not null default current_timestamp comment '创建时间',    `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    primary key (`id`)) comment '卖家信息表';

可以通过数据库的桌面软件连接到虚拟机,创建名叫sell的数据库,将五张表创建好。

3、环境准备

原本的课程里为我们准备了虚拟机,里面已经安装好redis、jdk、tomcat、nginx、mysql等,我们只需要将其启动,与我们的主机之间Ping通,就可以使用了。

4、数据库配置

我们这里使用spring data jps进行数据的crud操作,mysql作为数据库。

所以需要引入依赖:

<dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-jpa</artifactId></dependency>

再者,需要在配置文件中添加数据库连接(这里先将原来的application.zpplication文件重新命名为application.yml文件):

spring:  datasource:    driver-class-name: com.mysql.jdbc.Driver    username: 用户名    password: 密码    url: jdbc:mysql://虚拟机IP/sell?characterEncoding=utf-8&useSSL=false  jpa:    show-sql: true

这样,基本的数据库和连接的配置都差不多准备好了,下面就拿类目表进行测试吧!

5、创建类目的实体类

@RunWith(SpringRunner.class)@SpringBootTest@Slf4jpublic class ProductCategoryDaoTest {    @Autowired    private ProductCategoryDao productCategoryDao;    /**     * 测试获取类目的列表     * @throws Exception     */    @Test    public void findByCategoryTypeIn() throws Exception {        List<ProductCategory> categoryList = productCategoryDao.findByCategoryTypeIn(Arrays.asList(1,2));        //我在数据库中准备了三条数据        assertEquals(2,categoryList.size());        log.info("【获取类目列表】获取成功");    }    /**     * 测试保存数据     */    @Test    public void testSave(){        ProductCategory productCategory = new ProductCategory("冬日热销",2);        ProductCategory productCategory1 = productCategoryDao.save(productCategory);        assertNotNull(productCategory1);        log.info("【保存类目】保存成功");    }    /**     * 测试根据categoryId获取一条数据     */    @Test    public void testFindOne(){        ProductCategory productCategory = productCategoryDao.findOne(10);        assertNotNull(productCategory);        log.info("【获取一条类目】获取成功");    }}

这里使用了日志,springboot中默认使用的是logback,他是基于slf4j的一个log4j的升级版本,比较好用。下面是他的配置文件logback-spring.xml:

<?xml version="1.0" encoding="UTF-8" ?><configuration>    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">        <layout class="ch.qos.logback.classic.PatternLayout">            <pattern>                %d - %msg%n            </pattern>        </layout>    </appender>    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">        <filter class="ch.qos.logback.classic.filter.LevelFilter">            <level>ERROR</level>            <onMatch>DENY</onMatch>            <onMismatch>ACCEPT</onMismatch>        </filter>        <encoder>            <pattern>                %msg%n            </pattern>        </encoder>        <!--滚动策略-->        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--路径-->            <!--<fileNamePattern>/var/log/tomcat/sell/info.%d.log</fileNamePattern>-->            <fileNamePattern>E://swg/info.%d.log</fileNamePattern>        </rollingPolicy>    </appender>    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">            <level>ERROR</level>        </filter>        <encoder>            <pattern>                %msg%n            </pattern>        </encoder>        <!--滚动策略-->        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            <!--路径-->            <!--<fileNamePattern>/var/log/tomcat/sell/error.%d.log</fileNamePattern>-->            <fileNamePattern>E://swg/error.%d.log</fileNamePattern>        </rollingPolicy>    </appender>    <root level="info">        <appender-ref ref="consoleLog" />        <appender-ref ref="fileInfoLog" />        <appender-ref ref="fileErrorLog" />    </root></configuration>

这里我配置输出日志到我本地电脑(windows)下的E盘下的swg文件下。我们随便测试一个方法,会发现该文件夹下面会多出两个日志文件。输出台也会显示日志文件。

6、service的接口和实现、测试

public interface CategoryService {    //根据类目id获取一条类目    ProductCategory getByCategoryId(Integer categoryId);    //根据类目的类型(一组)获取类目的列表    List<ProductCategory> productCategoryListByType(List<Integer> categoryTypeList);    //插入或者更新一条类目    ProductCategory save(ProductCategory productCategory);    //查找所有的类目    List<ProductCategory> getAll();}

实现类:

@Servicepublic class CategoryServiceImpl implements CategoryService {    @Autowired    private ProductCategoryDao productCategoryDao;    @Override    public ProductCategory getByCategoryId(Integer categoryId) {        return productCategoryDao.findOne(categoryId);    }    @Override    public List<ProductCategory> productCategoryListByType(List<Integer> categoryTypeList) {        return productCategoryDao.findByCategoryTypeIn(categoryTypeList);    }    @Override    public ProductCategory save(ProductCategory productCategory) {        return productCategoryDao.save(productCategory);    }    @Override    public List<ProductCategory> getAll() {        return productCategoryDao.findAll();    }}

测试:

@RunWith(SpringRunner.class)@SpringBootTestpublic class CategoryServiceImplTest {    @Autowired    private CategoryService categoryService;    @Test    public void getByCategoryId() throws Exception {        ProductCategory productCategory = categoryService.getByCategoryId(10);        assertNotNull(productCategory);    }    @Test    public void productCategoryList() throws Exception {        List<ProductCategory> productCategoryList = categoryService.productCategoryListByType(Arrays.asList(1,2,3));        assertEquals(3,productCategoryList.size());    }    @Test    @Transactional    public void save() throws Exception {        ProductCategory productCategory = new ProductCategory("呵呵很好吃",4);        ProductCategory productCategory1 = categoryService.save(productCategory);        assertNotNull(productCategory1);    }    @Test    public void getAll() throws Exception {        List<ProductCategory> productCategoryList = categoryService.getAll();        assertEquals(3,productCategoryList.size());    }}

至此,关于类目的dao和service层的测试、数据库连接、日志等都完成了。

7、代码对应标签2.0