使用IDEA+springboot创建ssm+gradle项目(一)

699 阅读1分钟

创建项目

新建项目,选择SpringIntializr,创建方式选择spring官方链接即可。(点开此链接也可以在线创建项目)

下一步,Type选择gradle

下一步,勾选Spring Boot DevTools,Spring web,Mybatis Framework,MySQL Driver

下一步,点击finish,项目即创建完成。

连接数据库跑通测试

创建实体

public class UserEntity {
    private long id;
    private String name;
    private String code;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

创建dao

@Repository
public interface UserDao {
    @Select("select * from t_user")
    @Results({
            @Result(property = "name",column = "name"),
            @Result(property = "code",column="code")
    })
    List<UserEntity> getAll();
}

创建controller

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDao userDao;

    @RequestMapping("/getAll")
    public List<UserEntity> getAll() {
        return userDao.getAll();
    }
}

Demo9Application文件中中添加@MapperScan("com.example.demo9.dao")用于扫描dao,可以避免每个dao都去添加mapper注解了

@MapperScan("com.example.demo9.dao")
@SpringBootApplication
public class Demo9Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo9Application.class, args);
    }

}

配置application.properties

#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=testtest

现在项目就配置完了,具体目录结构如下:

现在可以运行程序了,运行后使用地址:http://localhost:8080/user/getAll如果返回表中数据就说明调用成功了。