准备工作
1.准备数据库等一些列操作
创建一个测试所用的数据库
创建service,dao层等
创建mapper.xml文件(dao层操作数据库的文件)
…操作和SSM类似
2.配置文件
application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
mybatis:
type-aliases-package: com.xjj.demo.domain #别名包
mapper-locations: classpath:mybatis/*.xml #Mapper.XML文件
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #mybatis日志文件
3.Service和Dao层调用关系
IPersonService
public interface IPsersonService {
List<Person> findAllPerson();
}
PersonServiceimpl
import com.xjj.demo.dao.IPersonDao;
import com.xjj.demo.domain.Person;
import com.xjj.demo.service.IPsersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonServiceimpl implements IPsersonService {
@Autowired
IPersonDao personDao;
@Override
public List<Person> findAllPerson() {
List<Person> list=personDao.findAll();
return list;
}
}
IPersonDao
import com.xjj.demo.domain.Person;
import java.util.List;
public interface IPersonDao {
List<Person> findAll();
}
IPersonDao.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.xjj.demo.dao.IPersonDao">
<select id="findAll" resultType="person">
select * from person
</select>
</mapper>
注意
在Springboot中相应的Mapper.xml文件可随意放置
在这里我放置在:/mybatis下
相的,在整合Mybatis 文件中,Mapper.xml文件信息如下:
因为我不可能只读取一个Mapper.xml文件,所有用*来替代文件名
因为SpringBoot启动一个程序来运行
测试!
测试数据库中的数据
测试代码
@Test
public void test02(){
List<Person> list=Service.findAllPerson();
System.out.println(list);
测试结果
和测试数据库中的数据相匹配!