1.springboot整合mybatis
1.1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql的驱动依赖 3.-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis和springboot整合的jar.-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
1.2 配置文件
#数据源的信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/qy174-springboot?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#配置mybatis映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml
1.3 实体类
@Data
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
private String email;
private String head;
private static final long serialVersionUID = 1L;
}
1.4 Dao
public interface UserDao {
int deleteByPrimaryKey(Long id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List<User> selectAll();
}
1.5 映射文件
<?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.yzf.dao.UserDao">
<resultMap id="BaseResultMap" type="com.yzf.entity.User">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
<result property="head" column="head" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,name,age,
email,head
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select * from user
</select>
</mapper>
1.6 service
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> selectAll() {
List<User> list = userDao.selectAll();
return list;
}
}
1.7 controller
@RestController
@Api(tags = "用户管理")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("selectAll")
@ApiOperation(value = "查询所有用户")
private R selectAll(){
List<User> list = userService.selectAll();
if (list.size()>0){
return new R(200,"查询成功",list);
}
return new R(500,"查询失败",null);
}
}
1.8 dao包代理实现类
@SpringBootApplication
@MapperScan("com.yzf.dao")//为指定的dao生成代理实现类
@EnableSwagger2
public class Springboot712Application {
public static void main(String[] args) {
SpringApplication.run(Springboot712Application.class, args);
}
}
2.springboot整合mybatis-plus
MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
2.1引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
2.2 配置文件
#数据源信息同上
#映射文件路径
mybatis-plus.mapper-locations=classpath*:mapper/*.xml
#配置日志--sql日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2.4 Dao
public interface UserDao extends BaseMapper<User> {
}
其余步骤和前面一样
测试
@SpringBootTest
class Springboot712ApplicationTests {
@Autowired
private UserDao userDao;
@Test
void contextLoads() {
User user = userDao.selectById(44);
System.out.println(user);
}
}
//结果
JDBC Connection [HikariProxyConnection@18382672 wrapping com.mysql.cj.jdbc.ConnectionImpl@116659] will not be managed by Spring
==> Preparing: SELECT id,name,age,email,head FROM user WHERE id=?
==> Parameters: 44(Integer)
<== Columns: id, name, age, email, head
<== Row: 44, s, 18, 150@qq.com, 2.jpg
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@dff901]
User(id=44, name=s, age=18, email=150@qq.com, head=2.jpg)
3. springboot整合swagger2
Swagger2 是一个规范和完整的框架,用于生成、描述、调用和可视化Restful风格的web服务,现在我们使用spring boot 整合它。作用:接口的文档在线自动生成;功能测试;
3.1 如何使用swagger2
3.3 如何使用swagger2
<!--引入swagger2依赖-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<!--图形化依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
创建swagger2配置类
@Configuration
public class SwaggerConfig {
//创建swagger实例
@Bean
public Docket docket() {
Docket docket=
new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getInfo())//设置接口文档的信息
.select()
.apis(RequestHandlerSelectors.basePackage("com.ykq.controller")) //指定为那些路径下得到类生成接口文档
.build()
;
return docket;
}
private ApiInfo getInfo(){
Contact DEFAULT_CONTACT = new Contact("name", "http://www.ldw.com", "110@qq.com");
ApiInfo DEFAULT = new ApiInfo("用户管理系统API", "该系统中的接口专门操作用户的", "v1.0", "http://www.baidu.com",
DEFAULT_CONTACT, "巴巴", "http://www.jd.com", new ArrayList<VendorExtension>());
return DEFAULT;
}
}
开启swagger的注解驱动
@SpringBootApplication
@MapperScan("com.yzf.dao")
@EnableSwagger2//开启swagger的注解驱动
public class Springboot712Application {
public static void main(String[] args) {
SpringApplication.run(Springboot712Application.class, args);
}
}
访问swagger接口文档
第一种: http://localhost:8080/swagger-ui.html
第二种: http://localhost:8080/doc.html
3.2 swagger2常用注解
@Api
作用
:用来指定接口的描述文字
修饰范围
:作用在类上
@ApiOperation
作用
:用来对接口中具体方法做描述
修饰范围
:作用在方法上
value
:用来对接口的总体描述
notes
:用来对接口的详细描述
@ApiImplicitParams
作用
:用来对接口中参数进行说明
修饰范围
:作用在方法上
参数
:@ApiImplicitParam数组
@ApiImplicitParam
作用
:修饰接口方法里面的参
修饰范围
:作用方法上
参数
:
name
:方法参数名称value
:方法参数的描述dataType
:方法参数数据类型defaultValue
:方法参数默认值(给测试人员做测试用的)paramType
:
默认query
:对应方式一
path
:对应方式二body
:对应方式三
方式一:url?id=1&user='qlh’后面参数
@ApiOperation(value = "接口总体描述", notes = "<span style='color:red;'>详细描述:</span> 方法详细描述信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", dataType = "String", defaultValue = "qlh"),
@ApiImplicitParam(name = "password", value = "密码", dataType = "String", defaultValue = "123")
})
@PostMapping("/")
public String login(String username, String password) {
return "Hello login ~";
}
方式二:url/1/2路径后 传参 在路径中获取参数
@ApiOperation(value = "接口总体描述", notes = "<span style='color:red;'>详细描述:</span> 方法详细描述信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "id", dataType = "String", defaultValue = "qlh",paramType = "path"),
@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", defaultValue = "123",paramType = "path")
})
@PostMapping("/index/{id}/{name}")
public String index(@PathVariable("id") String id, @PathVariable("name") String name) {
return "Hello World ~";
}
方式三:在body中传参
@ApiOperation(value = "接口总体描述", notes = "<span style='color:red;'>详细描述:</span> 方法详细描述信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "id", dataType = "String", defaultValue = "xxx", paramType = "body"),
@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", defaultValue = "123", paramType = "body")
})
@PostMapping("/index")
public String index(@RequestBody Map<String, Object> map) {
return "Hello World ~";
}
实体类中swagger注解
@ApiModel
作用
:用来对实体类进行说明
修饰范围
:作用在类上
@ApiModel(value="类名",description = "实体类描述")
@ApiModelProperty
作用
:用来对实体类中的属性进行说明
修饰范围
:作用在类中的属性上
@ApiModelProperty(value = "类属性描述",required = true,example = "属性举例",notes = "备注")
4. springboot整合定时器
在指定的时间执行相应的业务代码。场景: oss修改图片时,存在一个冗余图片。定时删除冗余图片。
比如: 下单。30分钟未支付取消订单。 比如: 新用户注册成功后,7天发送问候语。
引入定时器依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
创建一个定时业务类
@Configuration
public class MyQuartzConfig {
//定时业务代码
//定义定时规则
@Scheduled(cron = "0/5 * * * * ?")
public void show(){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//发生短信 或者删除oss冗余文件 或者取消订单
}
}
开启定时器注解驱动
@SpringBootApplication
@MapperScan("com.yzf.dao")
@EnableSwagger2//开启定时器注解驱动
public class Springboot712Application {
public static void main(String[] args) {
SpringApplication.run(Springboot712Application.class, args);
}
}