1.创建项目添加依赖
创建SpringBoot项目,添加MySQL,Mybatis,Lombok依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.连接数据库,配置mybatis
修改默认配置文件application.properties 为application.yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: '改为自己的数据库密码'
url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
mybatis:
mapper-locations: classpath:mapper/*.xml
#type-aliases-package: com.cwy.mybatisstudy.demo.mapper
configuration:
map-underscore-to-camel-case: true #开启驼峰映射
3.实体类
PersonInfo.java
@Data
@Accessors(chain = true)
public class PersonInfo implements Serializable {
private String id;
private String name;
private String fatherName;
private String motherName;
private String phoneBrand;
private String homeAddress;
}
Study.java
@Data
@Accessors(chain = true)
public class Study extends PersonInfo implements Serializable {
private String studyLanguage;
}
4.Controller 层
PersonInfoController.java
@RestController
@RequestMapping("personInfo")
public class PersonInfoController {
@Autowired
private PersonInfoService personInfoService;
@RequestMapping("/list")
public List<PersonInfo> getList(){
return personInfoService.getList();
}
@RequestMapping("/list2")
public List<PersonInfo> getList2(){
return personInfoService.getList2();
}
@RequestMapping("/list3")
public List<Study> getList3(){
return personInfoService.getList3();
}
}
5.Service 层
PersonInfoService.java
public interface PersonInfoService {
List<PersonInfo> getList();
List<PersonInfo> getList2();
List<Study> getList3();
}
6.ServiceImpl 层
PersonInfoServiceImpl.java
@Service
public class PersonInfoServiceImpl implements PersonInfoService {
@Autowired(required = false)
private PersonInfoMapper personInfoMapper;
@Override
public List<PersonInfo> getList() {
return personInfoMapper.getList();
}
@Override
public List<PersonInfo> getList2() {
return personInfoMapper.getList2();
}
@Override
public List<Study> getList3() {
return personInfoMapper.getList3();
}
}
7.Mapper层
PersonInfoMapper.java
@Mapper
public interface PersonInfoMapper {
List<PersonInfo> getList();
List<PersonInfo> getList2();
List<Study> getList3();
}
8.XML 文件
PersonInfoMapper.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.cwy.mybatisstudy.demo.mapper.PersonInfoMapper">
<resultMap id="BaseResultMap" type="com.cwy.mybatisstudy.demo.entity.PersonInfo">
<id column="id" property="id" javaType="java.lang.String"></id>
<result column="name" property="name" javaType="java.lang.String"></result>
<result column="father_name" property="fatherName" javaType="java.lang.String"></result>
<result column="mother_name" property="motherName" javaType="java.lang.String"></result>
<result column="phone_brand" property="phoneBrand" javaType="java.lang.String"></result>
<result column="home_address" property="homeAddress" javaType="java.lang.String"></result>
</resultMap>
<resultMap id="BaseResultMap2" type="com.cwy.mybatisstudy.demo.entity.Study">
<id column="id" property="id" javaType="java.lang.String"></id>
<result column="name" property="name" javaType="java.lang.String"></result>
<result column="father_name" property="fatherName" javaType="java.lang.String"></result>
<result column="mother_name" property="motherName" javaType="java.lang.String"></result>
<result column="phone_brand" property="phoneBrand" javaType="java.lang.String"></result>
<result column="home_address" property="homeAddress" javaType="java.lang.String"></result>
<result column="study_language" property="studyLanguage" javaType="java.lang.String"></result>
</resultMap>
<select id="getList" resultMap="BaseResultMap">
<!--select id,name,father_name fatherName,mother_name motherName,phone_brand phoneBrand,home_address homeAddress FROM person_info -->
select * from person_info
</select>
<select id="getList2" resultType="com.cwy.mybatisstudy.demo.entity.PersonInfo">
<!--select id,name,father_name fatherName,mother_name motherName,phone_brand phoneBrand,home_address homeAddress FROM person_info -->
select * from person_info
</select>
<select id="getList3" resultMap="BaseResultMap2">
<!--select id,name,father_name fatherName,mother_name motherName,phone_brand phoneBrand,home_address homeAddress FROM person_info -->
SELECT p.*,s.study_language FROM person_info p,study s WHERE p.id=s.id
</select>
</mapper>
9.项目启动测试
访问:http://localhost:8080/personInfo/list
[{"id":"00001","name":"xiaoli","fatherName":"xiaojie","motherName":"xiaohong","phoneBrand":"iphone","homeAddress":"广东省茂名市"},{"id":"00002","name":"wujie","fatherName":"wubing","motherName":"wuhong","phoneBrand":"huawei","homeAddress":"广东省广州市"}]
访问:http://localhost:8080/personInfo/list2
[{"id":"00001","name":"xiaoli","fatherName":"xiaojie","motherName":"xiaohong","phoneBrand":"iphone","homeAddress":"广东省茂名市"},{"id":"00002","name":"wujie","fatherName":"wubing","motherName":"wuhong","phoneBrand":"huawei","homeAddress":"广东省广州市"}]
访问:http://localhost:8080/personInfo/list3
[{"id":"00001","name":"xiaoli","fatherName":"xiaojie","motherName":"xiaohong","phoneBrand":"iphone","homeAddress":"广东省茂名市","studyLanguage":"java"},{"id":"00002","name":"wujie","fatherName":"wubing","motherName":"wuhong","phoneBrand":"huawei","homeAddress":"广东省广州市","studyLanguage":"C++"}]