永続層を増やして1
<dependency>
<groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
2.Mapper関連
Entityクラス
public class Emp {
private Integer empId; private String empName; private Integer empAge;
データベーステーブル
CREATE TABLE `table_emp` ( `emp_id` int NOT NULL AUTO_INCREMENT , `emp_name` varchar(100) NULL , `emp_age` int NULL , PRIMARY KEY (`emp_id`) )
マッパーの設定ファイル
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.springboot.mappers.EmpMapper">
<select id="selectAll" resultType="com.atguigu.springboot.bean.Emp">
select emp_id empId, emp_name empName, emp_age empAge
from table_emp
</select>
</mapper>
マッパーインタフェース
public interface EmpMapper {
List<Emp> selectAll(); }
サービスインタフェース
@Transactional
public interface EmpService { List<Emp> getAll(); }
サービスインタフェース
@Service
public class EmpServiceImpl implements EmpService { @Autowired private EmpMapper empMapper; @Override public List<Emp> getAll() { return empMapper.selectAll(); } }
ハンドラの呼び出し
@Autowired
private EmpService empService;
@ResponseBody
@RequestMapping("/getAll") public List<Emp> getAll() { return empService.getAll(); }
3.増加application.yml設定
spring:
datasource:
name: mydb
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/sb_db
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath*:/mybatis/*Mapper.xml
主走査における4.注釈はマッパークラスを開始します
@MapperScan("com.webcode.springboot.mappers")