一、使用idea创建一个springboot项目 1.新建一个module(新建一个新项目也是可以的,我这是在原有的父module下新建子module) 2. 3. 4.
二、在springboot项目中整合mybatis 1.导入相关的依赖:mybatis、mysql、lombok 在pom文件中导入依赖,如下(注意版本的兼容):
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
2.配置文件的设置 在resources目录下新建配置文件 application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
mybatis:
type-aliases-package: com.study.pojo
mapper-locations: classpath:mybatis/*.xml
3.新建数据库 mydb和数据表 user,并插入数据,如下所示:
CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(20) NOT NULL,
`upassword` varchar(20) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
4.完善springboot项目的文件结构,如下所示
5.在pojo包中新建实体类User
package com.study.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private Integer uid;
private String uname;
private String upassword;
}
6.在service包中新建接口 UserService
package com.study.service;
import com.study.pojo.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
7.在controller包下新建UserController类
package com.study.controller;
import com.study.pojo.User;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("findAll")
@ResponseBody
public List<User> findAll(){
return userService.findAll();
}
}
8.在mapper包下新建UserMapper接口
package com.study.mapper;
import com.study.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> selectAll();
}
9.在service下新建一个包impl,在impl包下新建一个类UserServiceImpl,实现UserService接口;
package com.study.service.impl;
import com.study.mapper.UserMapper;
import com.study.pojo.User;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectAll();
}
}
【注意,如果userMapper爆红,不是语法错误,降低idea的代码判断级别就可以了】 在idea的右下角,将代码判断级别设置为中等就不会爆红了;
10.在mybatis下新建UserMapper.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.study.mapper.UserMapper">
<select id="selectAll" resultType="user">
select * from user;
</select>
</mapper>
三、启动项目,测试是否整合成功 1.项目启动成功 2.测试: 在浏览器输入:http://localhost:8080/user/findAll
|