美文网首页
SpringBoot集成MyBatis

SpringBoot集成MyBatis

作者: 磨陀货_ | 来源:发表于2019-10-22 16:30 被阅读0次

1.导包

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

2.entity

@Data
public class Employee implements Serializable{
    private Long id;
    private String name;
    private Integer age;

}

3.mapper

public interface EmployeeMapper {
    List<Employee> getAll();
}

4.*mapper.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="cn.zx.springboot.mapper.EmployeeMapper" >
    <select id="getAll" resultType="cn.zx.springboot.entity.Employee">
        SELECT id,name,age FROM employee
    </select>
</mapper>

5.application.properties

spring.datasource.url=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=utf8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=121212

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
mybatis.typeAliasesPackage: cn.zx.springboot.entity
mybatis.mapperLocations: classpath:mapper/*.xml

6.service

public interface IEmployeeService {
    List<Employee> getAll();
}
@Service
public class EmployeeServiceImpl implements IEmployeeService{
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAll() {
        return employeeMapper.getAll();
    }
}

7.controller

@RestController
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    public List<Employee> getAll(){
        return employeeService.getAll();
    }
}


8.效果

相关文章

网友评论

      本文标题:SpringBoot集成MyBatis

      本文链接:https://www.haomeiwen.com/subject/kroqvctx.html