Idea练习简单的列表添加修改
启动类
package com.wpx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wpx.mapper")
public class StartProp {
public static void main(String[] args) {
System.out.println("启动");
SpringApplication.run(StartProp.class,args);
}
}
实体类
package com.wpx.pojo;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
public class Loan {
private Integer id;
private String name;
private String idCard;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date ltime;
private Double lmoney;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date rtime;
private Double rmoney;
private Integer bankid;
private Integer bankId;
private String bname;
private Double interest;
private Integer yearNum;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date t1;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date t2;
}
-----------------------------------------------------------------
package com.wpx.pojo;
import lombok.Data;
@Data
public class Bank {
private Integer bankId;
private String bname;
private Double interest;
}
Mapper接口
package com.wpx.mapper;
import com.wpx.pojo.Loan;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface LoanMapper {
int add(Loan loan);
List<Loan> list(Loan loan);
int update(Loan loan);
Loan toUpdate(Integer id);
}
service接口实现类
package com.wpx.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wpx.mapper.LoanMapper;
import com.wpx.pojo.Loan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LoanService {
@Autowired
private LoanMapper loanMapper;
public PageInfo<Loan> list(Loan loan, Integer pageNum, Integer pageSize){
PageHelper.startPage(pageNum,pageSize);
return new PageInfo<Loan>(loanMapper.list(loan));
}
public int add(Loan loan){
int i=loanMapper.add(loan);
return i;
}
public int update(Loan loan) {
int i=loanMapper.update(loan);
return i;
}
public Loan toUpdate(Integer id) {
return loanMapper.toUpdate(id);
}
}
Controller控制层
package com.wpx.controller;
import com.github.pagehelper.PageInfo;
import com.wpx.pojo.Loan;
import com.wpx.service.LoanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
@Controller
public class LoanController {
@Autowired
private LoanService loanService;
private Logger logger= LoggerFactory.getLogger(LoanController.class);
@RequestMapping("list")
public String list(ModelMap modelMap, Loan loan, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
PageInfo<Loan> list = loanService.list(loan, pageNum, pageSize);
modelMap.addAttribute("info",list);
modelMap.addAttribute("loan",loan);
logger.info("Controller分页"+pageNum);
logger.info("Controller分页"+list);
return "list";
}
@RequestMapping("toadd")
public String toadd(){
return "add";
}
@RequestMapping("add")
public String add(Loan loan){
loan.setLtime(new Date());
System.out.println(loan);
logger.info("Controller Add 贷款成功"+loan);
loanService.add(loan);
return "redirect:list";
}
@ResponseBody
@RequestMapping("update")
public Object update(Integer id){
Loan loan= loanService.toUpdate(id);
logger.info("Controller toUpdate"+loan+"id"+id);
Double d=(loan.getYearNum()*loan.getInterest()*loan.getLmoney());
loan.setRmoney((d/100)+loan.getLmoney());
loan.setRtime(new Date());
logger.info("Controller Update 还款成功"+loan);
int i=loanService.update(loan);
return i>0;
}
}
Mapperxml配置文件
<?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.wpx.mapper.LoanMapper">
<select id="list" resultType="Loan">
SELECT * from t_loan l LEFT JOIN t_bank b on(l.bankid=b.bankId)
<where>
<if test="name!=null and name!=''">
and l.name like concat('%',#{
name},'%')
</if>
<if test="bankid!=0 and bankid!=null">
and l.bankid=#{
bankid}
</if>
<if test="t1!=null">
and l.ltime >= #{
t1}
</if>
<if test="t2!=null">
and l.ltime <= #{
t2}
</if>
</where>
</select>
<insert id="add">
insert into t_loan(name,idCard,ltime,lmoney,bankid) value(#{
name},#{
idCard},#{
ltime},#{
lmoney},#{
bankid})
</insert>
<update id="update">
update t_loan set rtime=#{
rtime},rmoney=#{
rmoney} where id=#{
id}
</update>
<select id="toUpdate" resultType="loan">
SELECT *,TIMESTAMPDIFF(YEAR,l.ltime,DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))+1 as yearNum from t_loan l LEFT JOIN t_bank b on(l.bankid=b.bankId)
where l.id=#{
id}
</select>
</mapper>
测试类
package com.wpx.test;
import com.wpx.pojo.Loan;
import com.wpx.service.LoanService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.sql.SQLOutput;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestLoan {
@Autowired
private LoanService loanService;
@Test
public void huanTest(){
Loan loan= loanService.toUpdate(1);
Double d=(loan.getYearNum()*loan.getInterest()*loan.getLmoney());
loan.setRmoney((d/100)+loan.getLmoney());
loan.setRtime(new Date());
int i=loanService.update(loan);
if (i>0)
System.out.println("还款成功");
}
@Test
public void addTest(){
Loan loan = new Loan();
loan.setId(3);
int i=loanService.add(loan);
if (i>0)
System.out.println("添加成功");
}
}
前台列表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script type="text/javascript">
function toadd() {
location="toadd";
}
function update(id) {
$.post("update",{
id:id},function(flag) {
if (flag){
alert("还款成功");
location="list";
}else {
alert("还款失败");
}
})
}
</script>
<form action="list" method="get">
姓名:<input type="text" name="name">
<select name="bankid">
银行:<option value="0">请选择</option>
<option value="1">人民</option>
<option value="2">工商</option>
<option value="3">建设</option>
</select>
日期:<input type="date" name="t1">- <input type="date" name="t2">
<button>提交</button>
</form>
<button onclick="toadd()">添加</button>
<table class="table">
<tr>
<td>id</td>
<td>贷款人</td>
<td>贷款人身份证</td>
<td>贷款时间</td>
<td>贷款金额</td>
<td>贷款利息</td>
<td>还款时间</td>
<td>还款金额</td>
<td>贷款银行</td>
<td>操作</td>
</tr>
<#list info.list as item>
<tr>
<td>${
item.id!}</td>
<td>${
item.name!}</td>
<td>${
item.idCard!}</td>
<td>${
(item.ltime?string('yyyy-MM-dd'))!}</td>
<td>${
item.lmoney!}</td>
<td>${
item.interest!}</td>
<td>${
(item.rtime?string('yyyy-MM-dd'))!}</td>
<td>${
item.rmoney!}</td>
<td>${
item.bname!}</td>
<td>
<button onclick="update(${item.id})">
还款
</button>
</td>
</tr>
</#list>
</table>
<a href="list?pageNum=1">首页</a>
<a href="list?pageNum=${info.pageNum-1}">上一页</a>
<a href="list?pageNum=${info.pageNum+1}">下一页</a>
<a href="list?pageNum=${info.pages}">尾页</a>
</body>
</html>
前台添加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<form action="add" method="post">
贷款人 <input type="text" name="name"><br>
贷款银行 <select name="bankid">
<option value="0">请选择</option>
<option value="1">人民</option>
<option value="2">工商</option>
<option value="3">建设</option>
</select><br>
贷款人身份证 <input type="text" name="idCard"><br>
贷款金额 <input type="text" name="lmoney"><br>
<button>提交</button>
</form>
</body>
</html>