spring-boot - mybatis
开发工具
idea2020.1.3
jdk1.8
centOS 7.x
navicat
xshell
xftp
以下部分均不包含软件安装和环境变量的配置,如果安装有问题请私信,近期会出一篇针对安装的问题
以下部分暂不包含部署问题,部署部分包含在我的另一篇博客 部署的文章
一、新建项目
1、new project - spring Initializr
2、填写名字
3、选择相关的配置
二、修改配置文件
1、删除resources下的application.properties
2、新建application.yml和application-dev.yml,并写入
spring:
profiles:
active: dev
server:
port: 8080
spring:
datasource:
username: root
password: 1234
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.testjava.springboot.bean
#showSql
logging:
level:
com:
testjava:
springboot:
mapper : debug
三、新建包名,并写入文件
1、新建包名controller、bean、mapper、services
2、resources下新建mapper文件夹
3、在bean目录下新建UserBean.class,并写入
package com.testjava.springboot.bean;
import lombok.Data;
@Data
public class UserBean {
private String userName;
private int id;
private int age;
}
4、在resources/mapper下新建文件UserMapper.xml,并写入sql语句
<?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.testjava.springboot.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.testjava.springboot.bean.UserBean">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<select id="getUserById" resultType="com.testjava.springboot.bean.UserBean">
select * from user where id = #{id}
</select>
</mapper>
5、在mapper目录下新建UserMapper.class,并写入
package com.testjava.springboot.mapper;
import com.testjava.springboot.bean.UserBean;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
UserBean getUserById(int id);
}
6、在services目录下新建UserService.class,并写入
package com.testjava.springboot.services;
import com.testjava.springboot.bean.UserBean;
import com.testjava.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
public UserMapper userMapper;
public UserBean getUserById(int id) {
return userMapper.getUserById(id);
}
}
7、在controller目录下新建UserController.class,并写入
package com.testjava.springboot.controller;
import com.testjava.springboot.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/springboot")
public class UserController {
@Autowired
public UserService userService;
@RequestMapping("getUserById/{id}")
public String getUserById(@PathVariable int id) {
return userService.getUserById(id).toString();
}
}
8、在系统自动生成的Application内编辑
package com.testjava.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.testjava.springboot.mapper") //扫描的mapper
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
四、准备编译运行
1、直接通过右上角运行,并调用本地接口:http://localhost:8080/springboot/getUserById/1
2、运行成功后,打包并部署,打包命令 : mvn clean package,生成的文件为jar包格式,在target目录下
3、部署,请参照我的另一篇文章:部署的文章
以上内容参考:https://blog.csdn.net/iku5200/article/details/82856621