简单的springMVC + mybatis 编写程序流程

1.首先创建数据库表

2.通过 mybatis的反向映射生成相关的映射文件,实体类,数据访问文件(参考http://www.cnblogs.com/smileberry/p/4145872.html)

generatorConfiguration.xml,mysql驱动包,mybatis-generator-core 包 

填写数据库驱动包的本地位置 绝对路径,数据库链接,用户名称,密码,实体model 包名和当前项目中包 的绝对路径,映射文件,数据访问DAO文件 ,最后数据库的表

运行:

java -jar mybatis-generator-core-1.3.2.jar -configfile  generatorconfig.xml -overwrite

-----------------------------------------------------------------------------------------

<generatorConfiguration>
<classPathEntry
location="D:/jtools/pages/mysql-connector-java-5.1.27.jar" />
<context id="context1">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://  " userId=""
password="" />
<javaModelGenerator targetPackage="com.datayes.paas.cloud.fee.api.common.model"
targetProject="D:/workroom/cloud-paas/branches/cloud-fee-dev/src/main/java" />
<sqlMapGenerator targetPackage="mappers"
targetProject="D:/workroom/cloud-paas/branches/cloud-fee-dev/src/main/resources" />
<javaClientGenerator targetPackage="com.datayes.paas.cloud.fee.api.business.mapper"
targetProject="D:/workroom/cloud-paas/branches/cloud-fee-dev/src/main/java"
type="XMLMAPPER" />
<table tableName="user_profile" domainObjectName="UserProfile" />

</context>
</generatorConfiguration>

-----------------------------------------------------------------------------------------------------

3.在映射文件***Mapper.xml中 可以填写自己的业务查询语句,如果有关联必要,需要编写结果展示类,

<resultMap type="com.datayes.paas.utility.model.Page" id="pageAccountReportMap">
<id column="totalcount" property="totalcount"/>
<result column="pageIndex" property="pageIndex"/>
<result column="pageSize" property="pageSize"/>
<collection property="data" javaType="arraylist" resultMap="ReportMap"/>
</resultMap>
<resultMap type="com.datayes.paas.cloud.fee.api.common.entity.AccoutReportShow" id="ReportMap">
<result property="" column=""/>
<result property="reportId" column="reportId"/>
<result property="accountId" column="accountId"/>
<result property="accountName" column="accountName"/>
<result property="accountTransactionType" column="accountTransactionType"/>
<result property="reportDate" column="reportDate"/>
<result property="reportName" column="reportName"/>
<result property="submitTime" column="submitTime"/>
<result property="deadLine" column="deadLine"/>
<result property="reportStatus" column="reportStatus"/>
</resultMap>
<select resultMap="pageAccountReportMap" id="searchReport">
select @row_num totalcount,#{pageIndex} as pageIndex, #{pageSize} as pageSize,result_order.* 
from account_report as ar
inner join ( 
select @row_num:=@row_num+1 AS row_num ,a.id as accountId,r.id as reportId,a.name as accountName,a.transaction_type as accountTransactionType,r.report_date as reportDate,
r.name as reportName,r.submit_time as submitTime,r.deadline as deadLine,r.`status` as reportStatus
from account a,account_report r ,(SELECT @row_num:=0) AS rowvar 
where r.account_id=a.id 
<if test=" transactionTypes != null">
and a.transaction_type in ( #{transactionTypes} )
</if>
<if test=" reportStatuss != null">
and r.`status` in ( #{reportStatuss} )
</if>
<if test="reportTypes != null">
and r.`type` in ( #{reportTypes} )
</if>
) AS result_order on ar.id=result_order.reportId 
and result_order.row_num &gt; (#{pageSize}*(#{pageIndex}-1)) and result_order.row_num &lt;= (#{pageSize}*#{pageIndex})
order by result_order.row_num
</select>

4.DAO 文件***Mapper.java 中写相应的方法 

Page<AccoutReportShow> searchReport(
@Param("reportTypes") String reportTypes,
@Param("transactionTypes") String transactionTypes,@Param("reportStatuss") String reportStatuss,
@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

Page 是分页类,这里利用泛型赋值给List参数

5.服务层

@Service
public class AccountReportService {
@Autowired
AccountReportMapper accountReportMapper;
public Page<AccoutReportShow> searchReportList(AccoutReportSearchRequest searchRequest){
return accountReportMapper.searchReport( searchRequest.getReportTypes(), searchRequest.getTransactionTypes(), searchRequest.getReportStatuss(), searchRequest.getPageIndex(), searchRequest.getPageSize());
}
}

6.控制层

@Controller
public class AccountReportController extends BaseController{
@Autowired
AccountReportService accountReportService;
@RequestMapping(value = "/accountReport/searchReport", method = RequestMethod.POST)
public Object searchReport(
@RequestParam(value = "reportTypes",required = false) String reportTypes,
@RequestParam(value = "transactionTypes",required = false) String transactionTypes,
@RequestParam(value = "reportStatuss",required = false) String reportStatuss,
@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
@RequestParam(value = "pageSize",required = false) Integer pageSize)throws DatayesException, ParseException {
if (pageSize == null)
pageSize = Constant.MAX_RECORD_NUM;
if (pageIndex == null)
pageIndex = 1;

AccoutReportSearchRequest searchRequest = new AccoutReportSearchRequest();
searchRequest.setPageIndex(pageIndex);
searchRequest.setPageSize(pageSize);
searchRequest.setReportStatuss(reportStatuss);
searchRequest.setReportTypes(reportTypes);
searchRequest.setTransactionTypes(transactionTypes);
Page<AccoutReportShow> list = accountReportService.searchReportList(searchRequest);
return list;
}
}

返回结果是JSON返回。

7.Page类

public class Page
{

    public Page()
    {
        pageIndex = 0;
        pageSize = 0;
    }

    public Page(List resList, int pageIndex, int pageSize)
    {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        if(resList != null && !resList.isEmpty())
        {
            totalcount = resList.size();
            int startpos = (pageIndex - 1) * pageSize;
            int endpos = pageIndex * pageSize - 1;
            if(resList.size() > startpos)
                data = resList.subList(startpos, resList.size() <= endpos ? resList.size() : endpos + 1);
        } else
        {
            totalcount = 0;
            data = new ArrayList();
        }
    }

    public Page(List data, int totalcount, int pageIndex, int pageSize)
    {
        this.totalcount = totalcount;
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        this.data = ((List) (data == null ? ((List) (new ArrayList())) : data));
    }

    public boolean isDataNotNullAndEmpty()
    {
        return data != null && !data.isEmpty();
    }

    public List getData()
    {
        return data;
    }

    public void setData(List data)
    {
        this.data = data;
    }

    public int getTotalcount()
    {
        return totalcount;
    }

    public void setTotalcount(int totalcount)
    {
        this.totalcount = totalcount;
    }

    public int getPageIndex()
    {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex)
    {
        this.pageIndex = pageIndex;
    }

    public int getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    private List data;
    private int totalcount;
    private int pageIndex;
    private int pageSize;
}

该流程是在环境搭建成功的基础之上的,是作者在公司实习过程中的编写流程。旨在熟悉流程,熟练掌握。

原文地址:https://www.cnblogs.com/dasheng-maritime/p/5782875.html