1、依赖。pom.xml
<!--配置JDK版本,可以不用配置-->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 继承springboot默认配置,引入springboot相关的包,Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!--引入web相关的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--进行热部署,开启开发者模式,项目上线的时候将热部署关掉-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 数据库-驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 阿里巴巴数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency>
<!-- Junit4单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--必须有才能编译jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--引入maven相关的插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--关闭热部署。此配置在不同环境中可能起作用-->
<!--<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>-->
</plugin>
</plugins>
</build>
2、配置application.peoperties
#设置服务器端口号
server.port=8080
#设置数据库连接四大参数
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/kgc1?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.mvc.view.prefix=/jsp/
spring.mvc.view.suffix=.jsp
#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
mybatis.mapper-locations=classpath:mapper/*.xml
springboot配置文件原版:application.yml
yml文件对格式要求严格,注意每一行配置后面不要加空格
#配置后台admin的端口号
server:
port: 8081
#给后台admin模块设置名字
spring:
application:
name: hbuy-admin
#配置访问页面路径的前后缀
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
#将后台模块注册到注册中心
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10010/eureka
djin:
#配置虚拟路径
imagesPath: file:/F:/k9503/stu/img
#日志的配置
logging:
level:
org:
springframework: error
com:
java:
admin:
mapper: DEBUG
3、配置启动类:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.kgc.study.mapper")
public class StudyApplication {
public static void main(String[] args) {
SpringApplication.run(StudyApplication.class, args);
}
}
4、做简单的分页查询和删除:controller
import com.kgc.study.pojo.Emp;
import com.kgc.study.service.EmpService;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/emp")
public class EmpController {
@Resource
private EmpService service;
/**删除单条数据
* author:付浩
* @param id
* @return
*/
@RequestMapping("/delOne")
public @ResponseBody boolean delOne(@RequestParam String id){
return service.delOne(id);
}
/**
* 查询、分页
* author:付浩
* @return
*/
@RequestMapping("/all")
public @ResponseBody
Map<String,Object> selectEmpAll(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "5") Integer limit){
return service.selectEmpAll(page,limit);
}
/**前端页面跳转
* author:付浩
* @return
*/
@RequestMapping("/toEmp")
public String toEmp() {
return "show";
}
}
4-1:service和serviceimpl
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.study.mapper.EmpMapper;
import com.kgc.study.pojo.Emp;
import com.kgc.study.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class EmpServiceImpl implements EmpService {
@Resource
private EmpMapper empMapper;
@Override
public Map<String,Object> selectEmpAll(Integer pageNum,Integer pageSize) {
Map<String,Object> map=new HashMap<>();
//开启分页
PageHelper.startPage(pageNum,pageSize);
//开始查询
List<Emp> empList=empMapper.selectEmpAll();
//包装查询结果
PageInfo<Emp> pageInfo=new PageInfo(empList);
map.put("code","0");
map.put("msg","success");
map.put("count",pageInfo.getTotal());
map.put("data",pageInfo.getList());
return map;
}
@Override
public boolean delOne(String id) {
return empMapper.delOne(Integer.parseInt(id))!=0;
}
}
4-2:mapper接口:dao
package com.kgc.study.mapper;
import com.kgc.study.pojo.Emp;
import java.util.List;
public interface EmpMapper {
List<Emp>selectEmpAll();
int delOne(Integer id);
}
5、mapper。EmpMapper.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的接口类地址 相当于dao接口实现类 -->
<mapper namespace="com.kgc.study.mapper.EmpMapper">
<select id="selectEmpAll" resultType="com.kgc.study.pojo.Emp">
select id,name,age,sex,birth,bonus,salary
,deptid deptId
from emp
</select>
<delete id="delOne">
DELETE FROM emp WHERE id=#{id}
</delete>
</mapper>