1.新建一个model(启动类写dao层包扫描)
2.配置pom文件,加入依赖
<dependencies>
<!--mybatis依赖包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--jdbc依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
3.在java包和dao、service同级下创建启动类(启动类写dao层包扫描)
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.tedu.dao")
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class,args);
}
}
4.在resources中配置application.yml文件,才能正常启动启动类
#SpringBoot配置mysql信息
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///mybatisdb?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
#SpringBoot整合Mybatis配置
mybatis:
#指定UserMapper.xml文件的位置
mapper-locations: classpath:*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
5.开始写pojo实体类
package cn.tedu.pojo;
import lombok.Data;
@Data
public class Car {
private Integer id;
private String name;
private String color;
private double price;
}
6.写dao层接口
package cn.tedu.dao;
import cn.tedu.pojo.Car;
import java.util.List;
public interface CarDao {
/**
* 查询所有车辆信息
* @return 返回查出的list集合
*/
List<Car> findAll();
Car findById(Integer id);
}
7.根据dao层在resources中写CarMapper.xml (namespace是dao层接口全路径,sql语句的唯一标识id是接口中对应的方法名)
<?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="cn.tedu.dao.CarDao">
<sql id="sql1">id,name,color,price</sql>
<select id="findAll" resultType="cn.tedu.pojo.Car">
select <include refid="sql1"></include> from car
</select>
<select id="findById" resultType="cn.tedu.pojo.Car">
select <include refid="sql1"></include> from car
where id = #{id}
</select>
</mapper>
8.写service接口
package cn.tedu.service;
import cn.tedu.pojo.Car;
import java.util.List;
public interface CarService {
List<Car> selectAll();
Car selectById(Integer id);
}
9.写service实现类
package cn.tedu.service;
import cn.tedu.dao.CarDao;
import cn.tedu.pojo.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CarServiceImpl implements CarService {
@Autowired
private CarDao carDao;
@Override
public List<Car> selectAll() {
return carDao.findAll();
}
@Override
public Car selectById(Integer id) {
return carDao.findById(id);
}
}
10.写controller类(restful风格需要在属性前加@PathVariable注解)
package cn.tedu.controller;
import cn.tedu.pojo.Car;
import cn.tedu.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.xml.ws.Service;
import java.util.List;
@RestController
@RequestMapping("/car")
public class CarController {
@Autowired
private CarService carService;
/**
* http://localhost:8080/car/selectAll
* @return
*/
@RequestMapping("/selectAll")
public List<Car> selectAll(){
return carService.selectAll();
}
/**
* http://localhost:8080/car/selectById/1
* @param id
* @return
*/
@RequestMapping("/selectById/{id}")
public Car selectById(@PathVariable Integer id){
return carService.selectById(id);
}
}
成功
版权声明:本文为guozhe888原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。