mavn+ajax的单表项目
1.项目的搭建
-
步骤一:创建项目
-
步骤二:复制部门xml文件
-
步骤三:拷贝properties文件
-
步骤四:拷贝配置类
-
步骤五:编写启动
1.2 分类的基本结构
-
编写domain JavaBean:Category
-
编写dao:CategoryMapper
-
编写service:CategoryService
-
编写controller:CategoryController
-
编写domain JavaBean:Category
/** * @author 桐叔 * @email [email protected] */ @Table(name="t_category") public class Category { @Id private Integer cid; @Column(name="cname") private String cname; @Column(name="description") private String description; // 省略getter、setter 等方法 }
-
编写dao:CategoryMapper
package com.czxy.mapper; import com.czxy.domain.Category; import tk.mybatis.mapper.common.Mapper; /** * @author 桐叔 * @email [email protected] */ @org.apache.ibatis.annotations.Mapper public interface CategoryMapper extends Mapper<Category> { }
-
编写service:CategoryService
package com.czxy.service; import com.czxy.mapper.CategoryMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /** * @author 桐叔 * @email [email protected] */ @Service @Transactional public class CategoryService { @Resource private CategoryMapper categoryMapper; }
-
编写controller:CategoryController
package com.czxy.controller; import com.czxy.service.CategoryService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author 桐叔 * @email [email protected] */ @RestController @RequestMapping("/category") public class CategoryController { @Resource private CategoryService categoryService; }
1.3 商品的基本结构
-
编写domain :Product
-
编写dao:ProductMapper
-
编写service:ProductService
-
编写controller:ProductController
-
编写domain :Product
/** * @author 桐叔 * @email [email protected] */ @Table(name="t_product") public class Product { @Id private Integer pid; @Column(name="pname") private String pname; @Column(name="price") private Double price; @Column(name="num") private Integer num; @Column(name="category_id") private Integer categoryId; }
-
编写dao:ProductMapper
package com.czxy.mapper; import com.czxy.domain.Product; import tk.mybatis.mapper.common.Mapper; /** * @author 桐叔 * @email [email protected] */ @org.apache.ibatis.annotations.Mapper public interface ProductMapper extends Mapper<Product> { }
-
编写service:ProductService
package com.czxy.service; import com.czxy.mapper.ProductMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /** * @author 桐叔 * @email [email protected] */ @Service @Transactional public class ProductService { @Resource private ProductMapper productMapper; }
-
编写controller:ProductController
package com.czxy.controller; import com.czxy.service.ProductService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author 桐叔 * @email [email protected] */ @RestController @RequestMapping("/product") public class ProductController { @Resource private ProductService productService; }
-
1.5 单表测试
-
基于通用mapper的单表测试:分类
package com.czxy;
import com.czxy.domain.Category;
import com.czxy.mapper.CategoryMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
*/
@RunWith(SpringRunner.class) //spring整合Junit
@SpringBootTest(classes=Bms09Application.class) // 整合SpringBoot
public class TestCategory {
@Resource
private CategoryMapper categoryMapper;
@Test
public void testDemo() {
//查询所有
List<Category> list = categoryMapper.selectAll();
for(Category category : list) {
System.out.println(category);
}
}
@Test
public void testDemo02() {
// 通过id查询详情
Category category = categoryMapper.selectByPrimaryKey(2);
System.out.println(category);
}
}
- 基于通用mapper的单表测试:商品
package com.czxy;
import com.czxy.domain.Product;
import com.czxy.mapper.ProductMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Bms09Application.class)
public class TestProduct {
@Resource
private ProductMapper productMapper;
@Test
public void testDemo(){
List<Product> list = productMapper.selectAll();
for (Product product : list) {
System.out.println(product);
}
}
}