使用idea中的springboot写项目
1.首先在pom文件里注入依赖
<!-- 添加父项目,当前项目是Springboot项目 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- mysql java驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!-- freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <!--druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!--dbcp --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version> </dependency> <!-- bonecp --> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> </dependencies>
2.写上启动类
package com.liujin.springboot.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author:liujin * @date: 2020/5/18 22:12 * @description:启动类 */ @SpringBootApplication @MapperScan("com.liujin.springboot.demo.mapper") public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class,args); } }
3.根据数据库创建实体类
里面的Data的注解包含了get,set方法
package com.liujin.springboot.demo.pojo; import lombok.Data; import java.util.Date; /** * @author:liujin * @date: 2020/5/18 22:18 * @description: */ @Data public class Loan { private int id; private String name; private String idCard; private String loanTime; private double loanMany; private String repaymentTime; private double repaymentMoney; private int bid; private Bank bank; }
4.接着是dao层
package com.liujin.springboot.demo.mapper; import com.liujin.springboot.demo.pojo.Bank; import com.liujin.springboot.demo.pojo.Loan; import org.springframework.stereotype.Repository; import java.util.List; /** * @author:liujin * @date: 2020/5/18 22:26 * @description: */ @Repository public interface LoanMapper { //查询,模糊查询 List<Loan> findAll(Loan loan); //添加 int addLoan(Loan loan); //查询银行 List<Bank> banks(); //回显 Loan toUpdate(String id); //xiugai int update(Loan loan); }
5.service层
package com.liujin.springboot.demo.service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.liujin.springboot.demo.mapper.LoanMapper; import com.liujin.springboot.demo.pojo.Bank; import com.liujin.springboot.demo.pojo.Loan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author:liujin * @date: 2020/5/18 22:29 * @description:分页查询 */ @Service public class LoanService { @Autowired LoanMapper loanMapper; public PageInfo<Loan> findAll(Loan loan,Integer pageNum,Integer pageSize){ PageHelper.startPage(pageNum,pageSize); List<Loan> loanList = loanMapper.findAll(loan); return new PageInfo<Loan>(loanList); } public List<Bank> banks(){ List<Bank> banks = loanMapper.banks(); return banks; } public int addLoan(Loan loan){ int i = loanMapper.addLoan(loan); return i; } public Loan toUpdate(String id){ Loan loan = loanMapper.toUpdate(id); return loan; } public int update(Loan loan){ int update = loanMapper.update(loan); return update; } }
6.controller层
package com.liujin.springboot.demo.controller; import com.github.pagehelper.PageInfo; import com.liujin.springboot.demo.pojo.Bank; import com.liujin.springboot.demo.pojo.Loan; import com.liujin.springboot.demo.service.LoanService; 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.List; /** * @author:liujin * @date: 2020/5/18 22:36 * @description: */ @Controller public class LoanController { @Autowired LoanService loanService; @RequestMapping("findAll") public String list(ModelMap modelMap, Loan loan, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize){ PageInfo<Loan> info = loanService.findAll(loan, pageNum, pageSize); List<Bank> banks = loanService.banks(); modelMap.addAttribute("info",info); modelMap.addAttribute("loan",loan); modelMap.addAttribute("banks",banks); return "list"; } @RequestMapping("toAdd") public Object toAdd(ModelMap modelMap){ List<Bank> banks = loanService.banks(); modelMap.addAttribute("banks",banks); return "add"; } @RequestMapping("add") public Object addLoan(Loan loan){ int i = loanService.addLoan(loan); return "redirect:findAll"; } @RequestMapping("toUpdate") public Object toUpdate(ModelMap modelMap,String id){ Loan loan = loanService.toUpdate(id); List<Bank> banks = loanService.banks(); modelMap.addAttribute("banks",banks); modelMap.addAttribute("loan",loan); return "update"; } @RequestMapping("update") public Object update(Loan loan){ loanService.update(loan); return "redirect:findAll"; } }
7.SQL语句写在resources里创建一个文件夹mappers,创建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.liujin.springboot.demo.mapper.LoanMapper"> <select id="findAll" resultMap="Loan" resultType="com.liujin.springboot.demo.pojo.Loan"> select * from t_loan join t_bank on t_loan.bid=t_bank.bid <where> <if test="name!=null and name!=''"> and name like concat('%',#{name},'%') </if> <if test="bid!=null and bid!=''"> and t_loan.bid=#{bid} </if> <if test="loanTime!=null and loanTime!=''"> and loanTime >=#{loanTime} </if> <if test="repaymentTime!=null and repaymentTime!=''"> and repaymentTime <=#{repaymentTime} </if> </where> </select> <resultMap id="Loan" type="com.liujin.springboot.demo.pojo.Loan"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="idCard" property="idCard"></result> <result column="loanTime" property="loanTime"></result> <result column="loanMany" property="loanMany"></result> <result column="repaymentTime" property="repaymentTime"></result> <result column="repaymentMoney" property="repaymentMoney"></result> <result column="bid" property="bid"></result> <association property="bank" javaType="com.liujin.springboot.demo.pojo.Bank"> <id column="bid" property="bid"></id> <result column="bname" property="bname"></result> <result column="interest" property="interest"></result> </association> </resultMap> <!--查询银行--> <select id="banks" resultType="com.liujin.springboot.demo.pojo.Bank"> select * from t_bank </select> <insert id="addLoan"> insert into t_loan values(null,#{name},#{idCard},#{loanTime},#{loanMany},#{repaymentTime},#{repaymentMoney},#{bid}) </insert> <select id="toUpdate" resultMap="Loan"> select * from t_loan join t_bank on t_loan.bid=t_bank.bid where id=#{id} </select> <update id="update"> update t_loan set name=#{name},idCard=#{idCard},loanTime=#{loanTime},loanMany=#{loanMany},repaymentTime=#{repaymentTime} ,bid=#{bid} where id=#{id} </update> </mapper>
8.在templates文件里创建页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>贷款列表</title> <link rel="stylesheet" href="/css/bootstrap.css"> <script src="/js/jquery.min.js"></script> <script src="/js/bootstrap.min.js"></script> </head> <body> <form action="/findAll"> 贷款人:<input type="text" name="name" value="${loan.name!}"> 贷款银行:<select name="bid"> <#list banks as banks> <option value="${banks.bid}">${banks.bname}</option> </#list> </select> 贷款开始时间:<input type="date" name="loanTime" value="${loan.loanTime!}"> 贷款结束时间:<input type="date" name="repaymentTime" value="${loan.repaymentTime!}"> <button>查询</button> <a href="toAdd">贷款</a> </form> <table class="table"> <tbody> <#list info.list as item> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.idCard}</td> <td>${item.loanTime}</td> <td>${item.loanMany}</td> <td>${item.repaymentTime}</td> <td>${item.repaymentMoney}</td> <td>${item.bank.bname}</td> <td><a href="toUpdate?id=${item.id}">还款</a></td> </tr> </#list> </tbody> </table> <a href="/findAll?pageNum=${info.pageNum-1}">上一页</a> <a href="/findAll?pageNum=${info.pageNum+1}">下一页</a> </body> </html>
9.application.properties文件里设置各种配置
#应用名称 spring.application.name=springboot-demo #设置端口号 #server.port=8080 #设置web应用的上下文 server.servlet.context-path=/ #指定环境配置,指定了dev之后,它会加载application和application-dev的配置 spring.profiles.active=dev #数据源 spring.datasource.url=jdbc:mysql://localhost:3306/cms_week01?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.zaxxer.hikari.HikariDataSource #mybatis配置 mybatis.type-aliases-package=com.liujin.springboot.demo.pojo mybatis.mapper-locations=classpath:/mapper/*.xml # FreeMarker Mvc配置 # 编码格式 spring.freemarker.charset=UTF-8 # freemarker模板后缀 默认是 .ftl spring.freemarker.suffix=.html #模板加载路径,默认路径是 classpath:/templates/ spring.freemarker.template-loader-path=classpath:/templates #Content-Type值 spring.freemarker.content-type=text/html;charset=utf-8 #禁用模板缓存 spring.freemarker.cache=false #数字格式化 spring.freemarker.settings.number_format=0.## #自定义属性配置 #文件访问的前缀 file.domain=http://localhost:${server.port}/ #文件件的上传位置 file.path=D:\\pic\\ #定义静态文件的访问目录,图片上传位置的绑定 spring.resources.static-locations=file:${file.path},classpath:/public/ #配置文件上传 #1024000 spring.servlet.multipart.max-file-size=1MB spring.servlet.multipart.max-request-size=10MB #日志 #日志保存位置 logging.file.path=D:\log\\ #日志的级别 logging.level.com.zhanggm.springboot.demo=debug #日志文件的大小,如果超过最大设置,就会生成一个新的文件 logging.file.max-size=10MB
application-dev.properties
#设置端口号 server.port=90 spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
application-prod.properties
server.port=8082
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
application-test.properties
server.port=8083
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/cms_week01 jdbc.username=root jdbc.password=root
测试类
package com.liujin.springboot.demo; import com.github.pagehelper.PageInfo; import com.liujin.springboot.demo.pojo.Loan; import com.liujin.springboot.demo.service.LoanService; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * @author:liujin * @date: 2020/5/19 23:21 * @description: */ @RunWith(SpringRunner.class) @SpringBootTest public class LoanServiceTest { private Logger logger= LoggerFactory.getLogger(getClass()); @Autowired private LoanService loanService; @Test public void pageInfoTest(){ PageInfo<Loan> info = loanService.findAll(null, 1, 2); logger.info("list:{}",info.getList()); } }