-
idea创建springboot项目
在这里插入图片描述
只需要选择这一个
这个只需要选择一个web就可以了 ,其他的单独从pom文件中导入
- 从pom.xml文件中导入mysql和mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 在application.properties配置文件中添加mysql配置以及mybatis的配置
# ============================== mysql 配置 ============================================================================
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/hello?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
# ============================== mybatis 配置 ==========================================================================
# 所有的mapper映射文件
mybatis.mapper-locations=classpath:mapping/**/*.xml
# 定义所有操作类的别名所在包
mybatis.type-aliases-package=com.lover.mybatistest
- 在com.lover.mybatistest包下创建包 mapper、entity、service、controller、utils 以备用,在resource下创建mapping文件夹以备用
- 在entity包下创建实体类 User.java
//省略getter和setter
public class User {
private String id;
private int age;
private String name;
}
- 在mapping文件夹下创建 UserMapping.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.lover.mybatistest.mapper.UserMapper">
<!--这里面对应你的实体类和数据库表-->
<resultMap id="BaseResultMap" type="com.lover.mybatistest.entity.User">
<result column="id" jdbcType="VARCHAR" property="id" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<!--这里写sql语句,id就是方法名-->
<select id="selectUserById" resultType="com.lover.mybatistest.entity.User">
select * from t_user where id = #{id}
</select>
</mapper>
- 在 mapper包下创建接口 UserMapper.java
package com.lover.mybatistest.mapper;
import com.lover.mybatistest.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User selectUserById(String id);
}
- 在service包下创建接口 UserService.java 和 包 impl
public interface UserService {
public User selectUserById(String id);
}
- 在 impl 包下创建UserService的实现类 UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectUserById(String id) {
return userMapper.selectUserById(id);
}
}
- 在utils 包下创建工具类 Result.java
//省略getter和setter
public class Result {
private int code = 200;
private String msg = "操作成功";
private Object data;
private int total;
public Result(int code,String msg){
this.code = code;
this.msg = msg;
}
public Result(Object data){
this.data = data;
}
}
- 在 controller包下创建 HelloController.java
@RestController
public class HelloController {
@Autowired
private UserService userService;
@GetMapping("/")
public String hello(){
return "hello";
}
@GetMapping("/selectById")
public Result selectById(String id){
return new Result(userService.selectUserById(id));
}
}
- 最后一步 修改启动类 MybatisTestApplication 如下
@SpringBootApplication
@MapperScan(value = {"com.lover.mybatistest.mapper"}) //扫描mapper
public class MybatisTestApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisTestApplication.class, args);
}
}
到此mybatis就已经集成完毕了,运行程序,在浏览器中输入 http://localhost:8088/selectById?id=1 就能够正常查询,效果如下
最后附上项目目录结构
在这里插入图片描述
网友评论