步骤3:mybatis的数据导入和数据读写操作
1.创建数据表,插入测试数据值
DROP TABLE IF EXISTS `bbs_brand`; CREATE TABLE `bbs_brand` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(40) NOT NULL COMMENT '名称', `description` varchar(80) DEFAULT NULL COMMENT '描述', `img_url` varchar(80) DEFAULT NULL COMMENT '图片Url', `web_site` varchar(80) DEFAULT NULL COMMENT '品牌网址', `sort` int(11) DEFAULT NULL COMMENT '排序:最大最排前', `is_display` tinyint(1) DEFAULT NULL COMMENT '是否可见 1:可见 0:不可见', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='品牌'; /*Data for the table `bbs_brand` */ insert into `bbs_brand`(`id`,`name`,`description`,`img_url`,`web_site`,`sort`,`is_display`) values (1,'依琦莲',NULL,NULL,NULL,99,1), (2,'凯速(KANSOON)',NULL,NULL,NULL,NULL,1), (3,'梵歌纳(vangona)',NULL,NULL,NULL,NULL,1), (4,'伊朵莲',NULL,NULL,NULL,NULL,1), (5,'菩媞',NULL,NULL,NULL,NULL,1), (6,'丹璐斯',NULL,NULL,NULL,NULL,1), (7,'喜悦瑜伽',NULL,NULL,NULL,NULL,1), (8,'金乐乐','金乐乐','upload/20141201101326051272.jpg',NULL,44,1);
2.创建创建Brand的bean,数据读取BrandDao接口,mapper映射文件BrandDao.xml,创建BrandService和BrandServiceImpl
3.实现上述类
- 实现Brand类
package com.mybatis.core.bean; import java.io.Serializable; public class Brand implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String name; private String imgUrl; private String description; private Integer sort; private Integer isDisplay; private String webSite; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } public Integer getIsDisplay() { return isDisplay; } public void setIsDisplay(Integer isDisplay) { this.isDisplay = isDisplay; } public String getWebSite() { return webSite; } public void setWebSite(String webSite) { this.webSite = webSite; } @Override public String toString() { return "Brand [id=" + id + ", name=" + name + ", description=" + description + ", imgUrl=" + imgUrl + ", sort=" + sort + ", isDisplay=" + isDisplay + "]"; } }
- 实现BrandDao
package com.mybatis.core.dao; import java.util.List; import com.mybatis.core.bean.Brand; public interface BrandDao { public List<Brand> getBrandList(Brand brand) ; }
- 实现BrandDao.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mybatis.core.dao.BrandDao"> <resultMap type="Brand" id="brand"> <result column="id" property="id" /> <result column="name" property="name" /> <result column="description" property="description" /> <result column="img_url" property="imgUrl" /> <result column="sort" property="sort" /> <result column="is_display" property="isDisplay" /> </resultMap> <select id="getBrandList" parameterType="Brand" resultMap="brand"> select id,name,description,img_url,web_site,sort,is_display from bbs_brand where is_display = 1 order by id desc </select> </mapper>
- 实现BrandService
package com.mybatis.core.service; import java.util.List; import com.mybatis.core.bean.Brand; public interface BrandService { public List<Brand> getBrandList(Brand brand) ; }
- BrandServiceImpl
package com.mybatis.core.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mybatis.core.bean.Brand; import com.mybatis.core.dao.BrandDao; @Service public class BrandServiceImpl implements BrandService { @Autowired private BrandDao brandDao; public List<Brand> getBrandList(Brand brand) { return brandDao.getBrandList(brand); } }
4.配置测试类
- 实现通用测试类com.ecom.common.junit .SpringJunitTest
package com.ecom.common.junit; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:application-context.xml"}) public class SpringJunitTest { }
- 实现TestBrand
package com.mybatis; import java.util.List; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.ecom.common.junit.SpringJunitTest; import com.mybatis.core.bean.Brand; import com.mybatis.core.service.BrandService; public class TestBrand extends SpringJunitTest { @Autowired private BrandService brandService; @Test public void TestSelectAll() { Brand brand=new Brand(); brand.setIsDisplay(1); List<Brand> brands= brandService.getBrandList(brand); for (Brand brand1 : brands) { System.out.println(brand1.toString()); } } }