水果商城—第一阶段(用户登录,类目管理
作者:互联网
1.创建项目,搭建环境
-
引入所需依赖
<dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet - JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!-- 上传组件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <dependency> <groupId>jsptags</groupId> <artifactId>pager-taglib</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.4.49.ALL</version> </dependency> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-core</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.2.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.gitee.qdbp.thirdparty</groupId> <artifactId>ueditor</artifactId> <version>1.4.3.3</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- mybatis物理分页插件 --> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>1.2.15</version> </dependency> <!-- 引入JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <!-- mybatis物理分页插件 --> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>1.2.15</version> </dependency> </dependencies>
-
搭建ssm框架
具体细节见:ssm框架整合。
注!:spring-mybatis中的数据库连接池中的url中添加:&allowMultiQueries=true。
具体见:https://www.cnblogs.com/jiabowenn/articles/15391419.html
2.搭建mvc三层架构
-
pojo(Manage,ItemCategory)
- 使用lombok,根据数据库字段创建对象属性
-
dao
- ManageMapper:
- interface ManageMapper extends BaseDao
- .xml
- 创建通用sql片段,方便后期复用
- 实现ManageMapper interface
- interface ManageMapper extends BaseDao
- ManageMapper:
-
service
- service接口,继承BaseService;serviceImpl实现service接口,并且继承BaseSeriviceImpl类
- 在serviceImpl类中创建属性ManageMapper和get方法,并且注入到spring中
-
service层和dao层测试
-
我是在写mapper.xml的时候,边写边测试。完成mapper测试后发现,这个可以在写完service层之后一起测,一次就可以搞定。
-
测试代码示例1:mapper测试
/*测试批量修改*/ @Test public void test10(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml"); ManageMapper managerMapper = context.getBean("manageMapper", ManageMapper.class); ArrayList<Manage> manages = new ArrayList<Manage>(); for (int i = 10; i <= 19; i++) { manages.add(new Manage(i,"admin_" + i + i,"00000" + i + i,"realName_" + i + i)); } managerMapper.updateBatch(manages); for (Manage manage : manages) { System.out.println(manage); } }
-
测试代码示例2:service测试
@Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ManageService manageService = context.getBean("manageServiceImpl", ManageService.class); List<Manage> manages = manageService.listAll(); for (Manage manage : manages) { System.out.println(manage); } }
-
controller:
- 在进行下面步骤的时候,一定要确保环境没有问题。
3.登录及相关功能的实现
-
管理员登录:首先在ManageController.java中添加@Controller注解,并在@RequestMapping()中配置此类的统一访问路径
登录访问地址:http://localhost:8080/shuiguoshop/login/login
/*管理員登陸前*/ @RequestMapping("/login") public String login(){ return "/login/mLogin";}
前端显示代码(简化版):只有一个form表单...
<form action="${ctx}/login/tologin" method="post"> <div class="panel login-box"> <div class="text-center margin-big padding-big-top"><h1>管理员登录</h1></div> <div class="panel-body" style="padding: 30px;padding-bottom: 10px; padding-top: 10px;"> <div class="form-group"> <div class="field field-icon-right"> <input type="text" class="input input-big" name="userName" value="admin" placeholder="登录账号" data-validate="required:请填写账号" /> <span class="icon icon-user margin-small"></span> </div> </div> <div class="form-group"> <div class="field field-icon-right"> <input type="password" class="input input-big" name="passWord" value="111111" placeholder="登录密码" data-validate="required:请填写密码" /> <span class="icon icon-key margin-small"></span> </div> </div> </div> <div style="padding: 30px;"> <input type="submit" class="button button-block bg-main text-big input-big" value="登录" /> </div> </div> </form>
-
管理员登录验证:接收从前端传来的用户名和密码,通过getByEntity进行查找。如果查询成功,跳转到管理员后台界面;失败,将查询对象置空,并重定向到“退出登录功能(/outManage)”
/*登录验证*/ @RequestMapping("/tologin") public String tologin(Manage manage, Model model){ Manage byEntity = manageService.getByEntity(manage); if (byEntity == null){ return "redirect:/login/outManage"; } model.addAttribute("manage",byEntity); return "/login/mIndex"; }
-
退出登录
将manage对象设置为空
4.管理员后台管理界面-类目管理功能
4.1类目管理
执行流程:
-
点击链接
-
访问:findBySql
-
通过sql查询出符合条件的Pager对象
@RequestMapping("/findBySql") public String findBySql(Model model,ItemCategory itemCategory){ String sql = "select * from item_category where isDelete = 0 and pid is null order by id"; Pager<ItemCategory> pagers = itemCategoryService.findBySqlRerturnEntity(sql); model.addAttribute("pagers",pagers); model.addAttribute("obj",itemCategory); return "itemCategory/itemCategory"; }
-
将查询出的对象传递给前端的显示界面(前端需要添加分页功能)
前端分页具体实现(这个分页具体怎么写还有待后续研究QwQ)
<tr>
<td colspan="8">
<div class="pagelist">
<!--分页开始-->
<pg:pager url="${ctx}/itemCategory/findBySql" maxIndexPages="5" items="${pagers.total}"
maxPageItems="15" export="curPage=pageNumber">
<pg:last>
共${pagers.total}条记录,共${pageNumber}页
</pg:last>
当前第${curPage}页
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl}">上一页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${curPage eq pageNumber}">
<font color="red">[${pageNumber}]</font>
</c:when>
<c:otherwise>
<a href="${pageUrl}">${pageNumber}</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl}">下一页</a>
</pg:next>
<pg:last>
<c:choose>
<c:when test="${curPage eq pageNumber}">
<font color="red">尾页</font>
</c:when>
<c:otherwise>
<a href="${pageUrl}">尾页</a>
</c:otherwise>
</c:choose>
</pg:last>
</pg:pager>
</div>
</td>
</tr>
4.2新增类目
执行流程:点击“新增目录” --->addItem(controller)--->add(页面)--->填好表单后提交,提交地址为:exAdd
点击新增类目跳转到类目添加页面
<div>
<span>新增类目</span>
</div>
<form action="${ctx}/itemCategory/exAdd" method="post">
请输入一级类目名称:<input type="text" name="name" required>
<input type="submit">
</form>
controller:
/*添加一级类目执行*/
@RequestMapping("/exAdd")
public String exAdd(ItemCategory itemCategory){
System.err.println(itemCategory.getName());
itemCategory.setIsDelete(0);
itemCategoryService.insert(itemCategory);
return "redirect:/itemCategory/findBySql";
}
4.3一级类目修改
执行流程:
-
点击按钮,跳转到itemCategory/updete,并且将该对象的id传回后端
-
后端的:“/update” 接收到id,并且根据id查询出该对象(load(Integer id))
@RequestMapping("/update") public String update(Model model,Integer id){ ItemCategory obj = itemCategoryService.load(id); model.addAttribute("obj",obj); return "itemCategory/update"; }
-
将该对象传回前端页面
-
前端页面接收对象名和id(隐藏域中)
<form action="${ctx}/itemCategory/exUpdate" method="post"> <input type="hidden" name="id" value="${obj.id}"> 一级类目名称:<input type="text" name="name" value="${obj.name}" required> <input type="submit" value="修改"> </form>
-
对象名显示在文本框中,id放在隐藏域中(给下一步,根据id修改对象提供参数id)
-
提交表单后,“exUpdate”接收到前端数据:id 和 修改后的类目名
-
执行修改操作,之后重定向回findBySql。(之前findBySql一直写成fingBySql,所以导致重定向失败)
/*修改一级分类功能*/ @RequestMapping("/exUpdate") public String exUpdate(ItemCategory itemCategory){ itemCategoryService.updateById(itemCategory); return "redirect:/itemCategory/findBySql"; }
4.4一级类目删除
执行流程:
-
点击删除,跳转到“/delete”,并将要删除的对象的id传向后端
-
在/delete中,首先,通过id搜索到该对象(load),并将isDelete属性置为1。
-
通过sql,将该类目下的所有二级类目isDelete属性都置为1
-
重定向回findBySql
@RequestMapping("/delete") public String delete(Integer id){ ItemCategory load = itemCategoryService.load(id); load.setIsDelete(1); //删除本身 itemCategoryService.updateById(load); //将下级也删除 String sql = "update item_category set isDelete = 1 where pid =" + id; itemCategoryService.updateBysql(sql); return "redirect:/itemCategory/findBySql"; }
4.5查看二级类目:
执行流程:
-
点击查看二级目录,将该类的id作为pid传向后端
-
通过查询,得到pager对象
-
将pager对象传递回前端页面进行分页显示
/* * 分页查询类目列表2 * */ @RequestMapping("/findBySql2") public String findBySql2(Model model,ItemCategory itemCategory){ String sql = "select * from item_category where isDelete = 0 and pid = " + itemCategory.getPid() + " order by id"; Pager<ItemCategory> pagers = itemCategoryService.findBySqlRerturnEntity(sql); model.addAttribute("pagers",pagers); model.addAttribute("obj",itemCategory); return "itemCategory/itemCategory2"; }
4.6新增二级类目功能
执行流程:
-
点击新增二级目录,将该二级目录的pid传回后端,并跳转到新增界面
<div> <a href="${ctx}/itemCategory/add2?pid=${obj.pid}" style="padding-left: 10px">新增二级类目</a> </div>
/** * 转向到新增二级目录 */ @RequestMapping("/add2") public String add2(Integer pid, Model model){ model.addAttribute("pid",pid); return "itemCategory/add2"; }
-
add2.jsp接收pid,储存在隐藏域中
<form action="${ctx}/itemCategory/exAdd2?pid=${pid}" method="post"> <input type="hidden" name="pid" value="${obj.pid}"> 请输入二级类目名称:<input type="text" name="name" required> <input type="submit" value="添加"> </form>
-
填好二级类目名称后,将名称与pid作为对象一同传递回”exAdd2“.
- 将该对象isDelete有null置为0
- 将该对象插入表中
- 重定向回”itemCategory/findBySql2?pid=“
/** * 新增二级类目执行 */ @RequestMapping("/exAdd2") public String exAdd2(ItemCategory itemCategory){ itemCategory.setIsDelete(0); itemCategoryService.insert(itemCategory); System.err.println(itemCategory.toString()); return "redirect:/itemCategory/findBySql2?pid=" + itemCategory.getPid(); }
4.78二级类目修改,删除
与之前一级类目修改删除相似,再次不再过多赘述。
标签:String,commons,itemCategory,类目,org,id,商城,第一阶段 来源: https://www.cnblogs.com/jiabowenn/p/15415657.html