500分悬赏 thinkphp二级分类 包括数据库设计及代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了500分悬赏 thinkphp二级分类 包括数据库设计及代码相关的知识,希望对你有一定的参考价值。
参考技术A 在官方手册去查了下,没有具体说明怎么支持二级域名的方法,最后找到一个解决方案(修改thinkphp的parsePathInfo函数让其支持二级域名)。# 修改的路径是: # ThinkPHP\Lib\Think\Util\Dispatcher.class.php private static function parsePathInfo() $pathInfo = array(); if(C('URL_PATHINFO_MODEL')==2) $paths = explode(C('URL_PATHINFO_DEPR'),trim($_SERVER['PATH_INFO'],'/')); $APP_LEVEL_DOMAINS = false; $domain = array_shift(explode('.',$_SERVER['HTTP_HOST'])); if(in_array($domain, C('APP_LEVEL_DOMAINS'), true)) $APP_LEVEL_DOMAINS = true; if(C('APP_LEVEL_DOMAIN_DEPLOY_ON') && $APP_LEVEL_DOMAINS) if(isset($paths[0]) && strtolower($paths[0]) == strtolower($domain)) array_shift ($paths); $pathInfo[C('VAR_MODULE')] = ucfirst(array_shift(explode('.',$_SERVER['HTTP_HOST']))); $pathInfo[C('VAR_ACTION')] = array_shift($paths); else $groupApp = C('APP_GROUP_LIST'); if ($groupApp) $arr = array_map('strtolower',explode(',',$groupApp)); $pathInfo[C('VAR_GROUP')] = in_array(strtolower($paths[0]),$arr) ? array_shift($paths) : ''; $pathInfo[C('VAR_MODULE')] = array_shift($paths); $pathInfo[C('VAR_ACTION')] = array_shift($paths); for($i = 0, $cnt = count($paths); $i < $cnt; $i++) if(isset($paths[$i+1])) $pathInfo[$paths[$i]] = (string)$paths[++$i]; elseif($i==0) $pathInfo[$pathInfo[C('VAR_ACTION')]] = (string)$paths[$i]; else $res = preg_replace('@(\w+)'.C('URL_PATHINFO_DEPR').'([^,\/]+)@e', '$pathInfo[\'\\1\']="\\2";', $_SERVER['PATH_INFO']); return $pathInfo; 用上面这个函数替换后就能支持二级域名了,不过还有事情要做。在项目的配置文件(config.php)里面加上二级域名支持。
return array( #'配置项'=>'配置值' 'DEBUG_MODE' => true, 'URL_MODEL' => 2, 'APP_LEVEL_DOMAIN_DEPLOY_ON' => true, #标识打开二级域名支持 'APP_LEVEL_DOMAINS' => array('sites','seo'), #例:如果是sites.ys250.com 那么将采用2级别域名的方式 #'URL_html_SUFFIX'=>'.html', #'URL_ROUTER_ON' => true
结果如:
http://www.ys250.com/sites/ip (old)
http://sites.ys250.com/ip (new)
http://www.ys250.com/seo (old)
http://seo.ys250.com (new)
http://ips.ys250.com/ 让采用老的方式,因为ips没有开通二级支持支持,因此大家要开通自己的二级域名请设置APP_LEVEL_DOMAINS这个数组。
Mybatis练习(一对多:查询所有的一级分类,同时查询二级分类,以及查询二级分类下的所有图书)
文章目录
建表语句与表数据
CREATE TABLE category(
cid VARCHAR(32) PRIMARY KEY,
cname VARCHAR(50),
parent_id VARCHAR(32),
desc
VARCHAR(100)
);
INSERT INTO category(cid,cname,parent_id,desc
) VALUES(‘c100’,‘童书’,‘0’,‘一级分类’);
INSERT INTO category(cid,cname,parent_id,desc
) VALUES(‘c200’,‘电子书’,‘0’,‘一级分类’);
INSERT INTO category(cid,cname,parent_id,desc
) VALUES(‘c101’,‘中国儿童文学’,‘c100’,‘二级分类’);
INSERT INTO category(cid,cname,parent_id,desc
) VALUES(‘c102’,‘婴儿读物’,‘c100’,‘二级分类’);
INSERT INTO category(cid,cname,parent_id,desc
) VALUES(‘c201’,‘武侠小说’,‘c200’,‘二级分类’);
INSERT INTO category(cid,cname,parent_id,desc
) VALUES(‘c202’,‘侦探推理’,‘c200’,‘二级分类’);
CREATE TABLE book(
bid VARCHAR(32) PRIMARY KEY,
title VARCHAR(50),
price DOUBLE(6,2),
author VARCHAR(50),
cid VARCHAR(32)
);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b001’,‘学会管自己(歪歪兔独立成长童话)’,64.00,‘陈梦敏’,‘c101’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b002’,‘写给儿童的中国历史’,177.50,‘陈卫平’,‘c101’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b003’,‘小鸡球球触感玩具书’,125.00,’[日]入山智’,‘c102’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b004’,‘天龙八部’,12.00,‘金庸’,‘c201’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b005’,‘朱贞木短篇小说集(全套装共8册)’,103,‘朱贞木’,‘c201’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b006’,‘笑傲江湖(新修版)一’,1200,‘金庸’,‘c201’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b007’,‘解忧杂货店’,19.99,’(日)东野圭吾’,‘c202’);
INSERT INTO book(bid,title,price,author,cid) VALUES(‘b008’,‘三口棺材’,8.99,’(美)约翰·迪克森·卡尔’,‘c202’);
搭建环境
JavaBean
Category类
@Entity(name = "category")
public class Category {
@Id
private String cid;
@Column(name = "cname")
private String cname;
@Column(name="`desc`")
private String desc;
// 父分类id
@Column(name="parent_id")
private String parentId;
//多对一:多个二级分类 对应 一个一级分类
private Category category;
//一对多:一个一级分类 对应 多个二级分类
private List<Category> categoryList = new ArrayList<>();
(get,set,toString方法)
Book类
@Entity(name="book")
public class Book {
@Id
private String bid;
@Column(name="title")
private String title;
@Column(name="price")
private Double price;
@Column(name="author")
private String author;
@Column(name="cid")
private String cid;
//多对一:多本书 属于 一个分类
private Category category;
(get,set,toString方法)
查询所有一级分类
查询指定父分类的所有分类(单表)
功能接口
package com.czxy.ssm.mapper;
import com.czxy.ssm.domain.Category;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author sky
*/
public interface CategoryMapper {
/**
* 查询指定父分类的所有分类
* @param parentId
* @return
*/
public List<Category> selectAll(@Param("parentId") String parentId);
}
编写SQL语句
<?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.czxy.ssm.mapper.CategoryMapper">
<resultMap id="categoryResultMap" type="category">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<result property="desc" column="desc"></result>
<result property="parentId" column="parent_id"></result>
</resultMap>
<select id="selectAll" resultMap="categoryResultMap">
SELECT * FROM category WHERE parent_id = #{parentId}
</select>
</mapper>
测试类
package com.czxy.ssm;
import com.czxy.ssm.domain.Category;
import com.czxy.ssm.mapper.CategoryMapper;
import com.czxy.ssm.utils.MyBatisUtils;
import java.util.List;
/**
* @author sky
*/
public class Test01_XML_SelectAll {
public static void main(String[] args) {
//1 获得mapper
CategoryMapper categoryMapper = MyBatisUtils.getMapper(CategoryMapper.class);
//2 查询所有的一级分类,同时查询二级分类,同时查询关联书籍
List<Category> oneList = categoryMapper.selectAll("0");
for (Category category : oneList) {
System.out.println(category);
}
//3 释放
MyBatisUtils.commitAndclose();
}
}
查询指定父分类的所有分类,含二级分类(多表)
修改JavaBean
修改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.czxy.ssm.mapper.CategoryMapper">
<resultMap id="categoryResultMap" type="category">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<result property="desc" column="desc"></result>
<result property="parentId" column="parent_id"></result>
<!--一对多:一个一级分类 对应 多个二级分类-->
<collection property="categoryList" column="cid" select="com.czxy.ssm.mapper.CategoryMapper.selectAll"></collection>
</resultMap>
<select id="selectAll" resultMap="categoryResultMap">
SELECT * FROM category WHERE parent_id = #{parentId}
</select>
</mapper>
修改测试类
package com.czxy.ssm;
import com.czxy.ssm.domain.Category;
import com.czxy.ssm.mapper.CategoryMapper;
import com.czxy.ssm.utils.MyBatisUtils;
import java.util.List;
/**
* @author 桐叔
* @email liangtong@itcast.cn
*/
public class Test01_XML_SelectAll {
public static void main(String[] args) {
//1 获得mapper
CategoryMapper categoryMapper = MyBatisUtils.getMapper(CategoryMapper.class);
//2 查询所有的一级分类,同时查询二级分类,同时查询关联书籍
List<Category> oneList = categoryMapper.selectAll("0");
// 2.1 打印一级分类
for (Category oneCategory : oneList) {
System.out.println("one: " + oneCategory);
// 2.2 打印二级分类
for (Category twoCategory : oneCategory.getCategoryList()) {
System.out.println("two: " + twoCategory);
}
}
//3 释放
MyBatisUtils.commitAndclose();
}
}
查询指定父分类的所有分类,含二级分类,含书籍信息
编写BookMapper,功能方法selectAll(cid)
package com.czxy.ssm.mapper;
import com.czxy.ssm.domain.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author sky
*/
public interface BookMapper {
/**
* 查询指定分类id的所有书籍
* @param categoryId
* @return
*/
public List<Book> selectAll(@Param("categoryId") String categoryId);
}
编写BookMapper.xml ,实现SQL语句
<?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.czxy.ssm.mapper.BookMapper">
<select id="selectAll" resultType="book">
SELECT * FROM book WHERE cid = #{categoryId}
</select>
</mapper>
修改Category,提供 List<Book>
属性
//一对多:一个二级分类 对应 多个书籍
private List bookList = new ArrayList<>();
修改CategoryMapper.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.czxy.ssm.mapper.CategoryMapper">
<resultMap id="categoryResultMap" type="category">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<result property="desc" column="desc"></result>
<result property="parentId" column="parent_id"></result>
<!--一对多:一个一级分类 对应 多个二级分类-->
<collection property="categoryList" column="cid" select="com.czxy.ssm.mapper.CategoryMapper.selectAll"></collection>
<!--一对多:一个二级分类 对应 多个书籍 -->
<collection property="bookList" column="cid" select="com.czxy.ssm.mapper.BookMapper.selectAll"></collection>
</resultMap>
<select id="selectAll" resultMap="categoryResultMap">
SELECT * FROM category WHERE parent_id = #{parentId}
</select>
</mapper>
修改测试类,打印书籍信息
package com.czxy.ssm;
import com.czxy.ssm.domain.Book;
import com.czxy.ssm.domain.Category;
import com.czxy.ssm.mapper.CategoryMapper;
import com.czxy.ssm.utils.MyBatisUtils;
import java.util.List;
/**
* @author sky
*/
public class Test01_XML_SelectAll {
public static void main(String[] args) {
//1 获得mapper
CategoryMapper categoryMapper = MyBatisUtils.getMapper(CategoryMapper.class);
//2 查询所有的一级分类,同时查询二级分类,同时查询关联书籍
List<Category> oneList = categoryMapper.selectAll("0");
// 2.1 打印一级分类
for (Category oneCategory : oneList) {
System.out.println("one: " + oneCategory);
// 2.2 打印二级分类
for (Category twoCategory : oneCategory.getCategoryList()) {
System.out.println("two: " + twoCategory);
// 2.3 书籍信息
for (Book book : twoCategory.getBookList()) {
System.out.println(book);
}
}
}
//3 释放
MyBatisUtils.commitAndclose();
}
}
查询结果
以上是关于500分悬赏 thinkphp二级分类 包括数据库设计及代码的主要内容,如果未能解决你的问题,请参考以下文章
魔兽世界法师宏相关问题?(高级悬赏500分外加NGA推荐码)