实战SSM_O2O商铺_17【商铺编辑】Dao层开发

2021-08-17 11:29:16 浏览数 (1)

文章目录

  • 概述
  • ShopDao接口
  • ShopDao映射文件
  • 单元测试
  • Github地址

概述

ShopDao接口

com.artisan.o2o.dao.ShopDao.java 新增查询接口

代码语言:javascript复制
/**
	 * 
	 * 
	 * @Title: selectShopById
	 * 
	 * @Description: 根据shopId查询shop
	 * 
	 * @param shopId
	 * @return
	 * 
	 * @return: Shop
	 */
	Shop selectShopById(long shopId);

ShopDao映射文件

/o2o/src/main/resources/mapper/ShopDao.xml

代码语言:javascript复制
<resultMap id="shopMap" type="com.artisan.o2o.entity.Shop">
		<id column="shop_id" property="shopId"/>
		<!-- property对应实体类中的属性名  column 对应库表中的字段名 -->
		<result column="shop_name"  property="shopName"/>
		<result column="shop_desc" property="shopDesc"/>
		<result column="shop_addr" property="shopAddr"/>
		<result column="phone" property="phone"/>
		<result column="shop_img" property="shopImg"/>
		<result column="priority" property="priority"/>
		<result column="create_time" property="createTime"/>
		<result column="last_edit_time" property="lastEditTime"/>
		<result column="enable_status" property="enableStatus"/>
		<result column="advice" property="advice"/>
		
		<!-- 复合对象 -->
		
		<!-- property对应实体类中的属性名  column 对应库表中的字段名 -->
		<association property="owner" column="owner_id"  javaType="com.artisan.o2o.entity.PersonInfo">
			<id column="user_id"  property="userId"/>
			<result  column="name" property="name"/>
		</association>
		
		<association property="area" column="area_id"  javaType="com.artisan.o2o.entity.Area">
			<id column="area_id" property="areaId"/>
			<result column="area_name" property="areaName"/>
		</association>
			
		<association property="shopCategory"  column="shop_category_id"  javaType="com.artisan.o2o.entity.ShopCategory">
			<id column="shop_category_id"  property="shopCategoryId"/>
			<result  column="shop_category_name" property="shopCategoryName"/>
		</association>
			
	</resultMap>

单元测试

代码语言:javascript复制
	@Test
	public void testSelectShopById() {
		Shop shop = shopDao.selectShopById(30L);
		Assert.assertNotNull(shop);
		Assert.assertEquals("优乐美", shop.getShopName());
		Assert.assertEquals("北京", shop.getArea().getAreaName());
		Assert.assertEquals("奶茶", shop.getShopCategory().getShopCategoryName());

		System.out.println(shop.toString());
	}

单元测试运行通过, 控制台的日志输出如下:

代码语言:javascript复制
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@36cda2c2] was not registered for synchronization because synchronization is not active
六月 02, 2018 5:29:37 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 2, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 10000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge9g69v1hxv2gi151wrqu|73e22a3d, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge9g69v1hxv2gi151wrqu|73e22a3d, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@416c58f5] will not be managed by Spring
==>  Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id AND s.shop_id = ? 
==> Parameters: 30(Long)
<==    Columns: shop_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice, area_id, area_name, shop_category_id, shop_category_name
<==        Row: 30, 优乐美, 优乐美奶茶店, 复兴街, 123456, uploaditemshopImage302018053001010899137.png, null, 2018-05-30 01:01:07.0, 2018-05-30 01:01:07.0, 0, null, 1, 北京, 3, 奶茶
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@36cda2c2]
Shop [shopId=30, shopName=优乐美, shopDesc=优乐美奶茶店, shopAddr=复兴街, phone=123456, shopImg=uploaditemshopImage302018053001010899137.png, priority=null, createTime=Wed May 30 01:01:07 BOT 2018, lastEditTime=Wed May 30 01:01:07 BOT 2018, enableStatus=0, advice=null, owner=null, area=Area [areaId=1, areaName=北京, areaDesc=null, priority=null, createTime=null, lastEditTime=null], shopCategory=ShopCategory [shopCategoryId=3, shopCategoryName=奶茶, shopCategoryDesc=null, shopCategoryImg=null, priority=null, createTime=null, lastEditTime=null, parent=null]]
六月 02, 2018 5:29:38 下午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@45afc369: startup date [Sat Jun 02 17:29:33 BOT 2018]; root of context hierarchy

库表中的数据



Github地址

代码地址: https://github.com/yangshangwei/o2o

0 人点赞