单表CRUD- Xxx管理模板
单表CRUD- Xxx管理模板
Xxx管理后台命名模板
(1)TesXxxService
创建业务对象,调用业务方法
(2)IXxxService
(3)XxxServiceImpl
要用分页的话:设置参数,调用全查,包装成PageInfo
(4)IXxxDao
(5)XxxDaoImpl.xml
管理前台命名模板
1)XxxController(2)xxx-yyy.jsp
Ajax删除
此模板是基于敏捷开发,和单元测试驱动开发来进行的,各位开发者需要找出一套适合自己的开发路径,更清析的思路,更少的bug
Xxx管理分析步骤
(1)查看页面,定义XXController,每个功能页面都能用控制器打开,查看功能
(2)定义实体类与xml文件
(3)从测试开始,增删改查
(4)把业务类方法的增删改查编写出来
(5)把控制器类方法编写出来
(7)业务中,只有少数关联操作需要重点理解
查看页面,分析业务对象要定义什么方法
@Controller
@RequestMapping("/system/role")
public class RoleController {
private static final Logger l = LoggerFactory.getLogger(RoleController.class);
@RequestMapping(path="/toList",method ={ RequestMethod.GET, RequestMethod.POST})
public String toList(){
return "system/role/role-list";
}
@RequestMapping(path="/toAdd",method ={ RequestMethod.GET, RequestMethod.POST})
public String toAdd(){
return "system/role/role-add";
}
@RequestMapping(path="/toUpdate",method ={ RequestMethod.GET, RequestMethod.POST})
public String toUpdate(){
return "system/role/role-update";
}
@RequestMapping(path="/toRoleModule",method ={ RequestMethod.GET, RequestMethod.POST})
public String toRoleModule(){
return "system/role/role-module";
}
}
定义实体类
public class Role {
private String roleId ;
private String name ;
private String remark ;
private long orderNo ;
private String createBy ;
private String createDept ;
private Date createTime ;
private String updateBy ;
private Date updateTime ;
private String companyId ;
private String companyName ;
}
ajax提交用到的结果类
Result
如果觉得new 生成一个Result对象麻烦,可以在Result类中定义一个静态方法
,别的类可以直接调用静态方法进行
package com.zx.domain;
public class Result {
private int code;
private String msg;
private Object data;
public Result() { }
public Result(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static Result init(int code, String msg, Object data){
return new Result(code,msg,data);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
测试类
RoleTest: 使用注解创建业务对象,调用业务方法
import com.github.pagehelper.PageInfo;
import com.zx.domain.system.role.Role;
import com.zx.service.Role.IRoleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
public class TestRole {
@Autowired
IRoleService iRoleService;
@Test
public void test01(){
int currnt=1;
int pageSize=3;
String companyId="1";
PageInfo<Role> pageInfo= iRoleService.findByPage(currnt,pageSize,companyId);
System.out.println(pageInfo);
}
@Test
public void test02(){
//
//将一个表单数据保存在javaBean中,再将javaBean存到数据库
Role role = new Role();
role.setName("角色1");
role.setRemark("角色1的备注");
role.setCompanyId("1");
role.setCompanyName("吉首大学");
iRoleService.saveRole(role);
}
@Test
public void test03(){
//
//更新业务 先根据id查找出对应的一条记录,编辑它的值,再将记录保存到数据库中
String roleId="c77751fa-b6a5-45a6-a2b1-704f358e1bee";
Role role = iRoleService.findById(roleId);
//修改
role.setName("角色2");
role.setRemark("角色2备注");
//保存
iRoleService.updateRole(role);
}
@Test
public void test04(){
//
//删除业务,就是根据指定的id,删除数据库中的记录
String roleId="33";
//删除
iRoleService.deleteRole(roleId);
}
}
业务类
根据测试编写的业务类,来生成具体的业务类,业务接口,与业务方法
RoleServiceImpl
package com.zx.service.Role.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.dao.role.IRoleDao;
import com.zx.domain.system.role.Role;
import com.zx.service.Role.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
@Service
public class RoleServiceImpl implements IRoleService {
@Autowired
IRoleDao iRoleDao;
@Override
public PageInfo<Role> findByPage(int curr, int pageSize, String companyId) {
// start=(curr-1)*pageSize
PageHelper.startPage(curr,pageSize);
List<Role> list=iRoleDao.findAll(companyId);
// 全查语句会被自动的分为,一个查数量,一个limit分页语句
PageInfo<Role> pi=new PageInfo<>(list);
return pi;
}
@Override
public void saveRole(Role role) {
String uuid= UUID.randomUUID().toString();
role.setRoleId(uuid);
iRoleDao.save(role);
}
@Override
public Role findById(String roleId) {
return iRoleDao.findById(roleId);
}
@Override
public void updateRole(Role role) {
iRoleDao.update(role);
}
@Override
public void deleteRole(String roleId) {
iRoleDao.deleteById(roleId);
}
}
IRoleService
package com.zx.service.Role;
import com.github.pagehelper.PageInfo;
import com.zx.domain.system.role.Role;
public interface IRoleService {
PageInfo<Role> findByPage(int curr, int pageSize, String companyId);
void saveRole(Role role);
Role findById(String roleId);
void updateRole(Role role);
void deleteRole(String roleId);
}
Dao接口
根据业务编写的Dao接口,来生成Dao接口,xml文件,还有spring配置
IRoleDao
package com.zx.dao.role;
import com.zx.domain.system.role.Role;
import java.util.List;
public interface IRoleDao {
List<Role> findAll(String companyId);
void save(Role role);
Role findById(String roleId);
void update(Role role);
void deleteById(String roleId);
}
IRoleDao.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" >
<!--namespace: 需要映射的Dao接口类型-->
<mapper namespace="com.wzx.dao.system.role.IRoleDao">
<!-- resultType 指定一个java类型,要求表的字段与变量的名称一致
resultMap 适用不一致的情况-->
<resultMap id="roleMap" type="role">
<id column="role_id" property="roleId" />
<result column="name" property="name" />
<result column="remark" property="remark" />
<result column="order_no" property="orderNo" />
<result column="create_by" property="createBy" />
<result column="create_dept" property="createDept" />
<result column="create_time" property="createTime" />
<result column="update_by" property="updateBy" />
<result column="update_time" property="updateTime" />
<result column="company_id" property="companyId" />
<result column="company_name" property="companyName" />
</resultMap>
<select id="findAll" parameterType="string" resultMap="roleMap">
select * from pe_role where company_id=#{companyId} order by order_no
</select>
<insert id="save" parameterType="role">
insert into pe_role
(
role_id ,
name ,
remark ,
order_no ,
create_by ,
create_dept ,
create_time ,
update_by ,
update_time ,
company_id ,
company_name
)
values
(
#{roleId },
#{name },
#{remark },
#{orderNo },
#{createBy },
#{createDept },
#{createTime },
#{updateBy },
#{updateTime },
#{companyId },
#{companyName }
)
</insert>
<select id="findById" parameterType="string" resultMap="roleMap">
select * from pe_role where role_id=#{roleId}
</select>
<update id="update" parameterType="role">
update pe_role set
name = #{name },
remark = #{remark },
order_no = #{orderNo },
create_by = #{createBy },
create_dept = #{createDept },
create_time = #{createTime },
update_by = #{updateBy },
update_time = #{updateTime },
company_id = #{companyId },
company_name = #{companyName}
where role_id = #{roleId}
</update>
<delete id="deleteById" parameterType="string">
delete from pe_role where role_id=#{roleId}
</delete>
</mapper>
spring对Mybatis的整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--读取db.properties文件-->
<context:property-placeholder location="classpath:properties/db.properties"></context:property-placeholder>
<!--1.配置数据源 拿到四大信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2.配置Spring整合Mybatis *** 由Spring创建SqlSessionFactory对象 -->
<!--2.1 配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- com.zx.domain.company.Company 简化成company-->
<property name="typeAliasesPackage" value="com.zx.domain"/>
</bean>
<!--2.2 配置Dao接口所在包 动态代理 session.getMapper(Dao.class)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定Dao接口所在包-->
<property name="basePackage" value="com.zx.dao"/>
</bean>
</beans>
Service与Dao方法命名区别
dao的方法通常 update() save() deleteByXxx() findByXxx service 的方法通常
updateXxx() saveXxx() deleteXxxByXxx() findXxxByXxx
到此为止后台开发完毕,开始前台开发
之前是查看页面,现在是实现功能
BaseController
package com.zx.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class BaseController {
public String getLoginCompanyId(){
return "1";
}
//定义一个可以返回companyName
public String getLoginCompanyName(){
return "吉首大学";
}
//2 如果在父类中定义成员变量 request,session,response,并且注入对象
// 以后控制器方法可以直接使用
@Autowired
protected HttpServletRequest request;
//注入session
@Autowired
protected HttpSession session;
//注入response
@Autowired
protected HttpServletResponse response;//需要disable inspection
}
RoleController
package com.zx.web.controller.system.role;
import com.github.pagehelper.PageInfo;
import com.zx.domain.Result;
import com.zx.domain.system.role.Role;
import com.zx.service.Role.IRoleService;
import com.zx.web.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("system/role")
public class RoleCotroller extends BaseController {
private static final Logger l = LoggerFactory.getLogger(RoleCotroller.class);
@Autowired
IRoleService iRoleService;
@RequestMapping(path="/toList",method ={ RequestMethod.GET, RequestMethod.POST})
public String toList(@RequestParam(defaultValue = "1") int curr, @RequestParam(defaultValue = "3")int pageSize){
//调查询分页列表的方法
PageInfo<Role> pi = iRoleService.findByPage(curr, pageSize, getLoginCompanyId());
//将pi添加到页面
request.setAttribute("pi",pi);
return "system/role/role-list";
}
@RequestMapping(path="/toAdd",method ={ RequestMethod.GET, RequestMethod.POST})
public String toAdd(){
return "system/role/role-add";
}
@RequestMapping(path="/add",method ={ RequestMethod.GET, RequestMethod.POST})
public String add(Role role){//接收页面提交过来的表单
l.info("add role="+role);
role.setCompanyId(getLoginCompanyId());
role.setCompanyName(getLoginCompanyName());
iRoleService.saveRole(role);
return "redirect:/system/role/toList.do";
}
//${path}/system/role/toUpdate.do?roleId=${o.roleId}
@RequestMapping(path="/toUpdate",method ={ RequestMethod.GET, RequestMethod.POST})
public String toUpdate(String roleId){//需要使用参数接收提交的 roleId
Role role = iRoleService.findById(roleId);
//回显到页面
request.setAttribute("role",role);
return "system/role/role-update";
}
//action="${path}/system/role/update.do"
@RequestMapping(path="/update",method ={ RequestMethod.GET, RequestMethod.POST})
public String update(Role role){//需要接收编辑页面提交的表单数据
l.info("update role="+role);
//更新
//重新设置companyId companyName
role.setCompanyId(getLoginCompanyId());
role.setCompanyName(getLoginCompanyName());
iRoleService.updateRole(role);
return "redirect:/system/role/toList.do";
}
//${path}/system/role/delete.do?roleId='+id;
//{code:200,msg:'删除成功',data:null}
@RequestMapping(path="/delete",method ={ RequestMethod.GET, RequestMethod.POST})
public @ResponseBody
Object delete(String roleId){//参数接收页面js提交过来的roleId
try {
iRoleService.deleteRole(roleId);
//成功
return Result.init(200,"删除成功",null);
} catch (Exception e) {
e.printStackTrace();
//失败
return Result.init(-200,"删除失败",null);
}
}
@RequestMapping(path="/toRoleModule",method ={ RequestMethod.GET, RequestMethod.POST})
public String toRoleModule(){
return "system/role/role-module";
}
}
页面jsp
修改地址 修改表单 写el 写if标签 foreach标签 写ajax请求