1.引入依赖
超级重点: 因为idea自带的maven不能用所以要手动在settings中配置:如图![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0753357d51c86a1c79b2ebb23a27f731.png)
1.1创建maven普通工程,引入依赖(springboot自动装配的依赖 和 mybatis的依赖)
<!--引入父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- web相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 阿里巴巴的Druid数据源依赖启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- MyBatis依赖启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- MySQL数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2.配置application.properties配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/student_db?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=30
spring.datasource.druid.max-active=50
spring.datasource.druid.min-idle=20
#开启驼峰写法
mybatis.configuration.map-underscore-to-camel-case=true
#如果配置文件 和 类名对应包名可以省略,否则 需要声明位置
#mybatis.mapper-locations=classpath:mapper/*.xml
#配置别名
mybatis.type-aliases-package=com.aaa.springboot.entity
3.创建com.aaa.springboot根目录,并在当前包下面创建启动器MyApplication
3.1注意:MyApplication启动器必须在根目录下创建,否则会有问题
@SpringBootApplication //标记当前类是一个springboot启动类,注意一个应用只有一个启动类;
//注意一个应用只有一个启动类
//启动类要放在根目录,只有两个目录,springboot 应用才会扫描到对应的controller , service ,dao ,entity层里的东西.
public class MyApplication {
public static void main(String[] args) {
//启动springboot应用
SpringApplication.run(MyApplication.class,args);
}
}
3.2.1com.aaa.springboot.dao.RoomDao
@Mapper
public interface RoomDao {
/*@Select("select * from tabroom")*/
List<Room> selectAllRoom();
@Delete("delete from tabroom where roomid= #{roomid}")
int deleteRoom(int id) ;
@Update("update room set roomname = #{roomname} where roomid = #{roomid}")
int updateRoom(Room room);
@Insert("insert into room (roomname) values (#{roomname})")
int insertRoom(Room room);
}
3.2.2在****.xml 文件中配置 com/aaa/springboot/dao/RoomDao.xml 路径必须与dao层一致,而且与接口的类名相同![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ecff54e8eabd0dfe753d09e789c3b578.png)
<?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 对应的是字符串一般是 对应dao层 接口全限定名 -->
<mapper namespace=""> </mapper>
3.3com.aaa.springboot.entity.Room
public class Room {
private int roomid;
private String roomname;
private List<Owner> ownerList;
@Override
public String toString() {
return "Room{" +
"roomid=" + roomid +
", roomname='" + roomname + '\'' +
", ownerList=" + ownerList +
'}';
}
public int getRoomid() {
return roomid;
}
public void setRoomid(int roomid) {
this.roomid = roomid;
}
public String getRoomname() {
return roomname;
}
public void setRoomname(String roomname) {
this.roomname = roomname;
}
public List<Owner> getOwnerList() {
return ownerList;
}
public void setOwnerList(List<Owner> ownerList) {
this.ownerList = ownerList;
}
}
3.4com.aaa.springboot.MyApplicationTest 测试必须与MyApplication在同一目录下
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTest {
@Autowired
private OwnerDao ownerDao;
@Test
public void selectAll(){
List<Owner> ownerList = ownerDao.findAllOwner();
for (Owner owner:ownerList){
System.out.println(owner);
}
}
}
注意:如果测试输出值可以,就证明mybatis配置成功;
4.配置 service层 与 controller层,主要是注入@Autowired 怎么标明注入的注解
4.1controller层
@RestController
public class RoomController {
@Autowired
private RoomService roomService;
}
4.2service层
@Service
public class RoomServiceImpl implements RoomService {
@Autowired
private RoomDao roomDao;
}
最后启动MyApplication,选择好端口号,在网页上进行输入即可.