1.pom 配置
<!--mybatis配置 start--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!--mybatis配置 end-->
2.resources资源中application.yml 配置
spring: datasource: url: jdbc:mysql://192.168.0.1:3306/数据库?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
3.如果没有resources资源文件夹,可先建立resources 文件夹
file-》project structure-》Modules-》sources-》 找到 resources文件夹
点击红框中resources 点击 Apply OK 就可以了
4.建立实体包 entity
创建user实体类
@Setter @Getter public class user { private String id; private String name; private int sex; }//end
5.建立mapper包
创建 userMapper 接口
@Repository public interface userMapper { @Select("select * from user where id=#{id}") user selectUser(String id); }//end
6.建立service包
创建 UserService 接口
public interface UserService { user selectUser(String id); }//end
创建实现类 UserServiceImpl
@Service public class UserServiceImpl implements UserService { @Autowired userMapper _userMapper; @Override public user selectUser(String id) { return _userMapper.selectUser(id); } }//end
7. 创建testController 类
@RestController @RequestMapping(value = "/test") public class testController { @Autowired private UserService _UserService; @RequestMapping(value = "/select/{id}") public String selectUser(@PathVariable String id) { return _UserService.selectUser(id).getName(); } }//end
8.主程序入口,加入Mapper 扫包范围
@MapperScan("mapper包路径") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }